[Lldb-commits] PATCH for REVIEW: parse ansi color codes for prompt, add use color settings

Michael Sartain mikesart at gmail.com
Wed May 22 12:35:42 PDT 2013


Index: include/lldb/Core/Debugger.h
===================================================================
--- include/lldb/Core/Debugger.h (revision 182486)
+++ include/lldb/Core/Debugger.h (working copy)
@@ -308,6 +308,12 @@
     bool
     SetUseExternalEditor (bool use_external_editor_p);

+    bool
+    GetUseColor () const;
+
+    bool
+    SetUseColor (bool use_color);
+
     uint32_t
     GetStopSourceLineCount (bool before) const;

Index: include/lldb/Utility/AnsiTerminal.h
===================================================================
--- include/lldb/Utility/AnsiTerminal.h (revision 182486)
+++ include/lldb/Utility/AnsiTerminal.h (working copy)
@@ -21,7 +21,7 @@
 #define ANSI_BG_COLOR_BLACK         40
 #define ANSI_BG_COLOR_RED           41
 #define ANSI_BG_COLOR_GREEN         42
-#define ANSI_BG_COLOR_YELLOW        44
+#define ANSI_BG_COLOR_YELLOW        43
 #define ANSI_BG_COLOR_BLUE          44
 #define ANSI_BG_COLOR_PURPLE        45
 #define ANSI_BG_COLOR_CYAN          46
@@ -82,5 +82,75 @@
         const char *k_ctrl_conceal     = "8";
         const char *k_ctrl_crossed_out = "9";

+        inline std::string
+        FormatAnsiTerminalCodes(const char *format, bool do_color = true)
+        {
+            // Convert "${ansi.XXX}" tokens to ansi values or clear them
if do_color is false.
+            static const struct
+            {
+                const char *name;
+                const char *value;
+            } g_color_tokens[] =
+            {
+        #define _TO_STR2(_val) #_val
+        #define _TO_STR(_val) _TO_STR2(_val)
+                { "fg.black}",        ANSI_ESC_START
_TO_STR(ANSI_FG_COLOR_BLACK)      ANSI_ESC_END },
+                { "fg.red}",          ANSI_ESC_START
_TO_STR(ANSI_FG_COLOR_RED)        ANSI_ESC_END },
+                { "fg.green}",        ANSI_ESC_START
_TO_STR(ANSI_FG_COLOR_GREEN)      ANSI_ESC_END },
+                { "fg.yellow}",       ANSI_ESC_START
_TO_STR(ANSI_FG_COLOR_YELLOW)     ANSI_ESC_END },
+                { "fg.blue}",         ANSI_ESC_START
_TO_STR(ANSI_FG_COLOR_BLUE)       ANSI_ESC_END },
+                { "fg.purple}",       ANSI_ESC_START
_TO_STR(ANSI_FG_COLOR_PURPLE)     ANSI_ESC_END },
+                { "fg.cyan}",         ANSI_ESC_START
_TO_STR(ANSI_FG_COLOR_CYAN)       ANSI_ESC_END },
+                { "fg.white}",        ANSI_ESC_START
_TO_STR(ANSI_FG_COLOR_WHITE)      ANSI_ESC_END },
+                { "bg.black}",        ANSI_ESC_START
_TO_STR(ANSI_BG_COLOR_BLACK)      ANSI_ESC_END },
+                { "bg.red}",          ANSI_ESC_START
_TO_STR(ANSI_BG_COLOR_RED)        ANSI_ESC_END },
+                { "bg.green}",        ANSI_ESC_START
_TO_STR(ANSI_BG_COLOR_GREEN)      ANSI_ESC_END },
+                { "bg.yellow}",       ANSI_ESC_START
_TO_STR(ANSI_BG_COLOR_YELLOW)     ANSI_ESC_END },
+                { "bg.blue}",         ANSI_ESC_START
_TO_STR(ANSI_BG_COLOR_BLUE)       ANSI_ESC_END },
+                { "bg.purple}",       ANSI_ESC_START
_TO_STR(ANSI_BG_COLOR_PURPLE)     ANSI_ESC_END },
+                { "bg.cyan}",         ANSI_ESC_START
_TO_STR(ANSI_BG_COLOR_CYAN)       ANSI_ESC_END },
+                { "bg.white}",        ANSI_ESC_START
_TO_STR(ANSI_BG_COLOR_WHITE)      ANSI_ESC_END },
+                { "normal}",          ANSI_ESC_START
_TO_STR(ANSI_CTRL_NORMAL)         ANSI_ESC_END },
+                { "bold}",            ANSI_ESC_START
_TO_STR(ANSI_CTRL_BOLD)           ANSI_ESC_END },
+                { "faint}",           ANSI_ESC_START
_TO_STR(ANSI_CTRL_FAINT)          ANSI_ESC_END },
+                { "italic}",          ANSI_ESC_START
_TO_STR(ANSI_CTRL_ITALIC)         ANSI_ESC_END },
+                { "underline}",       ANSI_ESC_START
_TO_STR(ANSI_CTRL_UNDERLINE)      ANSI_ESC_END },
+                { "slow-blink}",      ANSI_ESC_START
_TO_STR(ANSI_CTRL_SLOW_BLINK)     ANSI_ESC_END },
+                { "fast-blink}",      ANSI_ESC_START
_TO_STR(ANSI_CTRL_FAST_BLINK)     ANSI_ESC_END },
+                { "negative}",        ANSI_ESC_START
_TO_STR(ANSI_CTRL_IMAGE_NEGATIVE) ANSI_ESC_END },
+                { "conceal}",         ANSI_ESC_START
_TO_STR(ANSI_CTRL_CONCEAL)        ANSI_ESC_END },
+                { "crossed-out}",     ANSI_ESC_START
_TO_STR(ANSI_CTRL_CROSSED_OUT)    ANSI_ESC_END },
+        #undef _TO_STR
+        #undef _TO_STR2
+            };
+            static const char tok_hdr[] = "${ansi.";
+
+            std::string fmt;
+            for (const char *p = format; *p; ++p)
+            {
+                const char *tok_start = strstr (p, tok_hdr);
+                if (!tok_start)
+                {
+                    fmt.append (p);
+                    break;
+                }
+
+                fmt.append (p, tok_start - p);
+                p = tok_start;
+
+                const char *tok_str = tok_start + sizeof(tok_hdr) - 1;
+                for (size_t i = 0; i < sizeof(g_color_tokens) /
sizeof(g_color_tokens[0]); ++i)
+                {
+                    if (!strncmp (tok_str, g_color_tokens[i].name,
strlen(g_color_tokens[i].name)))
+                    {
+                        if (do_color)
+                            fmt.append (g_color_tokens[i].value);
+                        p = tok_str + strlen (g_color_tokens[i].name) - 1;
+                        break;
+                    }
+                }
+            }
+            return fmt;
+        }
     }
 }
Index: source/Core/Debugger.cpp
===================================================================
--- source/Core/Debugger.cpp (revision 182486)
+++ source/Core/Debugger.cpp (working copy)
@@ -131,6 +131,7 @@
 {   "term-width",               OptionValue::eTypeSInt64 , true, 80   ,
NULL, NULL, "The maximum number of columns to use for displaying text." },
 {   "thread-format",            OptionValue::eTypeString , true, 0    ,
DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when
displaying thread information." },
 {   "use-external-editor",      OptionValue::eTypeBoolean, true, false,
NULL, NULL, "Whether to use an external editor or not." },
+{   "use-color",                OptionValue::eTypeBoolean, true, true ,
NULL, NULL, "Whether to use Ansi color codes or not." },

     {   NULL,                       OptionValue::eTypeInvalid, true, 0
 , NULL, NULL, NULL }
 };
@@ -148,7 +149,8 @@
     ePropertyStopLineCountBefore,
     ePropertyTerminalWidth,
     ePropertyThreadFormat,
-    ePropertyUseExternalEditor
+    ePropertyUseExternalEditor,
+    ePropertyUseColor,
 };

 //
@@ -186,9 +188,17 @@
         if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
         {
             const char *new_prompt = GetPrompt();
+            std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes
(new_prompt, GetUseColor());
+            if (str.length())
+                new_prompt = str.c_str();
             EventSP prompt_change_event_sp (new
Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes
(new_prompt)));
             GetCommandInterpreter().BroadcastEvent
(prompt_change_event_sp);
         }
+        else if (strcmp(property_path,
g_properties[ePropertyUseColor].name) == 0)
+        {
+ // use-color changed. Ping the prompt so it can reset the ansi terminal
codes.
+            SetPrompt (GetPrompt());
+        }
         else if (is_load_script && target_sp && load_script_old_value ==
eLoadScriptFromSymFileWarn)
         {
             if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile()
== eLoadScriptFromSymFileTrue)
@@ -244,6 +254,9 @@
     const uint32_t idx = ePropertyPrompt;
     m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
     const char *new_prompt = GetPrompt();
+    std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes
(new_prompt, GetUseColor());
+    if (str.length())
+        new_prompt = str.c_str();
     EventSP prompt_change_event_sp (new
Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes
(new_prompt)));;
     GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
 }
@@ -297,6 +310,22 @@
     return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
 }

+bool
+Debugger::GetUseColor () const
+{
+    const uint32_t idx = ePropertyUseColor;
+    return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx,
g_properties[idx].default_uint_value != 0);
+}
+
+bool
+Debugger::SetUseColor (bool b)
+{
+    const uint32_t idx = ePropertyUseColor;
+    bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
+    SetPrompt (GetPrompt());
+    return ret;
+}
+
 uint32_t
 Debugger::GetStopSourceLineCount (bool before) const
 {
@@ -629,6 +658,11 @@
     OptionValueSInt64 *term_width =
m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL,
ePropertyTerminalWidth);
     term_width->SetMinimumValue(10);
     term_width->SetMaximumValue(1024);
+
+    // Turn off use-color if this is a dumb terminal.
+    const char *term = getenv ("TERM");
+    if (term && !strcmp (term, "dumb"))
+        SetUseColor (false);
 }

 Debugger::~Debugger ()
@@ -1339,6 +1373,12 @@
     bool success = true;
     const char *p;
     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
+
+    bool use_color = exe_ctx ?
exe_ctx->GetTargetRef().GetDebugger().GetUseColor() : true;
+    std::string format_str = lldb_utility::ansi::FormatAnsiTerminalCodes
(format, use_color);
+    if (format_str.length())
+        format = format_str.c_str();
+
     for (p = format; *p != '\0'; ++p)
     {
         if (realvalobj)
@@ -1735,214 +1775,6 @@
                                     format_addr = *addr;
                                 }
                             }
-                            else if (::strncmp (var_name_begin, "ansi.",
strlen("ansi.")) == 0)
-                            {
-                                var_success = true;
-                                var_name_begin += strlen("ansi."); // Skip
the "ansi."
-                                if (::strncmp (var_name_begin, "fg.",
strlen("fg.")) == 0)
-                                {
-                                    var_name_begin += strlen("fg."); //
Skip the "fg."
-                                    if (::strncmp (var_name_begin,
"black}", strlen("black}")) == 0)
-                                    {
-                                        s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_fg_black,
-
 lldb_utility::ansi::k_escape_end);
-                                    }
-                                    else if (::strncmp (var_name_begin,
"red}", strlen("red}")) == 0)
-                                    {
-                                        s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_fg_red,
-
 lldb_utility::ansi::k_escape_end);
-                                    }
-                                    else if (::strncmp (var_name_begin,
"green}", strlen("green}")) == 0)
-                                    {
-                                        s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_fg_green,
-
 lldb_utility::ansi::k_escape_end);
-                                    }
-                                    else if (::strncmp (var_name_begin,
"yellow}", strlen("yellow}")) == 0)
-                                    {
-                                        s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_fg_yellow,
-
 lldb_utility::ansi::k_escape_end);
-                                    }
-                                    else if (::strncmp (var_name_begin,
"blue}", strlen("blue}")) == 0)
-                                    {
-                                        s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_fg_blue,
-
 lldb_utility::ansi::k_escape_end);
-                                    }
-                                    else if (::strncmp (var_name_begin,
"purple}", strlen("purple}")) == 0)
-                                    {
-                                        s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_fg_purple,
-
 lldb_utility::ansi::k_escape_end);
-                                    }
-                                    else if (::strncmp (var_name_begin,
"cyan}", strlen("cyan}")) == 0)
-                                    {
-                                        s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_fg_cyan,
-
 lldb_utility::ansi::k_escape_end);
-                                    }
-                                    else if (::strncmp (var_name_begin,
"white}", strlen("white}")) == 0)
-                                    {
-                                        s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_fg_white,
-
 lldb_utility::ansi::k_escape_end);
-                                    }
-                                    else
-                                    {
-                                        var_success = false;
-                                    }
-                                }
-                                else if (::strncmp (var_name_begin, "bg.",
strlen("bg.")) == 0)
-                                {
-                                    var_name_begin += strlen("bg."); //
Skip the "bg."
-                                    if (::strncmp (var_name_begin,
"black}", strlen("black}")) == 0)
-                                    {
-                                        s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_bg_black,
-
 lldb_utility::ansi::k_escape_end);
-                                    }
-                                    else if (::strncmp (var_name_begin,
"red}", strlen("red}")) == 0)
-                                    {
-                                        s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_bg_red,
-
 lldb_utility::ansi::k_escape_end);
-                                    }
-                                    else if (::strncmp (var_name_begin,
"green}", strlen("green}")) == 0)
-                                    {
-                                        s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_bg_green,
-
 lldb_utility::ansi::k_escape_end);
-                                    }
-                                    else if (::strncmp (var_name_begin,
"yellow}", strlen("yellow}")) == 0)
-                                    {
-                                        s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_bg_yellow,
-
 lldb_utility::ansi::k_escape_end);
-                                    }
-                                    else if (::strncmp (var_name_begin,
"blue}", strlen("blue}")) == 0)
-                                    {
-                                        s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_bg_blue,
-
 lldb_utility::ansi::k_escape_end);
-                                    }
-                                    else if (::strncmp (var_name_begin,
"purple}", strlen("purple}")) == 0)
-                                    {
-                                        s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_bg_purple,
-
 lldb_utility::ansi::k_escape_end);
-                                    }
-                                    else if (::strncmp (var_name_begin,
"cyan}", strlen("cyan}")) == 0)
-                                    {
-                                        s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_bg_cyan,
-
 lldb_utility::ansi::k_escape_end);
-                                    }
-                                    else if (::strncmp (var_name_begin,
"white}", strlen("white}")) == 0)
-                                    {
-                                        s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_bg_white,
-
 lldb_utility::ansi::k_escape_end);
-                                    }
-                                    else
-                                    {
-                                        var_success = false;
-                                    }
-                                }
-                                else if (::strncmp (var_name_begin,
"normal}", strlen ("normal}")) == 0)
-                                {
-                                    s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_ctrl_normal,
-
 lldb_utility::ansi::k_escape_end);
-                                }
-                                else if (::strncmp (var_name_begin,
"bold}", strlen("bold}")) == 0)
-                                {
-                                    s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_ctrl_bold,
-
 lldb_utility::ansi::k_escape_end);
-                                }
-                                else if (::strncmp (var_name_begin,
"faint}", strlen("faint}")) == 0)
-                                {
-                                    s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_ctrl_faint,
-
 lldb_utility::ansi::k_escape_end);
-                                }
-                                else if (::strncmp (var_name_begin,
"italic}", strlen("italic}")) == 0)
-                                {
-                                    s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_ctrl_italic,
-
 lldb_utility::ansi::k_escape_end);
-                                }
-                                else if (::strncmp (var_name_begin,
"underline}", strlen("underline}")) == 0)
-                                {
-                                    s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_ctrl_underline,
-
 lldb_utility::ansi::k_escape_end);
-                                }
-                                else if (::strncmp (var_name_begin,
"slow-blink}", strlen("slow-blink}")) == 0)
-                                {
-                                    s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_ctrl_slow_blink,
-
 lldb_utility::ansi::k_escape_end);
-                                }
-                                else if (::strncmp (var_name_begin,
"fast-blink}", strlen("fast-blink}")) == 0)
-                                {
-                                    s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_ctrl_fast_blink,
-
 lldb_utility::ansi::k_escape_end);
-                                }
-                                else if (::strncmp (var_name_begin,
"negative}", strlen("negative}")) == 0)
-                                {
-                                    s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_ctrl_negative,
-
 lldb_utility::ansi::k_escape_end);
-                                }
-                                else if (::strncmp (var_name_begin,
"conceal}", strlen("conceal}")) == 0)
-                                {
-                                    s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_ctrl_conceal,
-
 lldb_utility::ansi::k_escape_end);
-
-                                }
-                                else if (::strncmp (var_name_begin,
"crossed-out}", strlen("crossed-out}")) == 0)
-                                {
-                                    s.Printf ("%s%s%s",
-
 lldb_utility::ansi::k_escape_start,
-
 lldb_utility::ansi::k_ctrl_crossed_out,
-
 lldb_utility::ansi::k_escape_end);
-                                }
-                                else
-                                {
-                                    var_success = false;
-                                }
-                            }
                             break;

                         case 'p':
Index: tools/driver/Driver.cpp
===================================================================
--- tools/driver/Driver.cpp (revision 182486)
+++ tools/driver/Driver.cpp (working copy)
@@ -105,7 +105,9 @@
         "Tells the debugger to open source files using the host's
\"external editor\" mechanism." },
     { LLDB_3_TO_5,       false, "no-lldbinit"    , 'x', no_argument      ,
0,  eArgTypeNone,
         "Do not automatically parse any '.lldbinit' files." },
-    { LLDB_OPT_SET_6,    true , "python-path"        , 'P', no_argument
   , 0,  eArgTypeNone,
+    { LLDB_3_TO_5,       false, "no-use-colors"  , 'o', no_argument      ,
0,  eArgTypeNone,
+        "Do not use colors." },
+    { LLDB_OPT_SET_6,    true , "python-path"    , 'P', no_argument      ,
0,  eArgTypeNone,
         "Prints out the path to the lldb.py file for this version of
lldb." },
     { 0,                 false, NULL             , 0  , 0                ,
0,  eArgTypeNone,         NULL }
 };
@@ -621,6 +623,10 @@
                         m_debugger.SkipAppInitFiles (true);
                         break;

+                    case 'o':
+                        m_debugger.SetUseColor (false);
+                        break;
+
                     case 'f':
                         {
                             SBFileSpec file(optarg);
Index: include/lldb/API/SBDebugger.h
===================================================================
--- include/lldb/API/SBDebugger.h (revision 182486)
+++ include/lldb/API/SBDebugger.h (working copy)
@@ -172,6 +172,12 @@
     bool
     GetUseExternalEditor ();

+    bool
+    SetUseColor (bool use_color);
+
+    bool
+    GetUseColor () const;
+
     static bool
     GetDefaultArchitecture (char *arch_name, size_t arch_name_len);

Index: source/API/SBDebugger.cpp
===================================================================
--- source/API/SBDebugger.cpp (revision 182486)
+++ source/API/SBDebugger.cpp (working copy)
@@ -1057,6 +1057,22 @@
 }

 bool
+SBDebugger::SetUseColor (bool value)
+{
+    if (m_opaque_sp)
+        return m_opaque_sp->SetUseColor (value);
+    return false;
+}
+
+bool
+SBDebugger::GetUseColor () const
+{
+    if (m_opaque_sp)
+        return m_opaque_sp->GetUseColor ();
+    return false;
+}
+
+bool
 SBDebugger::GetDescription (SBStream &description)
 {
     Stream &strm = description.ref();
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20130522/1dc0124d/attachment.html>


More information about the lldb-commits mailing list