cli: eat terminal codes from stdout also

This *should* be sound, plus or minus the amount that the terminal code
eating code is messed up already.

This is useful for testing CLI output because it will strip the escapes
enough to just shove the expected output in a file.

Change-Id: I8a9b58fafb918466ac76e9ab585fc32fb9294819
This commit is contained in:
Jade Lovelace 2024-07-31 22:06:18 -07:00
parent 378ec5fb06
commit 5f0ef50077
5 changed files with 34 additions and 4 deletions

View file

@ -0,0 +1,26 @@
---
synopsis: "Better usage of colour control environment variables"
cls: [1699, 1702]
credits: [jade]
category: Improvements
---
Lix now heeds `NO_COLOR`/`NOCOLOR` for more output types, such as that used in `nix search`, `nix flake metadata` and similar.
It also now supports `CLICOLOR_FORCE`/`FORCE_COLOR` to force colours regardless of whether there is a terminal on the other side.
It now follows rules compatible with those described on <https://bixense.com/clicolors/> with `CLICOLOR` defaulted to enabled.
That is to say, the following procedure is followed in order:
- NO_COLOR or NOCOLOR set
Always disable colour
- CLICOLOR_FORCE or FORCE_COLOR set
Enable colour
- The output is a tty; TERM != "dumb"
Enable colour
- Otherwise
Disable colour

View file

@ -37,7 +37,7 @@ void Logger::warn(const std::string & msg)
void Logger::writeToStdout(std::string_view s)
{
writeFull(STDOUT_FILENO, s);
writeFull(STDOUT_FILENO, filterANSIEscapes(s, !shouldANSI(), std::numeric_limits<unsigned int>::max(), false));
writeFull(STDOUT_FILENO, "\n");
}

View file

@ -26,7 +26,8 @@ bool shouldANSI()
return cached;
}
std::string filterANSIEscapes(std::string_view s, bool filterAll, unsigned int width)
// FIXME(jade): replace with TerminalCodeEater. wowie this is evil code.
std::string filterANSIEscapes(std::string_view s, bool filterAll, unsigned int width, bool eatTabs)
{
std::string t, e;
size_t w = 0;
@ -55,7 +56,7 @@ std::string filterANSIEscapes(std::string_view s, bool filterAll, unsigned int w
t += e;
}
else if (*i == '\t') {
else if (*i == '\t' && eatTabs) {
i++; t += ' '; w++;
while (w < (size_t) width && w % 8) {
t += ' '; w++;

View file

@ -30,7 +30,8 @@ bool shouldANSI();
*/
std::string filterANSIEscapes(std::string_view s,
bool filterAll = false,
unsigned int width = std::numeric_limits<unsigned int>::max());
unsigned int width = std::numeric_limits<unsigned int>::max(),
bool eatTabs = true);
/**
* Recalculate the window size, updating a global variable. Used in the

View file

@ -29,6 +29,8 @@ nix search -f search.nix '' ^ | grepQuiet hello
## Tests for multiple regex/match highlighting
# FIXME: possibly not test this with colour in the future
export CLICOLOR_FORCE=1
e=$'\x1b' # grep doesn't support \e, \033 or even \x1b
# Multiple overlapping regexes
(( $(nix search -f search.nix '' 'oo' 'foo' 'oo' | grep -c "$e\[32;1mfoo$e\\[0;1m") == 1 ))