[Lldb-commits] [lldb] Colorize output when searching for symbols in lldb (PR #69422)

David Spickett via lldb-commits lldb-commits at lists.llvm.org
Thu Nov 2 08:43:13 PDT 2023


================
@@ -1506,13 +1514,50 @@ static bool LookupAddressInModule(CommandInterpreter &interpreter, Stream &strm,
 
     ExecutionContextScope *exe_scope =
         interpreter.GetExecutionContext().GetBestExecutionContextScope();
-    DumpAddress(exe_scope, so_addr, verbose, all_ranges, strm);
+    DumpAddress(exe_scope, so_addr, verbose, all_ranges, strm, nullptr);
     return true;
   }
 
   return false;
 }
 
+//===========================================================================================
+
+// This function is the one which colorizes the regex symbol searched
+static void PrintRed(Stream &strm, const char *text, const char *name, CommandInterpreter *interpreter= nullptr) {
+    if (!name) {
+        strm.PutCString(text);
+        return;
+    }
+
+    bool use_color = interpreter->GetDebugger().GetUseColor();
+    
+    std::string str_text(text);
+    std::regex reg_name(name);
+    std::sregex_iterator next(str_text.begin(), str_text.end(), reg_name);
+    std::sregex_iterator end;
+
+    std::string red_start = lldb_private::ansi::FormatAnsiTerminalCodes("${ansi.fg.red}", use_color);
+    std::string reset_color = lldb_private::ansi::FormatAnsiTerminalCodes("${ansi.normal}", use_color);
+
+    size_t last_pos = 0;
+    while (next != end) {
+        std::smatch match = *next;
+        size_t prefix_len = match.position() - last_pos;
+
+        strm.Write(text, prefix_len);
+        strm.PutCString(red_start.c_str());
+        strm.Write(text + match.position(), match.length());
+        strm.PutCString(reset_color.c_str());
+
+        last_pos = match.position() + match.length();
+        ++next;
----------------
DavidSpickett wrote:

I generally prefer a for loop for this sort of thing, so that one doesn't forget this last bit.
```
for ( ; next != end; ++next) {
...
}
```

https://github.com/llvm/llvm-project/pull/69422


More information about the lldb-commits mailing list