[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
144/376: Add some color
From: |
Ludovic Courtès |
Subject: |
144/376: Add some color |
Date: |
Wed, 28 Jan 2015 22:04:37 +0000 |
civodul pushed a commit to tag 1.8
in repository guix.
commit 373fad75e19a2f24db512621b8cedb627d03d49d
Author: Eelco Dolstra <address@hidden>
Date: Wed Aug 20 16:01:16 2014 +0200
Add some color
---
src/libexpr/nixexpr.cc | 2 +-
src/libmain/shared.cc | 11 +++++------
src/libstore/build.cc | 38 +-------------------------------------
src/libutil/util.cc | 35 +++++++++++++++++++++++++++++++++++
src/libutil/util.hh | 12 ++++++++++++
src/nix-env/nix-env.cc | 10 ++--------
6 files changed, 56 insertions(+), 52 deletions(-)
diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc
index b916a26..bb72c00 100644
--- a/src/libexpr/nixexpr.cc
+++ b/src/libexpr/nixexpr.cc
@@ -143,7 +143,7 @@ std::ostream & operator << (std::ostream & str, const Pos &
pos)
if (!pos)
str << "undefined position";
else
- str << (format("%1%:%2%:%3%") % pos.file % pos.line %
pos.column).str();
+ str << (format(ANSI_BOLD "%1%" ANSI_NORMAL ":%2%:%3%") % pos.file %
pos.line % pos.column).str();
return str;
}
diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc
index ba75847..7308752 100644
--- a/src/libmain/shared.cc
+++ b/src/libmain/shared.cc
@@ -247,6 +247,7 @@ void showManPage(const string & name)
int handleExceptions(const string & programName, std::function<void()> fun)
{
+ string error = ANSI_RED "error:" ANSI_NORMAL " ";
try {
try {
fun();
@@ -263,21 +264,19 @@ int handleExceptions(const string & programName,
std::function<void()> fun)
return e.status;
} catch (UsageError & e) {
printMsg(lvlError,
- format(
- "error: %1%\n"
- "Try `%2% --help' for more information.")
+ format(error + " %1%\nTry `%2% --help' for more information.")
% e.what() % programName);
return 1;
} catch (BaseError & e) {
- printMsg(lvlError, format("error: %1%%2%") % (settings.showTrace ?
e.prefix() : "") % e.msg());
+ printMsg(lvlError, format(error + "%1%%2%") % (settings.showTrace ?
e.prefix() : "") % e.msg());
if (e.prefix() != "" && !settings.showTrace)
printMsg(lvlError, "(use `--show-trace' to show detailed location
information)");
return e.status;
} catch (std::bad_alloc & e) {
- printMsg(lvlError, "error: out of memory");
+ printMsg(lvlError, error + "out of memory");
return 1;
} catch (std::exception & e) {
- printMsg(lvlError, format("error: %1%") % e.what());
+ printMsg(lvlError, error + e.what());
return 1;
}
diff --git a/src/libstore/build.cc b/src/libstore/build.cc
index ef0a304..d93b560 100644
--- a/src/libstore/build.cc
+++ b/src/libstore/build.cc
@@ -2441,42 +2441,6 @@ void DerivationGoal::deleteTmpDir(bool force)
}
-/* Filter out the special ANSI escape codes generated by Nixpkgs'
- stdenv (used to denote nesting etc.). */
-static string filterNixEscapes(const string & s)
-{
- 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 (c != 'p' && c != 'q' && c != 's' && c != 'a' && c != 'b')
- t += r;
- state = stTop;
- r.clear();
- }
- }
- }
- t += r;
- return t;
-}
-
-
void DerivationGoal::handleChildOutput(int fd, const string & data)
{
if ((hook && fd == hook->builderOut.readSide) ||
@@ -2491,7 +2455,7 @@ void DerivationGoal::handleChildOutput(int fd, const
string & data)
return;
}
if (verbosity >= settings.buildVerbosity)
- writeToStderr(filterNixEscapes(data));
+ writeToStderr(filterANSIEscapes(data, true));
if (bzLogFile) {
int err;
BZ2_bzWrite(&err, bzLogFile, (unsigned char *) data.data(),
data.size());
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 14aab7c..0600352 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -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;
+}
+
+
}
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 5f2d953..f6f5d1b 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -369,4 +369,16 @@ string decodeOctalEscaped(const string & s);
void ignoreException();
+/* Some ANSI escape sequences. */
+#define ANSI_NORMAL "\e[0m"
+#define ANSI_BOLD "\e[1m"
+#define ANSI_RED "\e[31;1m"
+
+
+/* Filter out ANSI escape codes from the given string. If ‘nixOnly’ is
+ set, only filter escape codes generated by Nixpkgs' stdenv (used to
+ denote nesting etc.). */
+string filterANSIEscapes(const string & s, bool nixOnly = false);
+
+
}
diff --git a/src/nix-env/nix-env.cc b/src/nix-env/nix-env.cc
index 44296c7..ad0f4bd 100644
--- a/src/nix-env/nix-env.cc
+++ b/src/nix-env/nix-env.cc
@@ -847,13 +847,6 @@ static VersionDiff compareVersionAgainstSet(
}
-static string colorString(const string & s)
-{
- if (!isatty(STDOUT_FILENO)) return s;
- return "\e[1;31m" + s + "\e[0m";
-}
-
-
static void queryJSON(Globals & globals, vector<DrvInfo> & elems)
{
JSONObject topObj(cout);
@@ -1056,7 +1049,8 @@ static void opQuery(Globals & globals, Strings opFlags,
Strings opArgs)
}
} else {
string column = (string) "" + ch + " " + version;
- if (diff == cvGreater) column = colorString(column);
+ if (diff == cvGreater && isatty(STDOUT_FILENO))
+ column = ANSI_RED + column + ANSI_NORMAL;
columns.push_back(column);
}
}
- 130/376: nix-log2xml: Handle newlines, (continued)
- 130/376: nix-log2xml: Handle newlines, Ludovic Courtès, 2015/01/28
- 143/376: nix-store -l: Automatically pipe output into $PAGER, Ludovic Courtès, 2015/01/28
- 139/376: Fix --attr parsing, Ludovic Courtès, 2015/01/28
- 142/376: Reduce test verbosity, Ludovic Courtès, 2015/01/28
- 138/376: Doh, Ludovic Courtès, 2015/01/28
- 135/376: nix-build: Propagate exit status from nix-store -r, Ludovic Courtès, 2015/01/28
- 125/376: Refactor option handling, Ludovic Courtès, 2015/01/28
- 134/376: build-remote.pl: Provide defaults for $NIX_CURRENT_LOAD and $NIX_REMOTE_SYSTEMS, Ludovic Courtès, 2015/01/28
- 141/376: Filter Nix-specific ANSI escape sequences from stderr, Ludovic Courtès, 2015/01/28
- 140/376: Make hook shutdown more reliable, Ludovic Courtès, 2015/01/28
- 144/376: Add some color,
Ludovic Courtès <=
- 147/376: Handle header file instantiation, Ludovic Courtès, 2015/01/28
- 152/376: Flush std::cout before closing stdout, Ludovic Courtès, 2015/01/28
- 146/376: Install config.h only once, Ludovic Courtès, 2015/01/28
- 148/376: Force template regeneration, Ludovic Courtès, 2015/01/28
- 158/376: fix disappearing bash arguments, Ludovic Courtès, 2015/01/28
- 150/376: Provide reasonable default flags for $LESS, Ludovic Courtès, 2015/01/28
- 149/376: Merge commit '2aa93858afee22e0c32d8f4366970976374091ac', Ludovic Courtès, 2015/01/28
- 156/376: Use PR_SET_PDEATHSIG to ensure child cleanup, Ludovic Courtès, 2015/01/28
- 155/376: Set a curl timeout on binary cache lookups, Ludovic Courtès, 2015/01/28
- 163/376: Fix manual build, Ludovic Courtès, 2015/01/28