From eaabcba1c39a22c588b8c9336ec7ba98aefce86e Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Sat, 25 Jun 2016 13:25:31 +0200 Subject: [PATCH 1/2] Colorize Berlin NixOS meetup --- nix-repl.cc | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/nix-repl.cc b/nix-repl.cc index a9a21ae76..e28c54303 100644 --- a/nix-repl.cc +++ b/nix-repl.cc @@ -18,6 +18,13 @@ using namespace std; using namespace nix; +#define ESC_RED "\033[31m" +#define ESC_GRE "\033[32m" +#define ESC_YEL "\033[33m" +#define ESC_BLU "\033[34m" +#define ESC_MAG "\033[35m" +#define ESC_CYA "\033[36m" +#define ESC_END "\033[0m" struct NixRepl { @@ -540,23 +547,25 @@ std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int m switch (v.type) { case tInt: - str << v.integer; + str << ESC_CYA << v.integer << ESC_END; break; case tBool: - str << (v.boolean ? "true" : "false"); + str << ESC_CYA << (v.boolean ? "true" : "false") << ESC_END; break; case tString: + str << ESC_YEL; printStringValue(str, v.string.s); + str << ESC_END; break; case tPath: - str << v.path; // !!! escaping? + str << ESC_GRE << v.path << ESC_END; // !!! escaping? break; case tNull: - str << "null"; + str << ESC_CYA "null" ESC_END; break; case tAttrs: { @@ -609,7 +618,7 @@ std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int m try { printValue(str, *i.second, maxDepth - 1, seen); } catch (AssertionError & e) { - str << "«error: " << e.msg() << "»"; + str << ESC_RED "«error: " << e.msg() << "»" ESC_END; } str << "; "; } @@ -635,7 +644,7 @@ std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int m try { printValue(str, *v.listElems()[n], maxDepth - 1, seen); } catch (AssertionError & e) { - str << "«error: " << e.msg() << "»"; + str << ESC_RED "«error: " << e.msg() << "»" ESC_END; } str << " "; } @@ -645,19 +654,19 @@ std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int m break; case tLambda: - str << "«lambda»"; + str << ESC_BLU "«lambda»" ESC_END; break; case tPrimOp: - str << "«primop»"; + str << ESC_MAG "«primop»" ESC_END; break; case tPrimOpApp: - str << "«primop-app»"; + str << ESC_BLU "«primop-app»" ESC_END; break; default: - str << "«unknown»"; + str << "ESC_RED «unknown»" ESC_END; break; } From 34ec98176e4644c8c6ec45e4e932b83cbddc6cce Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Sat, 25 Jun 2016 13:40:50 +0200 Subject: [PATCH 2/2] fixup "Colorize" Do not quote escape sequence macro --- nix-repl.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nix-repl.cc b/nix-repl.cc index e28c54303..fc0c37913 100644 --- a/nix-repl.cc +++ b/nix-repl.cc @@ -551,7 +551,7 @@ std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int m break; case tBool: - str << ESC_CYA << (v.boolean ? "true" : "false") << ESC_END; + str << ESC_CYA << (v.boolean ? "true" : "false") << ESC_END; break; case tString: @@ -666,7 +666,7 @@ std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int m break; default: - str << "ESC_RED «unknown»" ESC_END; + str << ESC_RED "«unknown»" ESC_END; break; }