-
Notifications
You must be signed in to change notification settings - Fork 7.1k
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
TERM and NO_COLOR envirnoment vars still not respected #21160
Comments
NO_COLOR does get recognized and affects $PSStyle.OutputRending
According to about ANSI terminals
So I question if the error channel is included in that handling. Regarding the TERM, it does not make sense to send ANSI colour escape sequences to terminals that don't understand them. (eg should refer to terminfo and not populate all the PSStyle colours if the terminal cannot deal with colour) |
The rationale is simple: if the colors are enforced by default, they will make the output for some users completely unreadable. There should be a way for users set a variable to prevent colors from being used: At the moment not only the error output is always colored, the same happens for the PS prompt input: on a gray background the input is yellow or light green both of which are unreadable. NO_COLOR is not recognized and not respected for errors and for prompts. |
Agreed! |
You can set monochrome pwsh: [console]::Write("`e]12;#ffffff`a") # set white cursor
$PSStyle.OutputRendering = "PlainText"
Set-PSReadlineOption -Colors @{
Command = "`e[37m"
Comment = "`e[37m"
ContinuationPrompt = "`e[37m"
Default = "`e[37m"
Emphasis = "`e[37m"
Error = "`e[37m"
InlinePrediction = "`e[37m"
Keyword = "`e[37m"
ListPrediction = "`e[37m"
ListPredictionSelected = "`e[37m"
ListPredictionTooltip = "`e[37m"
Member = "`e[37m"
Number = "`e[37m"
Operator = "`e[37m"
Parameter = "`e[37m"
Selection = "`e[7m"
String = "`e[37m"
Type = "`e[37m"
Variable = "`e[37m"
} |
Without seeing the contents of your "mono.ps1" I don't know what you are trying to demonstrate. Wanting to use monochrome and NO_COLORs does not mean setting the colours to black and white. It means not using any colour escape codes in any output. |
NO_COLOR also set colors with escape sequences, just reduce base 16 console colors to 2 |
escape 37 m sets the foreground colour to white, it does not mean 'no colour'. So if the background is white or yellow it becomes hard to read. |
No! This is 8th color of terminal color palette. |
From no-color.org
The goal is not have any ANSI colour code escape sequences in the output. |
How when terminal does understand what color it has to output? Any symbol, any color and any hotkey are esc-sequences |
A true monochrome terminal does not have that problem, there are no colours to choose from, It may be white on black, green on black or amber on black, some monochrome terminals allow you to have reversed characters. In the same manner it is completely legitimate to have black on white. The application does not deal with any colours at all, it just prints text and it is displayed how the terminal sees fit. Look ma, no colours |
What does it mean "true"? Kernel console 80x25? |
VT102, VT52, 3270, 5250 were all monochrome terminals. The original xterm was monochrome as not everyone could afford colour. Lisa and the first Macintosh were monochrome, they did not get colour until Macintosh II and Color QuickDraw |
These are hardware terminals, let's get closer to life.
I do not remember "original" xterm. But I do remember that xterm configured with ~/.Xresources |
Modern Macintosh Terminal Console offers many themes, including black on yellow. But PowerShell uses Yellow on Yellow, It also offers black and white. PowerShell gives you yellow on white. The terminals also work on according to the light/dark mode and will change colours during the day. So more than 50% of the time the PowerShell colour configuration is wrong, if you reconfigure it, then it will be wrong again when the dark/light mode changes. It will be wrong if you have multiple terminals open using different themes. So the solution is we want, when asked, is for PowerShell to not do any colours at all. |
Just test two commands, there is difference in order of colors:
The .Net... |
For a programmer, the correct answer is to see how it's implemented elsewhere. And example of using https://github.com/termcolor/termcolor where demo-termcolor.py is:
|
Python 3.11.6:
|
On windows:
But I already gave the picture of the output. NO_COLOR results in no ANSI color sequences at all, showing that they aren't supposed to be present if NO_COLOR is set. There's also a huge list of other libraries already implementing NO_COLOR on https://no-color.org/ pwsh is there too but this entry is to show that what's implemented is still insufficient. |
Start pwsh with $env:NO_COLOR=1; pwsh -NoProfile And unload PSReadline Module: Remove-Module PSReadline |
I do not know what to say. I tried in conhost, WT and alacritty and results are the same. Win 11 Home |
I haven't seen in your outputs that you ever see red errors? What's the step when the red errors stop being red in your case, and how does it look before that step? I think PSReadLine is completely irrelevant for that. |
In your example the NO_COLOR is ignored without the "-nop", which I don't think is how it's supposed to behave. In my case I also use your -nop to no effect. I'm not doing any msi, I'm simply extracting https://github.com/PowerShell/PowerShell/releases/download/v7.4.1/PowerShell-7.4.1-win-x64.zip to C:\tmp\pwsh741 and running my tests in that directory. |
I do the same on Linux and there is not red error. What terminal emulator are you using? And I set in registry:
|
I'm opening a %windir%\system32\cmd.exe and I haven't created any profile.ps1 myself and the only ones I see are something like in C:\Windows\System32\WindowsPowerShell\v1.0\Examples\profile.ps1 and these have only comments inside and should not be seen by the pwsh at all. And as you see adding -nop changes nothing. I also don't have VirtualTerminalLevel anywhere in registry. Can you try to remove your entry and see what happens? |
This behavior is strange. But if I entirely set mono for terminal then Write-Host is also monochrome: # mono.ps1
param ( [Parameter()] [string] $color = "black" )
if ($color -eq "white") { $bg, $fg = "#ccc", "#000" }
else { $bg, $fg = "#111", "#ddd" }
[console]::Write("`e]12;$fg`a")
$PSStyle.OutputRendering = "PlainText"
Set-PSReadlineOption -Colors @{
Command = "`e[37m"
Comment = "`e[37m"
ContinuationPrompt = "`e[37m"
Default = "`e[37m"
Emphasis = "`e[37m"
Error = "`e[37m"
InlinePrediction = "`e[37m"
Keyword = "`e[37m"
ListPrediction = "`e[37m"
ListPredictionSelected = "`e[37m"
ListPredictionTooltip = "`e[37m"
Member = "`e[37m"
Number = "`e[37m"
Operator = "`e[37m"
Parameter = "`e[37m"
Selection = "`e[7m"
String = "`e[37m"
Type = "`e[37m"
Variable = "`e[37m"
}
$col = (1..15 | Foreach-Object { '{0};{1}' -f $_, $fg }) -join ';'
[console]::Write("`e]4;0;$bg;$col`a") Works in conhost and it can work in terminals without living config reloading:
|
@237dmitry, the |
Rather than "stripping ansi", I suggest code should not be writing ANSI colour escape sequences to the output in the first place when NO_COLOR is present or OutputRendering = PlainText. |
Yes. What does it do to reset color palette to two colors (background and foreground)? Monochrome is two colors 37m and 40m and their values are taken from the terminal settings, where the 1st is background and the 8tn is foreground. |
If the terminal is monochrome terminal and does not handle ANSI colour escape sequences then it will just print out the text the best it can, so your output would be littered with the literals "37m" and "40m" in the content. |
@rhubarb-geek-nz, regarding
Here's a Unix example; run from # "green" prints in green, because the external printf utility prints directly to the terminal
# (and itself doesn't honor NO_COLOR)
NO_COLOR=1 pwsh -nop -c "printf 'It ain''t easy being \033[32mgreen\033[m.\n'"
# "green" does NOT print in green, because the use of (...) means that PowerShell captures and relays
# the output, and performs active VT-sequence stripping due to NO_COLOR -> $PSStyle.OutputRendering = 'PlainText'
NO_COLOR=1 pwsh -nop -c "(printf 'It ain''t easy being \033[32mgreen\033[m.\n')" |
@mklement0, thanks for mentioning the Win32 console virtual terminal support flags, in many ways the equivalent of this in the UNIX world is the |
This comment was marked as resolved.
This comment was marked as resolved.
If you explicitly print any escape sequence then that is what should be output because that is what you said to do. With NO_COLOR there should be no need for any filtering, it is up to the code to abide by the request to not output any colour. |
My bad, @237dmitry - I forgot to add the # From Bash
NO_COLOR=1 pwsh -nop -c "(printf 'It ain''t easy being \033[32mgreen\033[m.\n')" Now the word "green" is NOT colored.
I don't disagree, but - unlike the buggy, inconsistent That said, you could argue that outside callers, specifically, then see actively modified data is a buggy (possibly accidental implementation) aspect of this design. In other words: it may be worth opening a separate issue to question the CLI behavior when stripping is in effect. |
Try to see rgb value of background (for example):
All colors are escape sequences. Why do they not handle? I think they write directly into the host. |
All colours are escape sequences, and monochrome terminals do not deal with colours. |
Where does your yellow-black terminal get the colors? |
I don't know and I don't care because that is internal to the terminal implementation. What I want is my terminals to work with their existing themes. Years ago I wrote a nice simple terminal, WishStream, this was Windows Mobile/Windows 8 Store app. The foreground and background colours were defined by the user through the XAML Theme, if they wanted it dark it would be light on dark, if they wanted light, they got dark on light. As it was a monochrome terminal it implemented the VT100 escape sequences, which do not include colour. So this terminal ignored all colour escape sequences because they were meaningless, and as it set TERM=vt100, no application should have been sending any colour escape sequences. So vi etc all worked happily because they abided by the terminfo regarding what a vt100 terminal could do. |
Сonfigure pwsh and terminal yourself. This is the easiest way in the current situation. Or wait for the PSReadline developers to listen to our wishes. |
The other option is to not use PowerShell. It is a silly situation where, out of the box, with no configuration, PowerShell is unusable on a Mac with the default terminal theme of black on white because it gives you yellow on white. I have to open a new session explicitly different to use PowerShell. |
I could not launch bash in monochrome mode, all of |
On Ubuntu, it has this in .bashrc set a fancy prompt (non-color, unless we know we "want" color)case "$TERM" in So it should not use colour in the prompt unless the terminal matches the case statement. If I set TERM=dtterm I don't get a colour prompt. Unfortunately the aliases play by a completely different set of rules
I find standard Linux directory colours hard to read when directories are blue on black. |
If I launched bash:
It still started with full set of configurations from |
I would suggest that authors need to think REALLY hard about use of any kind of colour, and a lot harder than any actually do. If a script that doesn't check the users preference uses
There is always the scope to create terminal themes which become nonsensical . Terminal themes are a set of "slots" which represent colours by convention, and we can set exactly what R,G,B values are used for "Red-on-this-terminal" VS Code's default red and / Windows Terminal default Red are not automatically the same or something simple like 255,0,0. Windows PowerShell re-defined Dark Magenta in the default terminal settings for PowerShell.exe so that it isn't any kind of magenta but a dark blue and then set the background not to black but dark magenta. Absolutely any assumption that an application theme makes about the terminal has in each slot can be wrong footed: I think there is an argument that at start-up PSreadline should look at No_color and if found set all it's colours to the default foreground colour. That's a different branch from this though. |
@SteveL-MSFT, I don't think there's anything actionable here, as I've tried to express in my previous summary:
|
We can workaround bug 49780 by disabling colour outputs entirely, and removing the PSReadLine module. See: PowerShell/PSReadLine#3918 and PowerShell/PowerShell#21160 for context.
Landed here while searching the same issue. My use case is that I'm starting pwsh with redirected stdin, stdout, and stderr. With Maybe I'm wrong to assume this, but it seems like a logical conclusion based on the docs here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_ansi_terminals?view=powershell-7.4#disabling-ansi-output Here's a quick repro: using System.Diagnostics;
var psi = new ProcessStartInfo
{
FileName = "pwsh.exe",
RedirectStandardError = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
};
psi.Environment.Add("NO_COLOR", "1");
var process = Process.Start(psi)!;
process.OutputDataReceived += (sender, e) =>
{
Console.WriteLine(e.Data);
};
process.ErrorDataReceived += (sender, e) =>
{
Console.WriteLine(e.Data);
};
process.BeginOutputReadLine();
process.BeginErrorReadLine();
await process.StandardInput.WriteLineAsync("$env:NO_COLOR;$psversiontable;get-it;exit;");
await process.WaitForExitAsync();
|
@bitbound: The problem seems to somehow be related to providing commands via stdin and also In your repro code, if you comment out By contrast, from the command line providing commands via stdin does seem to honor # OK - monochrome error message.
$env:NO_COLOR=1; 'get-it' | pwsh -NoProfile |
Prerequisites
Steps to reproduce
As per "Enable use of TERM and NO_COLOR env var plaintext #14969" the TERM and NO_COLOR variables should disable coloring of errors in pwsh. But executing the showbug.bat in command.exe in the root directory of pwsh version 7 4 1 with this content:
Shows:
Here, the error message, triggered intentionally to demonstrate the bug, is RED colored.
Expected behavior
The error message should be printed without the colors.
Actual behavior
The error is RED colored, in spite of the environment variables requesting no colors and being clearly readable by the pwsh.exe.
Error details
Environment data
Visuals
The text was updated successfully, but these errors were encountered: