Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bat not using local theme when passed as a flag inside vim #634

Closed
snide opened this issue Aug 23, 2019 · 7 comments
Closed

Bat not using local theme when passed as a flag inside vim #634

snide opened this issue Aug 23, 2019 · 7 comments
Labels
help wanted Extra attention is needed themes upstream-error A bug in an upstream component

Comments

@snide
Copy link

snide commented Aug 23, 2019

Hi there. Thank you for this fantastic repo. As a UI designer who loves terminal tooling and Vim (all dozen of us!) this project has been really exciting for theming and I'd love to contribute some.

I'm not certain this is the right place to ask the issue, but figured I'd give it a shot. My issue is that bat doesn't seem to respect custom locally built themes when called with a --theme="myTheme" flag inside vim (specifically I'm using Neovim)

I'm using Bat, and following the readme created a custom theme called "horizon". It works perfectly in my terminal and shows up correctly in my Fish setup. I call it from a bat/bat.config file.

image

#Set the theme to "Horizon"
--theme="horizon"

# Show line numbers, Git modifications and file header (but no grid)
--style="numbers,changes,header"

# Use italic text on the terminal (not supported on all terminals)
--italic-text=always

# Add mouse scrolling support in less (does not work with older
# versions of "less")
--pager="less -FR"

# Use C++ syntax (instead of C) for .h header files
--map-syntax h:cpp

# Use "gitignore" highlighting for ".ignore" files
--map-syntax .ignore:.gitignore

In VIM, I use bat along with FZF to to provide dynamic previews to my searching.

function! Fzf_dev(qargs)
  let l:fzf_files_options = '--preview "bat --theme="horizon" --style numbers,changes --color always {2..-1} | head -'.&lines.'" --expect=enter,ctrl-t,ctrl-v,ctrl-x --multi --bind=ctrl-a:select-all,ctrl-d:deselect-all'

  function! s:files(dir)
    let l:cmd = $FZF_DEFAULT_COMMAND
    if a:dir != ''
      let l:cmd .= ' ' . shellescape(a:dir)
    endif
    let l:files = split(system(l:cmd), '\n')
    return s:prepend_icon(l:files)
  endfunction

  function! s:prepend_icon(candidates)
    let l:result = []
    for l:candidate in a:candidates
      let l:filename = fnamemodify(l:candidate, ':p:t')
      let l:icon = WebDevIconsGetFileTypeSymbol(l:filename, isdirectory(l:filename))
      call add(l:result, printf('%s %s', l:icon, l:candidate))
    endfor

    return l:result
  endfunction

  function! s:edit_file(lines)
    if len(a:lines) < 2 | return | endif

    let l:cmd = get({'enter': 'tabe','ctrl-x': 'split',
          \ 'ctrl-v': 'vertical split',
          \ 'ctrl-t': 'tabe'}, a:lines[0], 'e')

    for l:item in a:lines[1:]
      let l:pos = stridx(l:item, ' ')
      let l:file_path = l:item[pos+1:-1]
      execute 'silent '. l:cmd . ' ' . l:file_path
    endfor
  endfunction

Bat works here, but it doesn't properly pick up my custom theme. Surprisingly, when I use any of the provided themes in that --theme flag, they load fine. For example, if I change it to 1337 I will get the correct response. However, using my custom one, again will just fall back to the default bat theme

image

Here is a copy of my theme file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>author</key>
  <string>Dave Snider</string>
  <key>name</key>
  <string>horizon</string>
  <key>colorSpaceName</key>
  <string>sRGB</string>
  <key>settings</key>
	<array>
		<dict>
			<key>settings</key>
			<dict>
				<key>background</key>
				<string>#1C1E26</string>
				<key>caret</key>
				<string>#6C6F93</string>
				<key>foreground</key>
				<string>#d5d8da</string>
				<key>invisibles</key>
				<string>#6C6F93</string>
				<key>lineHighlight</key>
				<string>#000000</string>
				<key>selection</key>
				<string>#16161C</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Comment</string>
			<key>scope</key>
			<string>comment</string>
			<key>settings</key>
			<dict>
				<key>foreground</key>
				<string>#6C6F93</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>String</string>
			<key>scope</key>
			<string>string</string>
			<key>settings</key>
			<dict>
				<key>foreground</key>
				<string>#fab795</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Number</string>
			<key>scope</key>
			<string>constant.numeric</string>
			<key>settings</key>
			<dict>
				<key>foreground</key>
				<string>#eC6a88</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Built-in constant</string>
			<key>scope</key>
			<string>constant.language</string>
			<key>settings</key>
			<dict>
				<key>foreground</key>
				<string>#f09483</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>User-defined constant</string>
			<key>scope</key>
			<string>constant.character, constant.other</string>
			<key>settings</key>
			<dict>
				<key>foreground</key>
				<string>#f09483</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Variable</string>
			<key>scope</key>
			<string>variable</string>
			<key>settings</key>
			<dict>
				<key>fontStyle</key>
				<string></string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Keyword</string>
			<key>scope</key>
			<string>keyword</string>
			<key>settings</key>
			<dict>
				<key>foreground</key>
				<string>#F09383</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Storage</string>
			<key>scope</key>
			<string>storage</string>
			<key>settings</key>
			<dict>
				<key>fontStyle</key>
				<string></string>
				<key>foreground</key>
				<string>#25b0bc</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Storage type</string>
			<key>scope</key>
			<string>storage.type</string>
			<key>settings</key>
			<dict>
				<key>fontStyle</key>
				<string>italic</string>
				<key>foreground</key>
				<string>#E95678</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Class name</string>
			<key>scope</key>
			<string>entity.name.class</string>
			<key>settings</key>
			<dict>
				<key>fontStyle</key>
				<string>underline</string>
				<key>foreground</key>
				<string>#E95678</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Inherited class</string>
			<key>scope</key>
			<string>entity.other.inherited-class</string>
			<key>settings</key>
			<dict>
				<key>fontStyle</key>
				<string>italic underline</string>
				<key>foreground</key>
				<string>#E95678</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Function name</string>
			<key>scope</key>
			<string>entity.name.function</string>
			<key>settings</key>
			<dict>
				<key>fontStyle</key>
				<string></string>
				<key>foreground</key>
				<string>#E95678</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Function argument</string>
			<key>scope</key>
			<string>variable.parameter</string>
			<key>settings</key>
			<dict>
				<key>fontStyle</key>
				<string>italic</string>
				<key>foreground</key>
				<string>#25B2BC</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Tag name</string>
			<key>scope</key>
			<string>entity.name.tag</string>
			<key>settings</key>
			<dict>
				<key>fontStyle</key>
				<string></string>
				<key>foreground</key>
				<string>#5AF78E</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Tag attribute</string>
			<key>scope</key>
			<string>entity.other.attribute-name</string>
			<key>settings</key>
			<dict>
				<key>fontStyle</key>
				<string></string>
				<key>foreground</key>
				<string>#E95678</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Library function</string>
			<key>scope</key>
			<string>support.function</string>
			<key>settings</key>
			<dict>
				<key>fontStyle</key>
				<string></string>
				<key>foreground</key>
				<string>#25b0bc</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Library constant</string>
			<key>scope</key>
			<string>support.constant</string>
			<key>settings</key>
			<dict>
				<key>fontStyle</key>
				<string></string>
				<key>foreground</key>
				<string>#25b0bc</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Library class&#x2f;type</string>
			<key>scope</key>
			<string>support.type, support.class</string>
			<key>settings</key>
			<dict>
				<key>foreground</key>
				<string>#25b0bc</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Library variable</string>
			<key>scope</key>
			<string>support.other.variable</string>
			<key>settings</key>
			<dict>
				<key>fontStyle</key>
				<string></string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Invalid</string>
			<key>scope</key>
			<string>invalid</string>
			<key>settings</key>
			<dict>
				<key>background</key>
				<string>#25b0bc</string>
				<key>fontStyle</key>
				<string></string>
				<key>foreground</key>
				<string>#eC6a88</string>
			</dict>
		</dict>
		<dict>
			<key>name</key>
			<string>Invalid deprecated</string>
			<key>scope</key>
			<string>invalid.deprecated</string>
			<key>settings</key>
			<dict>
				<key>background</key>
				<string>#25b0bc</string>
				<key>foreground</key>
				<string>#57C7FF</string>
			</dict>
		</dict>
	</array>
  <key>uuid</key>
  <string>uuid</string></dict>
</plist>
@snide snide changed the title Bat not using local theme when passed as a flag Bat not using local theme when passed as a flag inside vim Aug 23, 2019
@rgreenblatt
Copy link

This is actually because neovim's terminal is incorrectly detected as not supporting true color (at least for me). I am working on a solution.

@rgreenblatt
Copy link

Neovim unsets the COLORTERM environment variable which is the only way in which bat detectes true color. As a workaround put something like

if [ -n "${NVIM_LISTEN_ADDRESS+x}" ]; then
  export COLORTERM="truecolor"
fi

in your bashrc/zshrc/etc.

@snide
Copy link
Author

snide commented Aug 23, 2019

Wow. Awesome. That totally worked. I'll leave it to you to close (dunno if you were looking into the core issue) but this works for me.

If there is any interest, I'd be happy to contribute some other themes and languages. Specifically, on the web dev front, scss support through Sass is probably something most people would enjoy on top of the CSS suite bat already supports.

@sharkdp
Copy link
Owner

sharkdp commented Aug 24, 2019

Hi there. Thank you for this fantastic repo. As a UI designer who loves terminal tooling and Vim (all dozen of us!) this project has been really exciting for theming and I'd love to contribute some.

Thank you for the feedback!

[...]

@rgreenblatt

This is actually because neovim's terminal is incorrectly detected as not supporting true color (at least for me). I am working on a solution.

Thank you for looking into this!

Neovim unsets the COLORTERM environment variable which is the only way in which bat detectes true color.

Oh.. why would they do that? Isn't their "terminal emulator" running inside another terminal emulator which does the actual rendering?

As a workaround put something like [..]

Another workaround could be to run COLORTERM=truecolor bat instead of just bat in the fzf --preview command.

@snide

I'll leave it to you to close (dunno if you were looking into the core issue) but this works for me.

I would say that the core issue is not with bat here. One thing that we could do is to provide a command-line option to force 24-bit colors (--color-mode=24-bit), but I'm not sure if that's a good idea.

If there is any interest, I'd be happy to contribute some other themes and languages. Specifically, on the web dev front, scss support through Sass is probably something most people would enjoy on top of the CSS suite bat already supports.

Sounds great. Let's discuss this in another ticket.

@sharkdp sharkdp added help wanted Extra attention is needed themes labels Aug 24, 2019
@sharkdp
Copy link
Owner

sharkdp commented Aug 24, 2019

Bat works here, but it doesn't properly pick up my custom theme. Surprisingly, when I use any of the provided themes in that --theme flag, they load fine. For example, if I change it to 1337 I will get the correct response. However, using my custom one, again will just fall back to the default bat theme

Hm. If the COLORTERM analysis above is correct, bat should actually not fall back to the default color scheme, but instead show a 8-bit approximation of your custom theme. Can you confirm that?

@snide
Copy link
Author

snide commented Aug 24, 2019

8-bit approximation of your custom theme. Can you confirm that?

Confirmed. They were so saturated I thought it was a different theme, but it looks like its working.

@ranelpadon
Copy link

This is my workaround for now to avoid editing the preview.sh and to avoid upgrading to Neovim's nightly builds. I've put it in my init.vim. Basically, inserted the COLORTERM=truecolor envvar in the preview command. This works as intended on my end (Mac + Alacritty + Tmux):

let $FZF_PREVIEW_COMMAND="COLORTERM=truecolor bat --style=numbers --color=always --line-range :500 {}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed themes upstream-error A bug in an upstream component
Projects
None yet
Development

No branches or pull requests

4 participants