tinted-theming / tinted-shell

Base16 and Base24 for Shells
https://github.com/tinted-theming/home
MIT License
65 stars 217 forks source link

Fix bug where TMUX doesn't load vim with the correct theme #14

Closed JamyGolden closed 2 years ago

JamyGolden commented 2 years ago

Fix tmux session bug mentioned in issue: https://github.com/base16-project/base16-shell/issues/1#issuecomment-1191866762

JamyGolden commented 2 years ago

@nbn22385 @jonleopard Could you two test this branch out (jamy/bugfix/tmux-session-env-vars) and see if it solves the issue for you?

jonleopard commented 2 years ago

Testing now. One issue I'm seeing off the bat is that the setup_theme.vim file does not exist in $HOME/.config/base16-project/setup_theme.vim. I have two files in this directory: base16_shell_theme and tmux.base16.conf.

Here's the full error:

Error detected while processing /Users/jon/dotfiles/nvim/.config/nvim/init.lua:
E5113: Error while calling lua chunk: /Users/jon/.config/nvim/lua/jon/colors.lua:9: Vim(source):E484: Can't
open file '/Users/jon/.config/base16-project/setup_theme.vim'
stack traceback:
        [C]: in function 'cmd'
        /Users/jon/.config/nvim/lua/jon/colors.lua:9: in main chunk
        [C]: in function 'require'
        /Users/jon/.config/nvim/lua/jon/init.lua:5: in main chunk
        [C]: in function 'require'
        /Users/jon/dotfiles/nvim/.config/nvim/init.lua:1: in main chunk
Press ENTER or type command to continue

Besides that, it seems to be working. When I switch themes and open a new pane, the theme matches correctly.

JamyGolden commented 2 years ago

@jonleopard thanks for testing! I feel like you may have tested with the old profile_helper.sh sourced. Maybe that is not the case but since I'm not getting any errors I just want to make sure.

  1. Checkout this branch within your base16-shell repo (probably ~/.config/base16-shell)
  2. Kill tmux entirely (not just detach)
  3. Run rm -rf ~/.config/base16-project
  4. Open a new terminal session/window
  5. Run base16_ocean
  6. ls -la ~/.config/base16-project and you should see all the files there.
nbn22385 commented 2 years ago

I also wasn't able to get the tmux fix to work using the steps below.

  1. Ensure tmux detached and server not running
  2. Delete folders ~/.config/base16-shell and ~/.config/base16-project
  3. Clone your branch with git clone --single-branch --branch jamy/bugfix/tmux-session-env-vars https://github.com/base16-project/base16-shell.git
  4. Launch a new terminal
  5. Run base16_ocean, theme applied successfully
  6. Start tmux, create an additional pane (now 2 exist)
  7. Change theme on pane 1, open vim on pane 2 and problem still persists.

Am I supposed to have the base16-tmux plugin installed? I have never used it in the past. I did not apply any of the README steps under the Hooks/Vim section.

After setting the theme, my base16-project folder has:

➜ ls -l base16-project
total 8
lrwxrwxrwx 1 vagrant vagrant 70 Aug  8 09:50 base16_shell_theme -> /home/vagrant/.config/base16-shell/scripts/base16-gruvbox-dark-hard.sh
-rw-rw---- 1 vagrant vagrant 18 Aug  8 09:50 theme_name

I added some debug statements in profile_helper.sh to see if the set_theme function was getting called, but it would always exit at this point:

# If $BASE16_THEME is set, this has already been loaded. This guards
# against a bug where this script is sourced two or more times.
if [ -n "$BASE16_THEME" ]; then
  echo "Theme already loaded, exiting helper"
  return 0
fi

Hope some of this info is helpful.

JamyGolden commented 2 years ago

Thanks yeah that is helpful. I thought you were both using base16-tmux. I'll create a hook to create those files without relying on base16-tmux existing.

jonleopard commented 2 years ago

That managed to get me a step further! Couple findings:

The filenames are different in the README.md vs what gets put in the base16-project directory. In the README.md, its setup_theme, the script creates the file as set_theme. I manually changed the filenames and neovim is now trying to open the file with another error:

Error detected while processing /Users/jon/dotfiles/nvim/.config/nvim/init.lua:
E5113: Error while calling lua chunk: /Users/jon/.config/nvim/lua/jon/colors.lua:9: Vim(source):E484: Can't open f
ile '/Users/jon/.config/base16-project/set_theme.vim'
stack traceback:
        [C]: in function 'cmd'
        /Users/jon/.config/nvim/lua/jon/colors.lua:9: in main chunk
        [C]: in function 'require'
        /Users/jon/.config/nvim/lua/jon/init.lua:5: in main chunk
        [C]: in function 'require'
        /Users/jon/dotfiles/nvim/.config/nvim/init.lua:1: in main chunk
Press ENTER or type command to continue

I think the quotes in the cmd function were throwing it off? I removed them and it seems to find the file now, but I am getting another error:

Error detected while processing /Users/jon/dotfiles/nvim/.config/nvim/init.lua:
E5113: Error while calling lua chunk: /Users/jon/.config/nvim/lua/jon/colors.lua:9: Vim(colorscheme):E185: Cannot
find color scheme 'nord'
stack traceback:
        [C]: in function 'cmd'
        /Users/jon/.config/nvim/lua/jon/colors.lua:9: in main chunk
        [C]: in function 'require'
        /Users/jon/.config/nvim/lua/jon/init.lua:5: in main chunk
        [C]: in function 'require'
        /Users/jon/dotfiles/nvim/.config/nvim/init.lua:1: in main chunk
Press ENTER or type command to continue

Is there a reason the local variable is using set_theme.lua, and then the cmd function calls set_theme.vim? I changed it to call set_theme.lua instead and this shows up:

Error detected while processing /Users/jon/dotfiles/nvim/.config/nvim/init.lua:
E5113: Error while calling lua chunk: /Users/jon/.config/nvim/lua/jon/colors.lua:9: Vim(source):E5113: Error while
 calling lua chunk: /Users/jon/.config/base16-project/set_theme.lua:1: attempt to index global 'g' (a nil value)
stack traceback:
        /Users/jon/.config/base16-project/set_theme.lua:1: in main chunk
        [C]: in function 'cmd'
        /Users/jon/.config/nvim/lua/jon/colors.lua:9: in main chunk
        [C]: in function 'require'
        /Users/jon/.config/nvim/lua/jon/init.lua:5: in main chunk
        [C]: in function 'require'
        /Users/jon/dotfiles/nvim/.config/nvim/init.lua:1: in main chunk
stack traceback:
        [C]: in function 'cmd'
        /Users/jon/.config/nvim/lua/jon/colors.lua:9: in main chunk
        [C]: in function 'require'
        /Users/jon/.config/nvim/lua/jon/init.lua:5: in main chunk
        [C]: in function 'require'
        /Users/jon/dotfiles/nvim/.config/nvim/init.lua:1: in main chunk
Press ENTER or type command to continue

For reference, this is what I am using in my colors.lua file:

local fn = vim.fn
local cmd = vim.cmd
local setup_script_path = "$HOME/.config/base16-project/set_theme.lua"

if fn.filereadable(fn.expand(setup_script_path)) then
  cmd("let base16colorspace=256")
  cmd("source $HOME/.config/base16-project/set_theme.lua")
end
JamyGolden commented 2 years ago

I fixed the readme, the bug in set_theme.lua and @jonleopard you shouldn't get those errors anymore once you update your colors.lua with the code in the readme.

@nbn22385 I've removed that base16-tmux dependency, that condition wasn't meant to be in the base16-vim.sh hook.

nbn22385 commented 2 years ago

I repeated the test but saw the same result as before.

Should I be following the instructions under the Base16-vim users / vim section or the Hooks/vim section? Using the former still relies on environment variable BASE16_THEME and exits the profile_helper logic early if it's defined. Using the latter doesn't work for me since the set_theme.vim file isn't created.

Edit: For now i put this in my vim configuration and it seems to reliably pull the latest applied theme:

if filereadable(expand("$HOME/.config/base16-project/theme_name"))
  let s:theme = readfile(expand("$HOME/.config/base16-project/theme_name"))[0]
  exec 'colorscheme base16-' .. s:theme
endif
jonleopard commented 2 years ago

All is working like a charm now! tmux, shell, and vim are all in sync even when opening new panes.

JamyGolden commented 2 years ago

@nbn22385 Regarding the readme, my initial thought was initially that someone could still use base16-shell, vim (and not tmux) and still use the BASE16_THEME variable. I've moved the info into a more relevant area now though (under tmux configuration).

Could you try one more time? I'm not sure why it wasn't creating the set_theme.vim/set_theme.lua files for you, but it should, otherwise it means the base16-shell/hooks/base16-vim.sh file isn't running.

nbn22385 commented 2 years ago

Could you try one more time? I'm not sure why it wasn't creating the set_theme.vim/set_theme.lua files for you, but it should, otherwise it means the base16-shell/hooks/base16-vim.sh file isn't running.

I cleaned my environment and tried again. Working great now, sorry for the confusion!

JamyGolden commented 2 years ago

Great, I'll merge this soon! (@belak want to have a look over this before I merge?)