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

Prompt is printed with SGR 40, resulting in black bands on supporting terminals #2516

Closed
1 of 3 tasks
DHowett opened this issue Apr 2, 2021 · 34 comments
Closed
1 of 3 tasks

Comments

@DHowett
Copy link

DHowett commented Apr 2, 2021

Purpose of the issue

  • Bug report (encountered problems/errors)
  • Feature request (request for new functionality)
  • Question

Version Information

This was migrated from microsoft/terminal#9693, and it appears to be valid with the latest code on master.
The user is using cmder's shell configuration outside of ConEmu, so I understand if this is not a valid report.

Description of the issue

When updating the prompt here ...

local cmder_prompt = "\x1b[1;32;40m{cwd} {git}{hg}{svn} \n\x1b[1;39;40m{lamb} \x1b[0m"

... clink specifically requests graphical rendition 40, "ANSI Color 0 Background". This works fairly well for the traditional Windows console and ConEmu, but it is not entirely correct. There is no guarantee that the user's background color is ANSI color 0! In certain terminals (Windows Terminal, gnome-terminal, ...) the user can specify a background color that cannot be represented via SGR, such as that of an image or a transparency effect.

The result is that when the user has transparency enabled, their terminal looks somewhat like this:

image

The ideal solution would be to print using SGR 49, "revert to default background". This has been supported on all versions of Windows where ENABLE_VIRTUAL_TERMINAL_PROCESSING supports colors, so there is no compatibility risk.

We had to fix a similar issue here in PowerShell: PowerShell/PSReadLine#1626

In short, the Windows console is getting better at determining when the user wanted a specific background color and when they wanted the default. I need to do an actual write-up about what's changed rather than writing a bunch of different GitHub comments, so that I can point users and developers at it.

@chrisant996
Copy link
Contributor

What about the green text? Using "revert default background" would cause the green text to be invisible when the background is green. I'm pretty sure that's why an explicit background color is used by default.

Instead of changing the default, maybe it would be better to make it more easily configurable.

@DHowett
Copy link
Author

DHowett commented Apr 2, 2021

That's a fair point, of course. 😄

@DHowett
Copy link
Author

DHowett commented Apr 2, 2021

Thinking on it a bit more, I'm not certain that we should be so defensive of users who set unexpected settings. Consider the default bash prompt in most Debian-derived distributions:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

Or the default root prompt for SLES-12: image

Each of those bears a risk, yes, but they've been doing it for ages and all they do is set the foreground! 😄

@chrisant996
Copy link
Contributor

I use green background in some console Windows.

Why would it be "unexpected" for users to make choices about how they will use colors? Programs are not the only things allowed to choose colors.

I think it's a mistake to label it as officially "unexpected" and therefore "unsupported".

If a program makes it easily configurable, then ok, maybe the program can make assumptions since it lets the user override the configuration. That's why I suggested an effective solution could be to make it configurable. It is not currently configurable in Cmder (without editing Cmder scripts, which will be overwritten by the next update).

@chrisant996
Copy link
Contributor

chrisant996 commented Apr 2, 2021

I don’t really have a stake in what Cmder does. Personally, I prefer to use ConEmu directly and have my own script for setting up my command windows.†

But in the relatively short time I’ve been working on Clink, I’ve seen several requests for Cmder to allow easy configuration of the lambda, and colors, etc. If Cmder makes the color and lambda character configurable, then it would seem very reasonable to use SGR 49, since the configurability would enable a user to change the green text color if they want to use a green background for the whole console.

(Actually I prefer to just use the default console window support in the OS -- I might use Windows Terminal, except that it doesn't seem to let me set the font, and I want to use Consolas NF or Fira Code NF.)

@Lunchb0ne
Copy link
Contributor

I think for now we can add a little note to the wiki page where we detail how to setup the prompt for windows terminal and detail how to fix this and probably add a reference to this issue.

@Hejle
Copy link

Hejle commented Apr 4, 2021

Note that the SGR 40 is also used for Git status (Line 305) and for mercurial status (Line 350). I think that if the promt color is changed to another value than 40 (or made editable), git status and mercurial status should be edited in the same way.

@Lunchb0ne
Copy link
Contributor

Is there a way to check the terminal at startup and set it according to that?

@chrisant996
Copy link
Contributor

Is there a way to check the terminal at startup and set it according to that?

Check for what, and set what?

@richyk1
Copy link

richyk1 commented Apr 5, 2021

I'm not really sure how I could contribute and fix this issue on the main branch because I didn't manage to build the project in Visual Studio. However, if anyone is trying to fix this issue then you can create a file called my_prompt.lua in your root directory -> config folder, and the paste the following:

function my_prompt_filter()
    cwd = clink.get_cwd()
    prompt = "\x1b[1;32;49m{cwd} {git}{hg}{svn} \n\x1b[2;37;49m{lamb} \x1b[0m"
    new_value = string.gsub(prompt, "{cwd}", cwd)
    clink.prompt.value = string.gsub(new_value, "{lamb}", "★")
end

clink.prompt.register_filter(my_prompt_filter, 1)

@Lunchb0ne
Copy link
Contributor

Is there a way to check the terminal at startup and set it according to that?

Check for what, and set what?

The values, so 40 if it's a legacy terminal and 49 if it's a terminal that supports extended/ custom backgrounds.

@chrisant996
Copy link
Contributor

Is there a way to check the terminal at startup and set it according to that?

Check for what, and set what?

The values, so 40 if it's a legacy terminal and 49 if it's a terminal that supports extended/ custom backgrounds.

How does one check what terminal system might be hosting it behind the scenes, and what settings are in effect in that terminal system that could be relevant?

Also, I think the issue is about contrast between "current background color" vs "current foreground color". That's why 40 is used instead of 49.

Since there's not a reliable way to check what terminal is being used or what settings are in effect for it, typically it's left as something for the user to configure. Since if the background is solid green, or a photo, both those are things the user had to configure. And they were given an easy way to configure them. So, it seems reasonable to continue with the existing model, and make Cmder have an easy way to configure the prompt colors instead of hard-coding them to 40 or 49.

@Lunchb0ne
Copy link
Contributor

Is there a way to check the terminal at startup and set it according to that?

Check for what, and set what?

The values, so 40 if it's a legacy terminal and 49 if it's a terminal that supports extended/ custom backgrounds.

How does one check what terminal system might be hosting it behind the scenes, and what settings are in effect in that terminal system that could be relevant?

Also, I think the issue is about contrast between "current background color" vs "current foreground color". That's why 40 is used instead of 49.

Since there's not a reliable way to check what terminal is being used or what settings are in effect for it, typically it's left as something for the user to configure. Since if the background is solid green, or a photo, both those are things the user had to configure. And they were given an easy way to configure them. So, it seems reasonable to continue with the existing model, and make Cmder have an easy way to configure the prompt colors instead of hard-coding them to 40 or 49.

Using something like this:
https://github.com/lptstr/winfetch/blob/2c9590df0900c5392657dc452a25e67d9a6646f6/winfetch.ps1#L533-L573

While it can be useful, it would really slow down the startup so, I think the best way would just be declaring another prompt by the user in a lua, which would give really good customization and control. It can also go another way and allow the config to be written/read from a file.

@chrisant996
Copy link
Contributor

chrisant996 commented Apr 7, 2021

How does one check what terminal system might be hosting it behind the scenes, and what settings are in effect in that terminal system that could be relevant?

Using something like this:
https://github.com/lptstr/winfetch/blob/2c9590df0900c5392657dc452a25e67d9a6646f6/winfetch.ps1#L533-L573

While it can be useful, it would really slow down the startup

In fact, Clink does something similar internally; see clink.getansihost() in Clink's Lua API. It's super fast in native code. But the result isn't fully reliable.

The cited script detects some terminals. It doesn't detect their capabilities or configuration. The script can make some guesses, but it can't achieve a reliable answer.

Here are some of the unreliable points:

  • In terminals that support image or blur backgrounds, the setting is off by default. And other terminals don't support that at all. Some allow different settings per window.
  • Console2/Z does not do any ANSI escape code processing; it lets the OS handle them. So depending on the OS version ANSI escape codes may not be supported, or may be partially supported, or may have the latest and greatest level of support. But even then, Console2/Z are unable to render the colors from xterm-256 and xterm-24bit escape codes, and end up using the closest match out of the basic 16 colors.
  • xterm-256 and xterm-24bit escape codes are supported in the Window ConsoleV2 mode. But there isn't a way to query a specific terminal window whether it is currently using ConsoleV2 mode.
  • I wonder whether the "parent process" approach depends on OS version. E.g. in older Windows versions Process Explorer shows conhost.exe as the parent of cmd.exe. In recent Windows versions it's reversed; Process Explorer shows cmd.exe as the parent of conhost.exe.

I think the best way would just be declaring another prompt by the user in a lua, which would give really good customization and control. It can also go another way and allow the config to be written/read from a file.

I agree. At the same time, it seems to be a common point of user feedback. That makes me wonder if it might save user frustration and might save time offering support help, if Cmder had a simple configurable setting to change the lambda and/or the prompt color.

@daxgames
Copy link
Member

See the PR #2523

@daxgames
Copy link
Member

@DHowett Does this address your issue?

@lfire
Copy link

lfire commented Apr 20, 2021

I'm not really sure how I could contribute and fix this issue on the main branch because I didn't manage to build the project in Visual Studio. However, if anyone is trying to fix this issue then you can create a file called my_prompt.lua in your root directory -> config folder, and the paste the following:

function my_prompt_filter()
    cwd = clink.get_cwd()
    prompt = "\x1b[1;32;49m{cwd} {git}{hg}{svn} \n\x1b[2;37;49m{lamb} \x1b[0m"
    new_value = string.gsub(prompt, "{cwd}", cwd)
    clink.prompt.value = string.gsub(new_value, "{lamb}", "★")
end

clink.prompt.register_filter(my_prompt_filter, 1)

@richyk1 thanks for your solution, but this till have some problem when directory with git infomation.
image

the problem like this, when the git status is not clear, the cmder performace normal, but when the git status is clear, it not normal now.

@daxgames
Copy link
Member

daxgames commented Apr 20, 2021

@lfire try the new build it has configurable prompt with no requirement for a custom prompt filter.

There should be a %cmder_root%\config\cmder_prompt_config.lua file you can edit colors in and restart Cmder. The new build does not change default settings, it just allows the user to configure them to work with alternate terminals.

All colors are configurable including git prompt xolors.

@daxgames
Copy link
Member

@uinguyenhoangtho see my last comment on this.

@daxgames daxgames closed this as completed May 4, 2021
@gustavomassa
Copy link

gustavomassa commented Jul 28, 2021

@lfire try the new build it has configurable prompt with no requirement for a custom prompt filter.

There should be a %cmder_root%\config\cmder_prompt_config.lua file you can edit colors in and restart Cmder. The new build does not change default settings, it just allows the user to configure them to work with alternate terminals.

All colors are configurable including git prompt xolors.

@daxgames

I have updated the CMDER, but it does not come with a "cmder_prompt_config.lua" file, so I created one. Do you have an example of a file? or can you point to some doc/example to fill this file?

There is this file, but it is located inside the vendor folder, it should be inside the config or test folder?
https://github.com/cmderdev/cmder/blob/master/vendor/cmder_prompt_config.lua.default

I have tried putting the file inside vendor and config folder, but it does not affect the settings, what I'm missing?

@daxgames
Copy link
Member

daxgames commented Jul 28, 2021

You need to copy it from the vendor location to the config folder and remove .default extension on the destination.

@gustavomassa
Copy link

@daxgames I did it, removed the .default, and moved to the config folder, I have changed some properties to test, like the symbol (prompt_lambSymbol), but it does not change anything
Version 1.3.18.1106

@daxgames
Copy link
Member

Did you restart the session or start a new tab?

@gustavomassa
Copy link

@daxgames I've tested both, I also killed all the cmder.exe and ConEmu64.exe processes and opened a new cmder, but it does not change the settings

@daxgames
Copy link
Member

I dont think that feature is in the realeased 1.3.18

Try this https://ci.appveyor.com/project/MartiUK/cmder/build/artifacts

@gustavomassa
Copy link

gustavomassa commented Jul 28, 2021

@daxgames you were right! using the version from your link works fine!
image

Could you point me what is the right property to change to avoid the black bands? Changing all 40 to 49?

-- Prompt Element Colors
uah_color = "\x1b[1;33;49m" -- Green = uah = [user]@[hostname]
cwd_color = "\x1b[1;32;49m" -- Yellow cwd = Current Working Directory
lamb_color = "\x1b[1;30;49m" -- Light Grey = Lambda Color
clean_color = "\x1b[1;37;49m"
dirty_color = "\x1b[33;3m"
conflict_color = "\x1b[31;1m"
unknown_color = "\x1b[37;1m" -- White = No VCS Status Branch Color

I can confirm that changing all the 40 to 49 fixes the black bands issues

@daxgames
Copy link
Member

Good to know.

@JoelBirlingmairBeastCode
Copy link

JoelBirlingmairBeastCode commented Jun 23, 2022

I was racking my brain over these black bars. Thanks for the excellent explanation and detailed workaround.

$CMDER_ROOT/config/cmder_prompt_config.lua

change the colors 40m to 49m and it fixes it!

@anburocky3
Copy link

Works very well, but in the symbol, it is kind of dark. i like to have some gray or white kind of color to have an contrast visibility. How to achieve that?

image

@JoelBirlingmairBeastCode

Works very well, but in the symbol, it is kind of dark. i like to have some gray or white kind of color to have an contrast visibility. How to achieve that?

image

you can change this line
image

change line 51 to a different color
so, this line
lamb_color = "\x1b[1;30;49m" -- Light Grey = Lambda Color

change to yellow
lamb_color = "\x1b[1;32;49m" -- Yellow = Lambda Color
image

or white for better visibility on your background
lamb_color = "\x1b[1;37;49m" -- White = Lambda Color
image

@anburocky3
Copy link

Works great. Thanks.

@DRSDavidSoft
Copy link
Contributor

Originally from b0f034a:

the default value of SGR 49 works best with both ConEmu and Windows Terminal, and both values don't take an effect on Cmder.
(ref: https://github.com/cmderdev/cmder/wiki/Seamless-Windows-Terminal-Integration)

The only reason I see to use an explicit black background for the prompt is the point by @chrisant996:

What about the green text? Using "revert default background" would cause the green text to be invisible when the background is green. I'm pretty sure that's why an explicit background color is used by default.

I'd argue that since the default terminal (ConEmu) and Cmd.exe won't be affected and most people would rather to see a transparent one in Windows Terminal, we should use SGR 49 as the default, and let the user to adjust the prompt background to their needs, if somehow they use the same background color (or a color close to the foreground) on their terminal (which will allow a high contrast setting).

DRSDavidSoft referenced this issue Oct 15, 2022
…#2560

the default value of SGR 49 works best with both ConEmu and Windows Terminal, and both values don't take an effect on Cmder.
(ref: https://github.com/cmderdev/cmder/wiki/Seamless-Windows-Terminal-Integration)
@DRSDavidSoft DRSDavidSoft mentioned this issue Oct 15, 2022
@chrisant996
Copy link
Contributor

@DRSDavidSoft to me it seems reasonable to default to SGR 49, and have doc/wiki text saying how to configure the prompt to work with non-black backgrounds. I don't know how broadly that viewpoint is shared, so I don't feel comfortable making a suggestion in either direction.

@DRSDavidSoft
Copy link
Contributor

@chrisant996 That's a good idea, and thank you for pointing it out. I've already described using a black color in the wiki:
https://github.com/cmderdev/cmder/wiki/Customization#prompt-color

P.S. Hopefully, since Cmder is used by people with all kinds of color, shell, and terminal preferences, we could collect all customization and configuration tips in one place. Personally, I love Cmder since it's a precisely pre-configured package with best configurations collected over time by the community adding their bits and parts to the project. I'd also appreciate it if you could share your suggestions, especially in the personal preferences section 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

12 participants