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: 8f80c57ab54c9


From: code
Subject: [Monotone-commits-diffs] net.venge.monotone.colored-diff: 8f80c57ab54c96547b551797d9f66abae284db23
Date: Sun, 10 Apr 2011 20:00:10 +0200 (CEST)

revision:            8f80c57ab54c96547b551797d9f66abae284db23
date:                2011-04-10T17:59:25
author:              Richard Hopkins <address@hidden>
branch:              net.venge.monotone.colored-diff
changelog:
Applied recent development of the colorizer onto latest before
whitespace-cleanup propagate

Looking back, the whitespace-cleanup branch should not have been merged in
to the colored-diff. This commit applies the recent colorizer.{cc,hh}
development which includes Win32 support and allows for an easier review of
the changes against net.venge.monotone.

The net.venge.monotone.colored-diff head which has the whitespace-cleanup
applied will be suspended, because it is easy to redo manually or via
mtn-indent if needed.

manifest:
format_version "1"

new_manifest [a9266c604744dfe4919d473cda5f441be2953b71]

old_revision [90a61d2166a6c30e950a0d02f590b83ca9b6b39d]

patch "src/colorizer.cc"
 from [96964884a83e4d7d638c146821a554d033f396eb]
   to [0a08fe6fb71f513041528ef6773ae9cfbfff99ee]

patch "src/colorizer.hh"
 from [eb3d0e3dad8d447e270e0c6264bf1a4ea139013f]
   to [4ca2bf29f0d731ce80d3136c7fb147a30b3b5139]
============================================================
--- src/colorizer.cc	96964884a83e4d7d638c146821a554d033f396eb
+++ src/colorizer.cc	0a08fe6fb71f513041528ef6773ae9cfbfff99ee
@@ -19,7 +19,7 @@ string colorizer::purpose_to_name(colori
 string colorizer::purpose_to_name(colorizer::purpose const p) const
 {
   switch (p)
-  {
+    {
     case normal:
       return "normal";
     case reset:
@@ -50,11 +50,12 @@ string colorizer::purpose_to_name(colori
 
     default:
       I(false); // should never get here
-  }
+    }
 }
 
-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,46 +208,99 @@ string colorizer::style_to_code(string c
     return "\033[22m\033[23m\033[24m"; // all off
 }
 
-colorizer::colorizer(bool enable, lua_hooks & lh) 
-  : lua(lh)
+#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)
+{
   if (!have_smart_terminal())
-    enable = false;
+    enabled = false;
+#ifdef WIN32
+  win_out = GetStdHandle(STD_OUTPUT_HANDLE);
+  I(win_out != INVALID_HANDLE_VALUE);
 
-  if (enable)
+  CONSOLE_SCREEN_BUFFER_INFO bufinfo;
+  if (GetConsoleScreenBufferInfo(win_out, &bufinfo) == 0)
     {
-      colormap.insert(map_output_color(normal));
-      colormap.insert(map_output_color(reset));
+      enabled = false;
+    }
+#endif
 
-      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));
+  if (enabled)
+    {
+#ifdef WIN32
+      init_colormap<int>();
+#else
+      init_colormap<string>();
+#endif
     }
 }
 
 string
 colorizer::colorize(string const & in, purpose p) const
 {
-  if (colormap.find(p) == colormap.end())
+  if (enabled)
+    {
+      I(colormap.find(p) != colormap.end());
+      return change_format(p) + in + change_format(reset);
+    }
+  else
     return in;
+}
 
-   return get_format(p) + in + get_format(reset);
+template<typename T>
+T
+colorizer::get_format(purpose const p) const
+{
+  boost::tuple<T, T, T> format = colormap.find(p)->second;
+
+  return format.template get<0>() + format.template get<1>() +
+         format.template get<2>();
 }
 
 string
-colorizer::get_format(purpose const p) const
+colorizer::change_format(purpose const p) const
 {
-  boost::tuple<string, string, string> format = colormap.find(p)->second;
+#ifdef WIN32
+  // We will have to rely on Win32 API calls for the side effect of
+  // changing output, but to keep callers happy regardless of platform we
+  // will return an empty string so they can have code like
+  //
+  // change_format(add) + "new_file.txt" + change_format(reset)
+  //
+  // on Windows, this will become "new_file.txt" as the API calls will have
+  // changed the format for us.
+  //
+  // on everything else, this will become
+  // "<ANSI_CODE>new_file.txt<ANSI_CODE>" and the terminal will then display
+  // the properly formatted "new_file.txt"
 
-  return format.get<0>() + format.get<1>() + format.get<2>();
+  SetConsoleTextAttribute(win_out, get_format<int>(p));
+  return "";
+#else
+  return get_format<string>(p);
+#endif
 }
 
 // Local Variables:
============================================================
--- src/colorizer.hh	eb3d0e3dad8d447e270e0c6264bf1a4ea139013f
+++ src/colorizer.hh	4ca2bf29f0d731ce80d3136c7fb147a30b3b5139
@@ -14,8 +14,12 @@
 #include "vocab.hh"
 #include <map>
 #include <boost/tuple/tuple.hpp>
+#ifdef WIN32
+#include <windows.h>
+#endif
 
-struct colorizer {
+struct colorizer
+{
 
   typedef enum { normal = 0,
                  reset,
@@ -31,7 +35,7 @@ struct colorizer {
                  separator,
                  set,
                  unset
-                } purpose;
+               } purpose;
 
   colorizer(bool enable, lua_hooks & lh);
 
@@ -39,19 +43,31 @@ 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;
+  colormap;
+#endif
   lua_hooks & lua;
+  bool enabled;
 
-  std::pair<purpose, boost::tuple<std::string, std::string, std::string> >
-  map_output_color(purpose const p);
+#ifdef WIN32
+  HANDLE win_out;
+#endif
 
-  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> void init_colormap();
 
-  std::string get_format(purpose const p) const;
+  template<typename T>
+  std::pair<purpose, boost::tuple<T, T, T> > map_output_color(purpose const p);
 
+  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]