monotone-commits-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Monotone-commits-diffs] net.venge.monotone.colored-diff: 74e936ec017c7


From: code
Subject: [Monotone-commits-diffs] net.venge.monotone.colored-diff: 74e936ec017c783019276fbf2a08110490102996
Date: Fri, 14 Jan 2011 23:04:20 GMT

revision:            74e936ec017c783019276fbf2a08110490102996
date:                2011-01-14T22:40:34
author:              Richard Hopkins
branch:              net.venge.monotone.colored-diff
changelog:
Start of get_output_color Lua hook being used by the colorizer

There is now a new Lua hook "get_output_color" with a default
implementation provided in std_hooks.lua to mimic the original
code. The hook maps a friendly "purpose name" (see 
colorizer::purpose and purpose_to_name) to a friendly
color name, which is then converted to the relevant ASCII escape
code.

As the colorizer now calls the get_output_color hook, it now
requires a lua_hooks instance in the constructor - and each of
the current callers currently pass in app.lua.

I have tried to keep the "const correctness" as mentioned in
HACKING - by making either the parameters const, as well as
the method const if possible.

NOTE: It's most likely that the default get_output_color
implementation could, and should, be cleaned up into a
more natural Lua style - instead of the if/elseif pattern.
Same goes for the color_to_code implementation as that uses
if/else if - although this might not be too bad as it's in 
C++.

manifest:
format_version "1"

new_manifest [2ab705f9e058a076fea356438461156079088f3f]

old_revision [3fd6dbdf433136f25a57a9043d02020cc4e044ba]

patch "asciik.cc"
 from [bdb2253549ec477bf725e9f5ae43c0fa7dff564f]
   to [64600c9969226fa55cd05982364147342d3b734e]

patch "cmd_diff_log.cc"
 from [9152df5398b88e5d29fc7af0941011c1137e169e]
   to [425003fb37c3dd5d8e3e0a34b654d704e11fb414]

patch "cmd_files.cc"
 from [8193666bb289537ceb8ce3fd05d85577dada5fe4]
   to [44c13d2a7ae5e2beb2b316d24202481680779f70]

patch "cmd_ws_commit.cc"
 from [cbcbf79937b6a299324b93357770b225d0e6cde2]
   to [e0bd38df40b3be50ffd8793c134fcc9edfb47bd9]

patch "colorizer.cc"
 from [c219975b61f0749de94fd6e9a6491022acbde7e3]
   to [91a78780eb3129aee04033b1a1c7a84642c2cd51]

patch "colorizer.hh"
 from [acac5ffcef7270972ccd94aa0a671ab27c892450]
   to [f57044cca7f6b76d0890db20d28b4c82b385965b]

patch "lua_hooks.cc"
 from [a71ded3aabcde9e0ef7281b7c56f2ff1fa6baaec]
   to [73cd250a06b6deeba4a1d276beea6cdc3597301f]

patch "lua_hooks.hh"
 from [66b79c7d1dcefed2d3530e71474346b72417fee9]
   to [f391871239b91edd0b281eb62304fbd2f5a74f0b]

patch "std_hooks.lua"
 from [b1a61ba83a6e8c0c9d72393aaf0e86089b75464d]
   to [4b995018e4b3ce9d07938ad28c204b9e28b90378]
============================================================
--- lua_hooks.cc	a71ded3aabcde9e0ef7281b7c56f2ff1fa6baaec
+++ lua_hooks.cc	73cd250a06b6deeba4a1d276beea6cdc3597301f
@@ -756,6 +756,18 @@ bool
 }
 
 bool
+lua_hooks::hook_get_output_color(string const purpose, string & color)
+{
+  Lua ll = Lua(st);
+
+  return ll.func("get_output_color")
+    .push_str(purpose)
+    .call(1, 1)
+    .extract_str(color)
+    .ok();
+}
+
+bool
 lua_hooks::hook_use_inodeprints()
 {
   bool use = false, exec_ok = false;
============================================================
--- lua_hooks.hh	66b79c7d1dcefed2d3530e71474346b72417fee9
+++ lua_hooks.hh	f391871239b91edd0b281eb62304fbd2f5a74f0b
@@ -167,6 +167,8 @@ public:
 
   bool hook_get_man_page_formatter_command(string & command);
 
+  bool hook_get_output_color(string const purpose, string & color);
+
   // notification hooks
   bool hook_note_commit(revision_id const & new_id,
                         revision_data const & rdat,
============================================================
--- std_hooks.lua	b1a61ba83a6e8c0c9d72393aaf0e86089b75464d
+++ std_hooks.lua	4b995018e4b3ce9d07938ad28c204b9e28b90378
@@ -1528,3 +1528,33 @@ end
    end
 end
 
+function get_output_color(purpose)
+	-- returns a friendly color name which will be converted by monotone
+	-- into the relevant ASCII escape code.
+	-- valid return values are
+	-- "" for monotone to use the default output
+	-- black, white, red, green, blue, magenta, yellow, cyan, bold, reset
+	if purpose == "normal" then
+		return ""
+	elseif purpose == "reset" then
+		return "reset"
+	elseif purpose == "diff_add" then
+		return "green"
+	elseif purpose == "diff_change" then
+		return "magenta"
+	elseif purpose == "diff_comment" then
+		return "cyan"
+	elseif purpose == "diff_delete" then
+		return "red"
+	elseif purpose == "diff_encloser" then
+		return "bold"
+	elseif purpose == "diff_separator" then
+		return "bold"
+	elseif purpose == "log_revision" then
+		return "bold"
+	elseif purpose == "rev_header" then
+		return "bold"
+	else
+		return ""
+	end
+end
============================================================
--- cmd_diff_log.cc	9152df5398b88e5d29fc7af0941011c1137e169e
+++ cmd_diff_log.cc	425003fb37c3dd5d8e3e0a34b654d704e11fb414
@@ -454,7 +454,7 @@ CMD(diff, "diff", "di", CMD_REF(informat
 
   prepare_diff(app, db, old_roster, new_roster, args, old_from_db, new_from_db, revs);
 
-  colorizer colorizer(app.opts.colorize);
+  colorizer colorizer(app.opts.colorize, app.lua);
 
   if (app.opts.with_header)
     {
@@ -497,7 +497,7 @@ CMD_AUTOMATE(content_diff, N_("[FILE [..
                dummy_header);
 
   // never colorize the diff output
-  colorizer colorizer(false);
+  colorizer colorizer(false, app.lua);
 
   if (app.opts.with_header)
     {
@@ -864,7 +864,7 @@ log_common(app_state & app,
   set<revision_id> seen;
   revision_t rev;
 
-  colorizer color(app.opts.colorize && !automate);
+  colorizer color(app.opts.colorize && !automate, app.lua);
   // this is instantiated even when not used, but it's lightweight
   asciik graph(output, color);
 
============================================================
--- cmd_files.cc	8193666bb289537ceb8ce3fd05d85577dada5fe4
+++ cmd_files.cc	44c13d2a7ae5e2beb2b316d24202481680779f70
@@ -134,7 +134,7 @@ CMD(fdiff, "fdiff", "", CMD_REF(debug), 
             src_id, dst_id,
             src.inner(), dst.inner(),
             cout, app.opts.diff_format, 
-            pattern, colorizer(app.opts.colorize));
+            pattern, colorizer(app.opts.colorize, app.lua));
 }
 
 CMD(annotate, "annotate", "", CMD_REF(informative), N_("PATH"),
============================================================
--- cmd_ws_commit.cc	cbcbf79937b6a299324b93357770b225d0e6cde2
+++ cmd_ws_commit.cc	e0bd38df40b3be50ffd8793c134fcc9edfb47bd9
@@ -259,7 +259,7 @@ get_log_message_interactively(lua_hooks 
   }
 
   utf8 summary;
-  colorizer color(false);
+  colorizer color(false, lua);
   revision_summary(rev, color, summary);
 
   utf8 full_message(changelog() + cancel() + instructions() + editable() + ignored() +
@@ -966,7 +966,7 @@ CMD(status, "status", "", CMD_REF(inform
 
   utf8 header;
   utf8 summary;
-  colorizer color(app.opts.colorize);
+  colorizer color(app.opts.colorize, app.lua);
 
   revision_header(rid, rev, author, date_t::now(), app.opts.branch, changelog,
                   date_fmt, color, header);
============================================================
--- asciik.cc	bdb2253549ec477bf725e9f5ae43c0fa7dff564f
+++ asciik.cc	64600c9969226fa55cd05982364147342d3b734e
@@ -390,7 +390,7 @@ CMD(asciik, "asciik", "", CMD_REF(debug)
   toposort(db, revs, sorted);
   reverse(sorted.begin(), sorted.end());
 
-  asciik graph(std::cout, colorizer(app.opts.colorize), 10);
+  asciik graph(std::cout, colorizer(app.opts.colorize, app.lua), 10);
 
   for (vector<revision_id>::const_iterator rev = sorted.begin();
        rev != sorted.end(); ++rev)
============================================================
--- colorizer.cc	c219975b61f0749de94fd6e9a6491022acbde7e3
+++ colorizer.cc	91a78780eb3129aee04033b1a1c7a84642c2cd51
@@ -15,23 +15,101 @@ using std::make_pair;
 using std::map;
 using std::make_pair;
 
-colorizer::colorizer(bool enable)
+
+string colorizer::purpose_to_name(colorizer::purpose const p) const
 {
+  switch (p)
+  {
+    case normal:
+      return "normal";
+
+    case reset:
+      return "reset";
+
+    case diff_add:
+      return "diff_add";
+
+    case diff_change:
+      return "diff_change";
+
+    case diff_comment:
+      return "diff_comment";
+
+    case diff_delete:
+      return "diff_delete";
+
+    case diff_encloser:
+      return "diff_encloser";
+
+    case diff_separator:
+      return "diff_separator";
+
+    case log_revision:
+      return "log_revision";
+
+    case rev_header:
+      return "rev_header";
+
+    default:
+      I(false); // should never get here
+  }
+}
+
+std::pair<colorizer::purpose, string> colorizer::map_output_color(
+  purpose const p)
+{
+  string color;
+  string purpose_name = purpose_to_name(p);
+
+  lua.hook_get_output_color(purpose_name, color);
+
+  return std::make_pair(p, color_to_code(color));
+}
+
+string colorizer::color_to_code(string const color) const
+{
+  if (color == "red")
+    return "\033[31m";
+  else if (color == "green")
+    return "\033[32m";
+  else if (color == "blue")
+    return "\033[34m";
+  else if (color == "magenta")
+    return "\033[35m";
+  else if (color == "yellow")
+    return "\033[33m";
+  else if (color == "cyan")
+    return "\033[36m";
+  else if (color == "reset")
+    return "\033[m";
+  else if (color == "bold")
+    return "\033[1m";
+  else if (color == "black")
+    return "\033[30m";
+  else if (color == "white")
+    return "\033[37m";
+  else
+    return "\033[37m"; // no color specified - so use default (white)
+}
+
+colorizer::colorizer(bool enable, lua_hooks & lh) 
+  : lua(lh)
+{
   if (!have_smart_terminal())
     enable = false;
 
   if (enable)
     {
-      colormap.insert(std::make_pair(normal,         ""));
-      colormap.insert(std::make_pair(reset,          "\033[m"));
-      colormap.insert(std::make_pair(diff_encloser,  "\033[1;34m"));
-      colormap.insert(std::make_pair(diff_add,       "\033[32m"));
-      colormap.insert(std::make_pair(diff_delete,    "\033[31m"));
-      colormap.insert(std::make_pair(diff_change,    "\033[33m"));
-      colormap.insert(std::make_pair(diff_comment,   "\033[36m"));
-      colormap.insert(std::make_pair(diff_separator, "\033[1m"));
-      colormap.insert(std::make_pair(log_revision,   "\033[34m"));
-      colormap.insert(std::make_pair(rev_header,     "\033[1m"));
+      colormap.insert(map_output_color(normal));
+      colormap.insert(map_output_color(reset));
+      colormap.insert(map_output_color(diff_encloser));
+      colormap.insert(map_output_color(diff_add));
+      colormap.insert(map_output_color(diff_delete));
+      colormap.insert(map_output_color(diff_change));
+      colormap.insert(map_output_color(diff_comment));
+      colormap.insert(map_output_color(diff_separator));
+      colormap.insert(map_output_color(log_revision));
+      colormap.insert(map_output_color(rev_header));
     }
 }
 
============================================================
--- colorizer.hh	acac5ffcef7270972ccd94aa0a671ab27c892450
+++ colorizer.hh	f57044cca7f6b76d0890db20d28b4c82b385965b
@@ -10,6 +10,7 @@
 #ifndef __COLORIZER_HH__
 #define __COLORIZER_HH__
 
+#include "lua_hooks.hh"
 #include "vocab.hh"
 #include <map>
 
@@ -27,13 +28,20 @@ struct colorizer {
                  rev_header
                  } purpose;
 
-  colorizer(bool enable);
+  colorizer(bool enable, lua_hooks & lh);
 
   std::string
   colorize(std::string const & in, purpose p = normal) const;
 
 private:
   std::map<purpose, std::string> colormap;
+  lua_hooks & lua;
+  
+  std::pair<purpose, std::string>
+  map_output_color(purpose const p);
+
+  std::string color_to_code(std::string const color) const;
+  std::string purpose_to_name(purpose const p) const;
 };
 
 #endif // __COLORIZER_HH__

reply via email to

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