aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/terminal.cc
diff options
context:
space:
mode:
authorJade Lovelace <lix@jade.fyi>2024-08-01 12:26:16 -0700
committerJade Lovelace <lix@jade.fyi>2024-08-04 20:41:19 -0700
commit378ec5fb0611e314511a7afc806664205846fc2e (patch)
treedab3a3cb9e7f91dc77956fb99cf4cac711c760d0 /src/libutil/terminal.cc
parent700762d8b24350bbe537258167244ec9c84fcae3 (diff)
Implement forcing CLI colour on, and document it better
This is necessary to make some old tests work when testing colour against non-interactive outputs. Change-Id: Id89f8a1f45c587fede35a69db85f7a52f2c0a981
Diffstat (limited to 'src/libutil/terminal.cc')
-rw-r--r--src/libutil/terminal.cc18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/libutil/terminal.cc b/src/libutil/terminal.cc
index b58331d04..68d358dc5 100644
--- a/src/libutil/terminal.cc
+++ b/src/libutil/terminal.cc
@@ -9,9 +9,21 @@ namespace nix {
bool shouldANSI()
{
- return isatty(STDERR_FILENO)
- && getEnv("TERM").value_or("dumb") != "dumb"
- && !(getEnv("NO_COLOR").has_value() || getEnv("NOCOLOR").has_value());
+ // 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.
+ // NO_COLOR CLICOLOR CLICOLOR_FORCE Colours?
+ // set x x No
+ // unset x set Yes
+ // unset x unset If attached to a terminal
+ // [we choose the "modern" approach of colour-by-default]
+ auto compute = []() -> 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";
+ return !mustNotColour && (shouldForce || isTerminal);
+ };
+ static bool cached = compute();
+ return cached;
}
std::string filterANSIEscapes(std::string_view s, bool filterAll, unsigned int width)