From 292567e0b0a4681eb8ca803c26293d70857fe387 Mon Sep 17 00:00:00 2001 From: Jade Lovelace Date: Sat, 10 Aug 2024 16:02:42 -0700 Subject: fix: check if it is a Real terminal, not just if it is a terminal This will stop printing stuff to dumb terminals that they don't support. I've overall audited usage of isatty and replaced the ones with intent to mean "is a Real terminal" with checking for that. I've also caught a case of carelessly assuming "is a tty" means "should be colour" in nix-env. Change-Id: I6d83725d9a2d932ac94ff2294f92c0a1100d23c9 --- src/libutil/terminal.cc | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src/libutil/terminal.cc') diff --git a/src/libutil/terminal.cc b/src/libutil/terminal.cc index 2ba1cb81b..25e97e599 100644 --- a/src/libutil/terminal.cc +++ b/src/libutil/terminal.cc @@ -7,7 +7,12 @@ namespace nix { -bool shouldANSI() +bool isOutputARealTerminal(StandardOutputStream fileno) +{ + return isatty(int(fileno)) && getEnv("TERM").value_or("dumb") != "dumb"; +} + +bool shouldANSI(StandardOutputStream fileno) { // Implements the behaviour described by https://bixense.com/clicolors/ // As well as https://force-color.org/ for compatibility, since it fits in the same shape. @@ -16,14 +21,14 @@ bool shouldANSI() // unset x set Yes // unset x unset If attached to a terminal // [we choose the "modern" approach of colour-by-default] - auto compute = []() -> bool { + auto compute = [](StandardOutputStream fileno) -> bool { bool mustNotColour = getEnv("NO_COLOR").has_value() || getEnv("NOCOLOR").has_value(); bool shouldForce = getEnv("CLICOLOR_FORCE").has_value() || getEnv("FORCE_COLOR").has_value(); - bool isTerminal = isatty(STDERR_FILENO) && getEnv("TERM").value_or("dumb") != "dumb"; + bool isTerminal = isOutputARealTerminal(fileno); return !mustNotColour && (shouldForce || isTerminal); }; - static bool cached = compute(); - return cached; + static bool cached[2] = {compute(StandardOutputStream::Stdout), compute(StandardOutputStream::Stderr)}; + return cached[int(fileno) - 1]; } // FIXME(jade): replace with TerminalCodeEater. wowie this is evil code. -- cgit v1.2.3