jarun / nnn

n³ The unorthodox terminal file manager
BSD 2-Clause "Simplified" License
19.4k stars 763 forks source link

mess with shell colors #1883

Closed guest73 closed 6 months ago

guest73 commented 6 months ago

Environment details (Put x in the checkbox along with the information)

Exact steps to reproduce the issue

  1. Open nnn and press e to edit any text file
  2. The first time vim opens all the colors are correct
  3. Close vim and then press e again to edit the file. Now some colors are messed

On each bash open I source this file to change shell colors

#!/usr/bin/env sh
# tinted-shell (https://github.com/tinted-theming/tinted-shell)
# Scheme name: pandora 
# Scheme author: Cassandra Fox
# Template author: Tinted Theming (https://github.com/tinted-theming)
export BASE16_THEME=pandora

color00="13/12/13" # Base 00 - Black
color01="b0/0b/69" # Base 08 - Red
color02="9d/df/69" # Base 0B - Green
color03="ff/cc/00" # Base 0A - Yellow
color04="00/80/80" # Base 0D - Blue
color05="a2/40/30" # Base 0E - Magenta
color06="71/4c/a6" # Base 0C - Cyan
color07="f1/5c/99" # Base 05 - White
color08="ff/be/e3" # Base 03 - Bright Black
color09="$color01" # Base 08 - Bright Red
color10="$color02" # Base 0B - Bright Green
color11="$color03" # Base 0A - Bright Yellow
color12="$color04" # Base 0D - Bright Blue
color13="$color05" # Base 0E - Bright Magenta
color14="$color06" # Base 0C - Bright Cyan
color15="63/22/27" # Base 07 - Bright White
color16="ff/91/53" # Base 09
color17="a2/40/30" # Base 0F
color18="2f/18/23" # Base 01
color19="47/22/34" # Base 02
color20="9b/2a/46" # Base 04
color21="81/50/6a" # Base 06
color_foreground="f1/5c/99" # Base 05
color_background="13/12/13" # Base 00

if [ -z "$TTY" ] && ! TTY=$(tty); then
  put_template() { true; }
  put_template_var() { true; }
  put_template_custom() { true; }
elif [ -n "$TMUX" ] || [ "${TERM%%[-.]*}" = "tmux" ]; then
  # Tell tmux to pass the escape sequences through
  # (Source: http://permalink.gmane.org/gmane.comp.terminal-emulators.tmux.user/1324)
  put_template() { printf '\033Ptmux;\033\033]4;%d;rgb:%s\033\033\\\033\\' "$@" > "$TTY"; }
  put_template_var() { printf '\033Ptmux;\033\033]%d;rgb:%s\033\033\\\033\\' "$@" > "$TTY"; }
  put_template_custom() { printf '\033Ptmux;\033\033]%s%s\033\033\\\033\\' "$@" > "$TTY"; }
elif [ "${TERM%%[-.]*}" = "screen" ]; then
  # GNU screen (screen, screen-256color, screen-256color-bce)
  put_template() { printf '\033P\033]4;%d;rgb:%s\007\033\\' "$@" > "$TTY"; }
  put_template_var() { printf '\033P\033]%d;rgb:%s\007\033\\' "$@" > "$TTY"; }
  put_template_custom() { printf '\033P\033]%s%s\007\033\\' "$@" > "$TTY"; }
elif [ "${TERM%%-*}" = "linux" ]; then
  put_template() { [ "$1" -lt 16 ] && printf "\e]P%x%s" "$1" "$(echo "$2" | sed 's/\///g')" > "$TTY"; }
  put_template_var() { true; }
  put_template_custom() { true; }
else
  put_template() { printf '\033]4;%d;rgb:%s\033\\' "$@" > "$TTY"; }
  put_template_var() { printf '\033]%d;rgb:%s\033\\' "$@" > "$TTY"; }
  put_template_custom() { printf '\033]%s%s\033\\' "$@" > "$TTY"; }
fi

# 16 color space
put_template 0  "$color00"
put_template 1  "$color01"
put_template 2  "$color02"
put_template 3  "$color03"
put_template 4  "$color04"
put_template 5  "$color05"
put_template 6  "$color06"
put_template 7  "$color07"
put_template 8  "$color08"
put_template 9  "$color09"
put_template 10 "$color10"
put_template 11 "$color11"
put_template 12 "$color12"
put_template 13 "$color13"
put_template 14 "$color14"
put_template 15 "$color15"

# 256 color space
put_template 16 "$color16"
put_template 17 "$color17"
put_template 18 "$color18"
put_template 19 "$color19"
put_template 20 "$color20"
put_template 21 "$color21"

# foreground / background / cursor color
if [ -n "$ITERM_SESSION_ID" ]; then
  # iTerm2 proprietary escape codes
  put_template_custom Pg f15c99 # foreground
  put_template_custom Ph 131213 # background
  put_template_custom Pi f15c99 # bold color
  put_template_custom Pj 472234 # selection color
  put_template_custom Pk f15c99 # selected text color
  put_template_custom Pl f15c99 # cursor
  put_template_custom Pm 131213 # cursor text
else
  put_template_var 10 "$color_foreground"
  if [ "$BASE16_SHELL_SET_BACKGROUND" != false ]; then
    put_template_var 11 "$color_background"
    if [ "${TERM%%-*}" = "rxvt" ]; then
      put_template_var 708 "$color_background" # internal border (rxvt)
    fi
  fi
  put_template_custom 12 ";7" # cursor (reverse video)
fi

# clean up
unset put_template
unset put_template_var
unset put_template_custom
unset color00
unset color01
unset color02
unset color03
unset color04
unset color05
unset color06
unset color07
unset color08
unset color09
unset color10
unset color11
unset color12
unset color13
unset color14
unset color15
unset color16
unset color17
unset color18
unset color19
unset color20
unset color21
unset color_foreground
unset color_background

# Optionally export variables
if [ -n "$TINTED_SHELL_ENABLE_BASE16_VARS" ] || [ -n "$BASE16_SHELL_ENABLE_VARS" ]; then
  export BASE16_COLOR_00_HEX="131213"
  export BASE16_COLOR_01_HEX="2f1823"
  export BASE16_COLOR_02_HEX="472234"
  export BASE16_COLOR_03_HEX="ffbee3"
  export BASE16_COLOR_04_HEX="9b2a46"
  export BASE16_COLOR_05_HEX="f15c99"
  export BASE16_COLOR_06_HEX="81506a"
  export BASE16_COLOR_07_HEX="632227"
  export BASE16_COLOR_08_HEX="b00b69"
  export BASE16_COLOR_09_HEX="ff9153"
  export BASE16_COLOR_0A_HEX="ffcc00"
  export BASE16_COLOR_0B_HEX="9ddf69"
  export BASE16_COLOR_0C_HEX="714ca6"
  export BASE16_COLOR_0D_HEX="008080"
  export BASE16_COLOR_0E_HEX="a24030"
  export BASE16_COLOR_0F_HEX="a24030"
fi

Even if I close nnn the colors remain messed until I source the above file again. What nnn could potentially do to cause this behaveor? Peek 2024-05-13 03-50

guest73 commented 6 months ago

Basically, the vertical line at the right becomes blue, selection becomes blue but most annoyingly, some numbers become black making them unreadable :(

N-R-K commented 6 months ago

nnn shouldn't be doing anything like that. It's most likely something in your .bashrc or .vimrc or similar.

Try to see if you can reproduce in a clean environment without any .bashrc or .vimrc etc.

guest73 commented 6 months ago

nnn shouldn't be doing anything like that. It's most likely something in your .bashrc or .vimrc or similar.

Try to see if you can reproduce in a clean environment without any .bashrc or .vimrc etc.

But nevertheless it does something. Tried on stock .bashrc, minimal vim config where loaded just 1 plugin Plug 'tinted-theming/base16-vim

let base16_colorspace=256
let base16_background_transparent=1
colorscheme base16-default-dark

and then source .config/bash-config/colors.sh - which is the script posted in original message Run nnn, do the steps and it happens again. I am not an expert on bash, but I guess nnn does something weird with sub-shelling when calling Vim with e key. I can run vim a couple of times, do some editing, run MidnightCommander, trigger vim from it, do more editing, quit MC, do editing again, and no problem whatsoever. Also when I run nnn and simply do the navigation and then quit it also works ok. But once I press e and start vim and quit it, next time I run vim its colors are screwed. It might be something related to xfce-terminal I use, but I doubt it.

N-R-K commented 6 months ago

source .config/bash-config/colors.sh

Are you putting this in your bashrc?

guest73 commented 6 months ago

source .config/bash-config/colors.sh

Are you putting this in your bashrc?

It does not matter. I can put it in .bashrc or source it in the shell, the result is the same.

N-R-K commented 6 months ago

I guess nnn does something weird with sub-shelling when calling Vim with e key.

nnn doesn't spawn any sub-shells. It executes $EDITOR directly.

Plug 'tinted-theming/base16-vim

It seems like this colorscheme executes some shell script: https://github.com/tinted-theming/base16-vim/blob/17b238f0c173b4c174523690bb41cdab952ee27b/colors/base16-default-dark.vim#L12-L16

I suspect this is what's causing the issue. If you cannot reproduce it with vim --clean then the problem is likely not in nnn.

guest73 commented 6 months ago

Ok. I figured. What helped was setting

set termguicolors

inside .vimrc. This is mentioned inhere Troubleshooting

Also

It seems like this colorscheme executes some shell script: https://github.com/tinted-theming/base16-vim/blob/17b238f0c173b4c174523690bb41cdab952ee27b/colors/base16-default-dark.vim#L12-L16

is not true. None of the docs for either TintedTheming Vim or Base16-vim was actually saying I must set this variable let g:base16_shell_path, so I did not. And without this variable nothing was actually executed. But I guess it must be set in order to work in login shells or something.

Nevertheless, @N-R-K thanks for pointing me in the right direction.

guest73 commented 6 months ago

Seems like indeed not nnn's problem.