guix-commits
[Top][All Lists]
Advanced

[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);
                 }
             }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]