Closed araaha closed 2 months ago
I seem to have the same issue using lf
with kitty
and tmux
.
I describe it as slight blue and/or red tint when previewing with bat
from lf
. Also present when not using tmux
. I also have map i $~/.config/lf/previewer $f
binding and the tint appears there as well.
This tint is not present when using bat
directly, still with kitty
and tmux
. Also, there's no tint on the lf
dir/file list.
My previewer
simply says bat --force-colorization --paging=never --plain "$1" || true
. No combination of bat
options seems to help here.
I investigated a bit more on Windows. There, the color tint was a problem for bat
alone, without lf
in the picture. I noticed that Windows Terminal does not set the $COLORTERM
whereas WezTerm does set it to truecolor
. Sure enough, the problem did not appear on WezTerm. So I worked around it by explicitly setting $COLORTERM
to truecolor
when running on Windows Terminal https://github.com/GrzegorzKozub/pwsh/commit/a76c131e9d63114222a4882259bae4b667e2e435.
Back on Linux, $COLORTERM
is always set to truecolor
for me. That's how I configured tmux
. So it wasn't that.
I noticed the following behavior regarding $TERM
though:
kitty
, without tmux
, the $TERM
is set to xterm-kitty
tmux
, still hosted by kitty
, $TERM
is tmux-256color
In both these cases bat
displays the incorrect, tinted colors when running from lf
.
When I explicitly set $TERM
to xterm-256color
then the issue is fixed and bat
displays the same colors inside lf
as when launched alone.
So in the end, my workaround is:
alias lf='TERM=xterm-256color lf' # fix bat color tint
https://github.com/GrzegorzKozub/dotfiles/commit/39de43a8c5b9be77352a22e095250825e5416586
Specific to lf
. I still think we should fix the issue in lf
.
@GrzegorzKozub Thank you for investigating. It sounds like the tint effect you describe is due to whether bat
is running in truecolor mode or not. You should be able to verify this by piping the output of bat
into a hexdump tool and inspecting the terminal sequences used.
That being said, I don't think it makes sense to make any changes to lf
. AFAIK lf
does not manipulate the TERM
/COLORTERM
environment variables, which means the output of bat
simply depends on the environment it was run in.
For your workaround, maybe it makes more sense to define TERM
when invoking bat
in the previewer script, not when running lf
. It's probably also worth considering raising an issue with bat
to see why it (or its crates) don't work with certain values for TERM
.
You see when we take lf
out of the picture completely then bat
works fine alone. No workarounds needed.
Here's a summary of what fixes the colors when previewing in lf
and what doesn't. Sorry for being verbose but I just want the test results to be understood.
bat
, incorrect colors when using bat
from lf
alias bat='TERM=xterm-256color bat'
in the shell profile - correct colors in bat
, incorrect colors when using bat
from lf
TERM=xterm-256color bat --force-colorization --paging=never --plain "$1"
in previewer
- correct colors in bat
, incorrect colors when using bat
from lf
alias lf='TERM=xterm-256color lf'
in the shell profile - correct colors in bat
, correct colors when using bat
from lf
So clearly something to do with lf
. I have my workaround so I'm not forcing the fix. Up to you guys.
Actually, it's good to be verbose. From the sounds of your description it seems like bat
is not relevant to the issue at all, and we should be trying to eliminate it from the reproduction steps, to make it as minimal as possible.
Are you able to reproduce the tint issue if you:
bat
outside of lf
and save the output into a filecat
the contents of that same fileAnd if so, can you also provide a minimal hexdump of that file. Ideally the reproduction steps can be minimized to just having a single echo
statement (output a colored string) in the previewer script, where changing TERM
gives a different result.
BTW lf
doesn't handle output colors directly - it uses the tcell
TUI library to do so. At this stage my guess is that lf
converts escape sequences to tcell.Style
objects, which is converted back differently when actually writing to the terminal.
Removed all the TERM
workarounds and left it at tmux-256color
as set by tmux
.
Did bat dnd.sh > bat-output.txt
. Yields
bat-output.txt.
Then, used cat
in the previewer
. Yields:
Don't quite follow your request regarding the hexdump, sorry :)
You need to use the same settings for bat
as you do in the previewer script. So:
bat --force-colorization --paging=never --plain dnd.sh > bat-output.txt
Simply bat dnd.sh > bat-output.txt
isn't sufficient because bat
outputs the regular file contents like cat
without any additions when not writing directly to the terminal, see https://github.com/sharkdp/bat?tab=readme-ov-file#file-concatenation
As for the hexdump, don't worry about it, just upload the bat-output.txt
directly as before and I can examine it myself.
Ah, of course. Now we're getting somewhere. The updated bat-output.txt contains color escape sequences.
OK so the format of bat-output.txt
is what I expected. The hexdump (using hexdump -C
) looks like this:
This contains true color escape sequences in the form ESC[38;2;⟨r⟩;⟨g⟩;⟨b⟩m
which should work fine when printing directly to your terminal, since it supports true color.
However when lf
displays this preview, it doesn't pass through these terminal sequences directly to the terminal. It actually converts them to tcell.Style
objects (e.g. tcell.StyleDefault.Foreground(tcell.NewRGBColor(212, 190, 152))
), and then forwards them to the tcell
library to perform the actual printing. I'm guessing that for some reason tcell
fails to detect that your terminal supports true color, and as a result ends up mapping these colors to a 256-color palette, resulting in the tinted effect.
You can probably test this by using cat bat-output.txt
or even printf '\e[38;2;212;190;152mhello world\e[0m\n'
directly in the previewer script, and see if it makes any difference depending on whether you have the TERM
workarounds or not.
One final thing I'm curious about - is this a regression introduced from r31 to r32, or has this issue always been the case? I don't recall any recent changes that might affect this, but we do bump tcell
versions from time to time, which could possibly have triggered this. I don't know if it's too much to ask you to perform a git bisect
to help track down the bad commit.
I tracked it down to commit 0c02239
I've further tracked the problem to commit gdamore/tcell/v2@216e30a
As it turns out there was a pull request gdamore/tcell#719 that fixes the problem
OK, closing this as a duplicate of #1668 then
When using bat to preview a file with syntax highlighting, lf's preview window uses different colors compared to bat. This started happening with r32.