-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
fix(libutil/terminal): require both stderr/stdout to be tty to output color #11994
base: master
Are you sure you want to change the base?
fix(libutil/terminal): require both stderr/stdout to be tty to output color #11994
Conversation
… color This is a bit more conservative and safe for cli composability. A common use-case for nix cli is to filter/transform output with grep/ripgrep/jq from stdout. Those tools usually have their own colored output. It's best not to step on their toes. Once example of such command is: `nix registry list | rg nixpkgs` Note that this is more of a band-aid solution. Nix generally handles ANSI colors in an ad-hoc way and there might be some other unexpected bugs related to control characters.
My linux-jitsu is too weak to implement regression tests for this. Is there some way to redirect output to a file but pretending that's a tty? Not sure if using |
@Ericson2314, I'm not very familiar with portability for windows. Are we okay with generally using POSIX |
You can use the
|
@@ -18,6 +18,7 @@ bool isTTY() | |||
{ | |||
static const bool tty = | |||
isatty(STDERR_FILENO) | |||
&& isatty(STDOUT_FILENO) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only caveat I see just now is that we have tooling like cachix
(maybe also attic?), will no have less fancy builds logs:
nix-build | cachix push
Also was nix-build
output maybe not relying on this.
Do the said commands actually profit from colors much? |
I'm not sure about this. It's pretty reasonable to do something like |
Linux has atomic write behavior for single syscalls up to 4k or page size or something, so if both commands use a single write syscall per line, then the result would be mostly not scrambled, especially if Nix keeps an empty line above the progress bar, and puts the cursor there after its refresh (otherwise the progress bar sort of becomes a background). I'm not looking forward to bug reports like " |
So the question is, how can we make |
Our stdout output doesn't contain color AFAIK. For example, the submitter's example ( I think the real issue is that the progress bar doesn't play nice with other commands when piped into them because it does raw terminal I/O (like moving the cursor). |
Than is this issue invalid? #11583 I was also trying to reproduce this issue locally but couldn't find any colors in the output. I haven't looked for non-visible ansi characters. |
Doesn't have to be "raw". Basically everything can be ANSI bytes, and those don't have to be chunked in any particular way.
Probably valid. The progress bar is happy to appear, disappear, and leave the cursor at the start of the line. Value printing in the repl is supposed to be streaming, but apparently also "permits" progress bar updates or something while it's doing that. |
That's because it needs to call |
Motivation
Fixes #11583
Fixes #4848
This is a bit more conservative and safe for cli composability. A common use-case for nix cli is to filter/transform output with grep/ripgrep/jq from stdout. Those tools usually have their own colored output. It's best not to step on their toes.
Once example of such command is:
nix registry list | rg nixpkgs
Note that this is more of a band-aid solution. Nix generally handles ANSI colors in an ad-hoc way and there might be some other unexpected bugs related to control characters.
Context
Add 👍 to pull requests you find important.
The Nix maintainer team uses a GitHub project board to schedule and track reviews.