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

no color for bat in ssh from a Windows client. #865

Closed
enihcam opened this issue Mar 14, 2020 · 18 comments · Fixed by #934
Closed

no color for bat in ssh from a Windows client. #865

enihcam opened this issue Mar 14, 2020 · 18 comments · Fixed by #934
Assignees
Labels
bug Something isn't working feature-request New feature or request windows Issue is related to the Windows build of bat windows-msys2 Issue is related to running bat inside MSYS2/Cygwin

Comments

@enihcam
Copy link

enihcam commented Mar 14, 2020

I'm using C:\Program Files\Git\usr\bin\ssh.exe to connect to my linux host which has bat installed.

I have export COLORTERM=truecolor, and

  • man with bat pager: color
  • everything in tmux/screen: color
  • bat : no color
@enihcam enihcam added the question Further information is requested label Mar 14, 2020
@eth-p
Copy link
Collaborator

eth-p commented Mar 14, 2020

What's the value of $TERM?
What happens with --color=always?

@enihcam
Copy link
Author

enihcam commented Mar 15, 2020

$ echo $TERM
cygwin

--color=always does not help.

@eth-p
Copy link
Collaborator

eth-p commented Mar 15, 2020

$ echo $TERM
cygwin

I suspect that would be the culprit. Try setting export TERM=xterm-256color or export TERM=xterm. If it still doesn't work, I'll set up an environment and try to reproduce the issue on my own machine.

Additional question for you: do the decorations (grid, line number, etc.) still print, or is bat behaving exactly like cat?

@enihcam
Copy link
Author

enihcam commented Mar 16, 2020

Neither export TERM=xterm-256color nor export TERM=xterm work.
Yes, the decorations still print.

Thanks.

@eth-p
Copy link
Collaborator

eth-p commented Mar 16, 2020

Thanks for checking. Two more things:

  1. Is it any different if you use the built-in Windows 10 SSH command? Can you try it both through your git-bash terminal (MSYS2), and through cmd.exe? I want to figure out if it's an issue with the terminal/ssh, or some other issue.

  2. Could I have a bit more info about your Linux environment? Distro, version, etc. If you want to run the script in diagnostics/info.sh on your server, that would help me figure out how to create a similar environment.

@enihcam
Copy link
Author

enihcam commented Mar 16, 2020

Thanks for quick response @eth-p .

  1. I'm using Windows 10 LTSC (the long term support version of Windows), which has no SSH in built-in program list.
$ ./info.sh
system
------

**$ uname -srm**
Linux 5.5.9-2-ck x86_64

bat
---

**$ bat --version**
bat 0.12.1

**$ env**

bat_config
----------

bat_wrapper
-----------

No wrapper script.

bat_wrapper_function
--------------------

No wrapper function.

tool
----

**$ less --version**
less 551 (PCRE regular expressions)

@eth-p
Copy link
Collaborator

eth-p commented Apr 8, 2020

Sorry about the delay, and thanks for waiting.

After a little bit of experimentation, I found out how to reproduce this with git-bash. It's a very particular setup that requires you to use git-bash with the default Windows console (and not mintty).

image

To try and figure out what was going on, I printed the ANSI escape sequences generated by bat using bat --paging=never sf.sh --color=always | bat -A -pp --color=never:

␛[38;5;238m───────┬────────────────────────────────────────────────────────────────────────␛[0m␊
•••••••␛[38;5;238m│•␛[0mFile:•␛[1msf.sh␛[0m␊
␛[38;5;238m───────┼────────────────────────────────────────────────────────────────────────␛[0m␊
␛[38;5;238m•••1␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;243m#␛[0m␛[38;5;243m!/bin/sh␛[0m␛[38;5;243m␛[0m␊
␛[38;5;238m•••2␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;243m#␛[0m␛[38;5;243m•Copyright•2017•The•Chromium•OS•Authors.•All•rights•reserved.␛[0m␛[38;5;243m␛[0m␊
␛[38;5;238m•••3␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;243m#␛[0m␛[38;5;243m•Use•of•this•source•code•is•governed•by•a•BSD-style•license•that•can•be␛[0m␛[38;5;243m␛[0m␊
␛[38;5;238m•••4␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;243m#␛[0m␛[38;5;243m•found•in•the•LICENSE•file.␛[0m␛[38;5;243m␛[0m␊
␛[38;5;238m•••5␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;243m#␛[0m␛[38;5;243m•Write•an•error•message•and•exit.␛[0m␛[38;5;243m␛[0m␊
␛[38;5;238m•••6␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;243m#␛[0m␛[38;5;243m•Usage:•<message>␛[0m␛[38;5;243m␛[0m␊
␛[38;5;238m•••7␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;149mdie␛[0m␛[38;5;231m(␛[0m␛[38;5;231m)␛[0m␛[38;5;231m•␛[0m␛[38;5;231m{␛[0m␛[38;5;231m␛[0m␊
␛[38;5;238m•••8␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;231m••␛[0m␛[38;5;81mecho␛[0m␛[38;5;231m•␛[0m␛[38;5;231m"␛[0m␛[38;5;186mERROR:•␛[0m␛[38;5;231m$␛[0m␛[38;5;231m*␛[0m␛[38;5;231m"␛[0m␛[38;5;231m␛[0m␊
␛[38;5;238m•••9␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;231m••␛[0m␛[38;5;81mexit␛[0m␛[38;5;231m•1␛[0m␛[38;5;231m␛[0m␊
␛[38;5;238m••10␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;231m}␛[0m␛[38;5;231m␛[0m␊
␛[38;5;238m••11␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;243m#␛[0m␛[38;5;243m•Send•a•DCS•sequence•through•tmux.␛[0m␛[38;5;243m␛[0m␊
␛[38;5;238m••12␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;243m#␛[0m␛[38;5;243m•Usage:•<sequence>␛[0m␛[38;5;243m␛[0m␊
␛[38;5;238m••13␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;149mtmux_dcs␛[0m␛[38;5;231m(␛[0m␛[38;5;231m)␛[0m␛[38;5;231m•␛[0m␛[38;5;231m{␛[0m␛[38;5;231m␛[0m␊
␛[38;5;238m••14␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;231m••␛[0m␛[38;5;81mprintf␛[0m␛[38;5;231m•␛[0m␛[38;5;231m'␛[0m␛[38;5;186m\033Ptmux;\033%s\033\\␛[0m␛[38;5;231m'␛[0m␛[38;5;231m•␛[0m␛[38;5;231m"␛[0m␛[38;5;231m$␛[0m␛[38;5;231m1␛[0m␛[38;5;231m"␛[0m␛[38;5;231m␛[0m␊
␛[38;5;238m••15␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;231m}␛[0m␛[38;5;231m␛[0m␊
␛[38;5;238m••16␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;243m#␛[0m␛[38;5;243m•Send•a•DCS•sequence•through•screen.␛[0m␛[38;5;243m␛[0m␊
␛[38;5;238m••17␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;243m#␛[0m␛[38;5;243m•Usage:•<sequence>␛[0m␛[38;5;243m␛[0m␊
␛[38;5;238m••18␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;149mscreen_dcs␛[0m␛[38;5;231m(␛[0m␛[38;5;231m)␛[0m␛[38;5;231m•␛[0m␛[38;5;231m{␛[0m␛[38;5;231m␛[0m␊
␛[38;5;238m••19␛[0m•••␛[38;5;238m│␛[0m•␛[38;5;231m••␛[0m␛[38;5;81mprintf␛[0m␛[38;5;231m•␛[0m␛[38;5;231m'␛[0m␛[38;5;186m\033P\033%s\033\\␛[0m␛[38;5;231m'␛[0m␛[38;5;231m•␛[0m␛[38;5;231m"␛[0m␛[38;5;231m$␛[0m␛[38;5;231m1␛[0m␛[38;5;231m"␛[0m␛[38;5;231m␛[0m␊

The escape sequence \ESC[38;5;___m is used for selecting a color on a terminal with 256 color support. If I use the native Windows 10 ssh executable, it's perfectly capable of displaying those sequences:

image

Since that displays the colors correctly, that means that it's not the terminal's fault that we end up with broken colors. To try and pinpoint the cause of the issue, I tried running that executable through the git-bash shell.

/c/windows/system32/openssh/ssh.exe [...]

image

And that resulted in the same issue. This suggests that the MSYS2/git-bash/cygwin environment does not support ANSI 256-color escape sequences. To verify that, I ran the git-bash ssh executable (which uses the MSYS2 environment) from cmd:

\Program Files\git\usr\bin\ssh.exe [...]

image

The exact same result. The common denominator here would be any program that uses the MSYS2 environment. I suspect the MSYS2 environment is parsing the escape sequences and using the legacy SetConsoleTextAttribute API (which is only 16 colors) to actually display it. Any colors that don't fall within the standard 16 (\ESC[3_m) are converted to white on grey instead.


Conclusion:
When running under the windows terminal, MSYS2 (the git-bash environment) tries to parse the ANSI color sequences. Since the terminal traditionally only supported 16 colors, MSYS2 helpfully converts everything outside the 16 color range to a white on grey color.

Workaround:
Use the mintty terminal with git-bash instead of the Windows terminal.

Solution:
Get bat to only output 16 color ANSI sequences. I'm not familiar with how to do this, though. Mentioning @sharkdp for feedback/help.

@eth-p eth-p added windows Issue is related to the Windows build of bat windows-msys2 Issue is related to running bat inside MSYS2/Cygwin labels Apr 8, 2020
@enihcam
Copy link
Author

enihcam commented Apr 9, 2020

thanks for the details.
did you try tmux under Windows terminal? no idea why bat works in tmux whatever the terminal type is.

@eth-p
Copy link
Collaborator

eth-p commented Apr 9, 2020

I haven't tried it, but if I have to take a guess, it's because tmux is a terminal multiplexer. A large part of that requires interpreting escape sequences and emulating a real terminal. Maybe it approximates the 256-color sequences as 16-color sequences as part of the process for rendering the pty's display?

Try running this inside tmux:

printf "\x1B[38;5;211mTest\n"

If it's 256 colors, it should show up as a warm pink. If it's being approximated, it might show up as pure magenta.

@enihcam
Copy link
Author

enihcam commented Apr 9, 2020

image

@sharkdp
Copy link
Owner

sharkdp commented Apr 21, 2020

How can we continue here? Is this something that can be fixed in bat?

@eth-p
Copy link
Collaborator

eth-p commented Apr 21, 2020

image

That looks a bit like it was changed to red (\ESC[31m) by tmux.

How can we continue here? Is this something that can be fixed in bat?

The MSYS2 issue isn't really fixable by us, but we could work around the issue by having a simple way to force 16-color output (--color=auto,compatibility or --color=auto,16?) from the config file.

@sharkdp
Copy link
Owner

sharkdp commented Apr 21, 2020

Thanks. That sounds good to me.

I would prefer --color-mode=*auto*/24bit/8bit/off. I have implemented the same option in github.com/sharkdp/pastel

We would then simply need to change this line:

true_color: is_truecolor_terminal(),

@sharkdp sharkdp added feature-request New feature or request good first issue Good for newcomers labels Apr 21, 2020
@eth-p
Copy link
Collaborator

eth-p commented Apr 21, 2020

Thanks. That sounds good to me.

I would prefer --color-mode=*auto*/24bit/8bit/off. I have implemented the same option in github.com/sharkdp/pastel

Sounds good. We would also need to support 4-bit color (ANSI 16 colors) to fix this issue. Should that be called "4bit" or "ansi"?

Does bat currently have a mechanism to convert to 4-bit color, or is it just 256/truecolor right now?

@sharkdp
Copy link
Owner

sharkdp commented Apr 21, 2020

Oh wait. We already have a way to fix this:

--theme=base16

@eth-p
Copy link
Collaborator

eth-p commented Apr 21, 2020

Oh wait. We already have a way to fix this

Sadly, it still emits 256-color escape sequences (\ESC[38;5;___m) when using the base16 theme. We need it to emit the 16-color sequences (\ESC[3_m) to make it compatible with terminals and environments that don't support 256 or truecolor.

echo "echo test" | bat -lbash -p --color=always --theme=base16 | bat --show-all -p
␛[38;5;6mecho␛[0m␛[38;5;7m•test␛[0m␊

sharkdp added a commit that referenced this issue Apr 22, 2020
@sharkdp sharkdp self-assigned this Apr 22, 2020
@sharkdp sharkdp added bug Something isn't working and removed good first issue Good for newcomers question Further information is requested labels Apr 22, 2020
@sharkdp
Copy link
Owner

sharkdp commented Apr 22, 2020

Ok, that actually looks like a bug to me. I have opened #934 to address this.

sharkdp added a commit that referenced this issue Apr 22, 2020
@sharkdp
Copy link
Owner

sharkdp commented May 11, 2020

Please see the updates in v0.15.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working feature-request New feature or request windows Issue is related to the Windows build of bat windows-msys2 Issue is related to running bat inside MSYS2/Cygwin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants