nix icon indicating copy to clipboard operation
nix copied to clipboard

Make `nix log` respect NO_COLOR=1

Open knedlsepp opened this issue 3 years ago • 7 comments

Some parts of nix already respect NO_COLOR=1. It seems however this functionality is desired, yet missing from nix log. See: https://discourse.nixos.org/t/disabling-colors-for-nix-log-output/17480

knedlsepp avatar Aug 16 '22 20:08 knedlsepp

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/disabling-colors-for-nix-log-output/17480/5

nixos-discourse avatar Aug 16 '22 20:08 nixos-discourse

I wonder if there's a way to make the logger do this instead of having to litter around shouldANSI checks... I haven't looked at the Nix code in a while, but I imagine that would be better than the current situation.

cole-h avatar Aug 17 '22 19:08 cole-h

I wonder if there's a way to make the logger do this instead of having to litter around shouldANSI checks... I haven't looked at the Nix code in a while, but I imagine that would be better than the current situation.

How about wrapping std::cout in nix::cout?

knedlsepp avatar Aug 17 '22 20:08 knedlsepp

I wonder if there's a way to make the logger do this instead of having to litter around shouldANSI checks... I haven't looked at the Nix code in a while, but I imagine that would be better than the current situation.

How about wrapping std::cout in nix::cout?

Hm. There is also logger->cout.

knedlsepp avatar Aug 17 '22 21:08 knedlsepp

Hm. There is also logger->cout.

Right, probably the cleanest solution is to make logger->cout strip the ANSI colors if needed.

edolstra avatar Aug 22 '22 12:08 edolstra

I suggest to add a --colour=<never|auto|always> option, that defaults to auto which is the status quo. With never, shouldANSI must always return false, while with always, it must always return true ignoring NO_COLOR and whether the file descriptor is an actual terminal. There should be an alias --color for this option (or the other way around --colour aliasing to --color). Perhaps an --colour/--color without an argument could be equal to --colour=always, but this might complicate argument parsing. The motivation behind --colour=always is to pipe the output e.q. to a pager like more or less. At the moment, because isatty returns false in this case, shouldANSI evaluates to false and any ANSI-colour-sequences are stripped.

sivizius avatar Jan 19 '23 11:01 sivizius

This would be really nice. For example, it's convenient to pipe nix log output to gh gist create, but as is (even with NO_COLOR=1) this creates a bunch of escape sequence mess.

Seems that one can pipe to a sed -e $'s/\x1b\[[0-9;]*m//g' intermediary for now (source).

n8henrie avatar May 29 '24 15:05 n8henrie

Any chance this gets some attention?

DavSanchez avatar Oct 30 '24 23:10 DavSanchez

This seems like a really small PR for a decent QoL win, particularly as we need to post logs for build errors that are currently littered with ansi escape codes unless people do manual workarounds.

n8henrie avatar Dec 02 '24 14:12 n8henrie

This seems like a really small PR for a decent QoL win, particularly as we need to post logs for build errors that are currently littered with ansi escape codes unless people do manual workarounds.

@n8henrie I'm sorry I currently don't have the time to work in the suggested changes ( https://github.com/NixOS/nix/pull/6911#issuecomment-1222278160 ). But I agree that it might not be much work to do this properly. Maybe somebody else can give it a shot. :) I'll close this for now.

knedlsepp avatar Dec 02 '24 14:12 knedlsepp

@knedlsepp I stumbled upon this issue myself today. Is there an issue open to keep track of the issue properly?

drupol avatar Feb 26 '25 20:02 drupol

@knedlsepp I stumbled upon this issue myself today. Is there an issue open to keep track of the issue properly?

Looks like https://github.com/NixOS/nix/issues/5214 would do.

knedlsepp avatar Feb 26 '25 20:02 knedlsepp