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: 752d2e719a619


From: code
Subject: [Monotone-commits-diffs] net.venge.monotone.colored-diff: 752d2e719a619b986c2ac9658e828679379e6dc9
Date: Sun, 10 Apr 2011 19:26:38 +0200 (CEST)

revision:            752d2e719a619b986c2ac9658e828679379e6dc9
date:                2011-04-10T15:52:11
author:              Richard Hopkins <address@hidden>
branch:              net.venge.monotone.colored-diff
changelog:
Theres no functionality difference for Unix/Linux, and everything compiles
and works. I haven't compiled it on Windows yet, but all the structure and
ground functionality is there apart from change_format which needs
implementing.

The existing strategy for keeping a colormap for Unix/Linux is kept as this
holds which ANSI codes are to added to the output for each purpose. The
templatization just means on Win32 the colormap now holds the format
attributes instead, which needs to be passed via API call.

manifest:
format_version "1"

new_manifest [6b6c9c5bde2c29ecbe333821b377565ea94728e0]

old_revision [f9c07b67917833501d4e5110553dc2a9799bf8e8]

patch "src/colorizer.cc"
 from [4b57f12e6d30074c90950efffc34652d30c11c3e]
   to [288f70c817d98d6411dbfb246296373d4c939a58]

patch "src/colorizer.hh"
 from [1c5ed11f8631a859c5788782b05f2e7117b8ff8a]
   to [d2d13a1f10934408761287392b0ba094e898e58b]
============================================================
--- src/colorizer.cc	4b57f12e6d30074c90950efffc34652d30c11c3e
+++ src/colorizer.cc	288f70c817d98d6411dbfb246296373d4c939a58
@@ -53,8 +53,9 @@ string colorizer::purpose_to_name(colori
     }
 }
 
-std::pair<colorizer::purpose, boost::tuple<string, string, string> > colorizer::map_output_color(
-  purpose const p)
+template<typename T>
+std::pair<colorizer::purpose, boost::tuple<T, T, T> >
+colorizer::map_output_color(purpose const p)
 {
   string fg, bg, style;
   string purpose_name = purpose_to_name(p);
@@ -70,14 +71,84 @@ std::pair<colorizer::purpose, boost::tup
       lua.hook_get_output_color(purpose_name, fg, bg, style);
     }
 
-  return std::make_pair(p, boost::make_tuple(fg_to_code(fg),
-                                             bg_to_code(bg),
-                                             style_to_code(style)));
+  return std::make_pair(p, boost::make_tuple(fg_to_code<T>(fg),
+                                             bg_to_code<T>(bg),
+                                             style_to_code<T>(style)));
 }
 
-string colorizer::fg_to_code(string const color) const
+#ifdef WIN32
+
+template<>
+int
+colorizer::fg_to_code<int>(string const color) const
 {
   if (color == "black")
+    return 0;
+  else if (color == "red")
+    return FOREGROUND_RED;
+  else if (color == "green")
+    return FOREGROUND_GREEN;
+  else if (color == "yellow")
+    return FOREGROUND_RED | FOREGROUND_GREEN;
+  else if (color == "blue")
+    return FOREGROUND_BLUE;
+  else if (color == "magenta")
+    return FOREGROUND_RED | FOREGROUND_BLUE;
+  else if (color == "cyan")
+    return FOREGROUND_BLUE | FOREGROUND_GREEN;
+  else if (color == "white")
+    return FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
+  else
+    return FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // default
+}
+
+template<>
+int
+colorizer::bg_to_code<int>(string const color) const
+{
+  if (color == "black")
+    return 0x100;
+  else if (color == "red")
+    return BACKGROUND_RED;
+  else if (color == "green")
+    return BACKGROUND_GREEN;
+  else if (color == "yellow")
+    return BACKGROUND_RED | BACKGROUND_GREEN;
+  else if (color == "blue")
+    return BACKGROUND_BLUE;
+  else if (color == "magenta")
+    return BACKGROUND_RED | BACKGROUND_BLUE;
+  else if (color == "cyan")
+    return BACKGROUND_BLUE | BACKGROUND_GREEN;
+  else if (color == "white")
+    return BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
+  else
+    return 0x100; // default
+}
+
+template<>
+int
+colorizer::style_to_code<int>(string const style) const
+{
+  if (style == "none")
+    return 0;
+  else if (style == "bold")
+    return FOREGROUND_INTENSITY;
+  else if (style == "italic")
+    return 0;
+  else if (style == "underline")
+    return COMMON_LVB_UNDERSCORE;
+  else
+    return 0; // all off
+}
+
+#else // #ifdef WIN32
+
+template<>
+string
+colorizer::fg_to_code<string>(string const color) const
+{
+  if (color == "black")
     return "\033[30m";
   else if (color == "red")
     return "\033[31m";
@@ -97,7 +168,9 @@ string colorizer::fg_to_code(string cons
     return "\033[39m"; // default
 }
 
-string colorizer::bg_to_code(string const color) const
+template<>
+string
+colorizer::bg_to_code<string>(string const color) const
 {
   if (color == "black")
     return "\033[40m";
@@ -119,7 +192,9 @@ string colorizer::bg_to_code(string cons
     return "\033[49m"; // default
 }
 
-string colorizer::style_to_code(string const style) const
+template<>
+string
+colorizer::style_to_code<string>(string const style) const
 {
   if (style == "none")
     return "\033[22m\033[23m\033[24m";
@@ -133,6 +208,28 @@ string colorizer::style_to_code(string c
     return "\033[22m\033[23m\033[24m"; // all off
 }
 
+#endif // #ifdef WIN32
+
+template<typename T>
+void
+colorizer::init_colormap()
+{
+  colormap.insert(map_output_color<T>(normal));
+  colormap.insert(map_output_color<T>(reset));
+
+  colormap.insert(map_output_color<T>(add));
+  colormap.insert(map_output_color<T>(change));
+  colormap.insert(map_output_color<T>(comment));
+  colormap.insert(map_output_color<T>(encloser));
+  colormap.insert(map_output_color<T>(log_revision));
+  colormap.insert(map_output_color<T>(remove));
+  colormap.insert(map_output_color<T>(rename));
+  colormap.insert(map_output_color<T>(rev_header));
+  colormap.insert(map_output_color<T>(separator));
+  colormap.insert(map_output_color<T>(set));
+  colormap.insert(map_output_color<T>(unset));
+}
+
 colorizer::colorizer(bool enable, lua_hooks & lh)
   : lua(lh),
     enabled(enable)
@@ -142,20 +239,11 @@ colorizer::colorizer(bool enable, lua_ho
 
   if (enabled)
     {
-      colormap.insert(map_output_color(normal));
-      colormap.insert(map_output_color(reset));
-
-      colormap.insert(map_output_color(add));
-      colormap.insert(map_output_color(change));
-      colormap.insert(map_output_color(comment));
-      colormap.insert(map_output_color(encloser));
-      colormap.insert(map_output_color(log_revision));
-      colormap.insert(map_output_color(remove));
-      colormap.insert(map_output_color(rename));
-      colormap.insert(map_output_color(rev_header));
-      colormap.insert(map_output_color(separator));
-      colormap.insert(map_output_color(set));
-      colormap.insert(map_output_color(unset));
+#ifdef WIN32
+      init_colormap<int>();
+#else
+      init_colormap<string>();
+#endif
     }
 }
 
@@ -171,12 +259,14 @@ colorizer::colorize(string const & in, p
     return in;
 }
 
-string
+template<typename T>
+T
 colorizer::get_format(purpose const p) const
 {
-  boost::tuple<string, string, string> format = colormap.find(p)->second;
+  boost::tuple<T, T, T> format = colormap.find(p)->second;
 
-  return format.get<0>() + format.get<1>() + format.get<2>();
+  return format.template get<0>() + format.template get<1>() +
+         format.template get<2>();
 }
 
 string
@@ -198,7 +288,7 @@ colorizer::change_format(purpose const p
   // the properly formatted "new_file.txt"
   return "";
 #else
-  return get_format(p);
+  return get_format<string>(p);
 #endif
 }
 
============================================================
--- src/colorizer.hh	1c5ed11f8631a859c5788782b05f2e7117b8ff8a
+++ src/colorizer.hh	d2d13a1f10934408761287392b0ba094e898e58b
@@ -14,6 +14,9 @@
 #include "vocab.hh"
 #include <map>
 #include <boost/tuple/tuple.hpp>
+#ifdef WIN32
+#include <windows.h>
+#endif
 
 struct colorizer
 {
@@ -40,19 +43,25 @@ private:
   colorize(std::string const & in, purpose p = normal) const;
 
 private:
+#ifdef WIN32
+  std::map<purpose, boost::tuple<int, int, int> > colormap;
+#else
   std::map<purpose, boost::tuple<std::string, std::string, std::string> >
   colormap;
+#endif
   lua_hooks & lua;
   bool enabled;
 
-  std::pair<purpose, boost::tuple<std::string, std::string, std::string> >
-  map_output_color(purpose const p);
+  template<typename T> void init_colormap();
 
-  std::string fg_to_code(std::string const color) const;
-  std::string bg_to_code(std::string const color) const;
-  std::string style_to_code(std::string const style) const;
+  template<typename T>
+  std::pair<purpose, boost::tuple<T, T, T> > map_output_color(purpose const p);
 
-  std::string get_format(purpose const p) const;
+  template<typename T> T fg_to_code(std::string const color) const;
+  template<typename T> T bg_to_code(std::string const color) const;
+  template<typename T> T style_to_code(std::string const style) const;
+
+  template<typename T> T get_format(purpose const p) const;
   std::string change_format(purpose const p) const;
 
   std::string purpose_to_name(purpose const p) const;

reply via email to

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