mirror of
https://github.com/NixOS/nix
synced 2025-06-27 00:11:17 +02:00
Add some color
This commit is contained in:
parent
392430b2c4
commit
373fad75e1
6 changed files with 56 additions and 52 deletions
|
@ -453,6 +453,7 @@ void printMsg_(Verbosity level, const FormatOrString & fs)
|
|||
else if (logType == ltEscapes && level != lvlInfo)
|
||||
prefix = "\033[" + escVerbosity(level) + "s";
|
||||
string s = (format("%1%%2%\n") % prefix % fs.s).str();
|
||||
if (!isatty(STDERR_FILENO)) s = filterANSIEscapes(s);
|
||||
writeToStderr(s);
|
||||
}
|
||||
|
||||
|
@ -1106,4 +1107,38 @@ void ignoreException()
|
|||
}
|
||||
|
||||
|
||||
string filterANSIEscapes(const string & s, bool nixOnly)
|
||||
{
|
||||
string t, r;
|
||||
enum { stTop, stEscape, stCSI } state = stTop;
|
||||
for (auto c : s) {
|
||||
if (state == stTop) {
|
||||
if (c == '\e') {
|
||||
state = stEscape;
|
||||
r = c;
|
||||
} else
|
||||
t += c;
|
||||
} else if (state == stEscape) {
|
||||
r += c;
|
||||
if (c == '[')
|
||||
state = stCSI;
|
||||
else {
|
||||
t += r;
|
||||
state = stTop;
|
||||
}
|
||||
} else {
|
||||
r += c;
|
||||
if (c >= 0x40 && c != 0x7e) {
|
||||
if (nixOnly && (c != 'p' && c != 'q' && c != 's' && c != 'a' && c != 'b'))
|
||||
t += r;
|
||||
state = stTop;
|
||||
r.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
t += r;
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue