JDevlieghere created this revision.
JDevlieghere added reviewers: LLDB, labath, teemperor, clayborg.
JDevlieghere added a project: LLDB.
Herald added a subscriber: abidh.
JDevlieghere added a parent revision: D81056: [Support] Replace 'DisableColors' 
boolean with 'ColorMode' enum.
JDevlieghere retitled this revision from "[Interpreter] Color "error:" and 
"warning:" in the CommandReturnObject output." to "[lldb/Interpreter] Color 
"error:" and "warning:" in the CommandReturnObject output.".

Color the `error:` and `warning:` part of the CommandReturnObject output, 
similar to how an error is printed from the driver when colors are enabled.

Here's a screenshot of the output: 
https://jonasdevlieghere.com/static/colors.png


Repository:
  rLLDB LLDB

https://reviews.llvm.org/D81058

Files:
  lldb/include/lldb/Interpreter/CommandReturnObject.h
  lldb/include/lldb/Utility/Stream.h
  lldb/source/Interpreter/CommandInterpreter.cpp
  lldb/source/Interpreter/CommandReturnObject.cpp

Index: lldb/source/Interpreter/CommandReturnObject.cpp
===================================================================
--- lldb/source/Interpreter/CommandReturnObject.cpp
+++ lldb/source/Interpreter/CommandReturnObject.cpp
@@ -28,8 +28,9 @@
     strm.EOL();
 }
 
-CommandReturnObject::CommandReturnObject()
+CommandReturnObject::CommandReturnObject(bool colors)
     : m_out_stream(), m_err_stream(), m_status(eReturnStatusStarted),
+      m_colors(colors ? llvm::ColorMode::Enable : llvm::ColorMode::Disable),
       m_did_change_process_state(false), m_interactive(true) {}
 
 CommandReturnObject::~CommandReturnObject() {}
@@ -45,9 +46,10 @@
 
   const std::string &s = std::string(sstrm.GetString());
   if (!s.empty()) {
-    Stream &error_strm = GetErrorStream();
-    error_strm.PutCString("error: ");
-    DumpStringToStreamWithNewline(error_strm, s);
+    llvm::WithColor(GetErrorStream().AsRawOstream(),
+                    llvm::HighlightColor::Error, m_colors)
+        << "error: ";
+    DumpStringToStreamWithNewline(GetErrorStream(), s);
   }
 }
 
@@ -72,7 +74,9 @@
   sstrm.PrintfVarArg(format, args);
   va_end(args);
 
-  GetErrorStream() << "warning: " << sstrm.GetString();
+  llvm::WithColor(GetErrorStream().AsRawOstream(),
+                  llvm::HighlightColor::Warning, m_colors)
+      << "warning: " << sstrm.GetString();
 }
 
 void CommandReturnObject::AppendMessage(llvm::StringRef in_string) {
@@ -84,7 +88,9 @@
 void CommandReturnObject::AppendWarning(llvm::StringRef in_string) {
   if (in_string.empty())
     return;
-  GetErrorStream() << "warning: " << in_string << "\n";
+  llvm::WithColor(GetErrorStream().AsRawOstream(),
+                  llvm::HighlightColor::Warning, m_colors)
+      << "warning: " << in_string << '\n';
 }
 
 // Similar to AppendWarning, but do not prepend 'warning: ' to message, and
@@ -99,7 +105,9 @@
 void CommandReturnObject::AppendError(llvm::StringRef in_string) {
   if (in_string.empty())
     return;
-  GetErrorStream() << "error: " << in_string << "\n";
+  llvm::WithColor(GetErrorStream().AsRawOstream(), llvm::HighlightColor::Error,
+                  m_colors)
+      << "error: " << in_string << '\n';
 }
 
 void CommandReturnObject::SetError(const Status &error,
Index: lldb/source/Interpreter/CommandInterpreter.cpp
===================================================================
--- lldb/source/Interpreter/CommandInterpreter.cpp
+++ lldb/source/Interpreter/CommandInterpreter.cpp
@@ -209,7 +209,7 @@
   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
 
-  CommandReturnObject result;
+  CommandReturnObject result(m_debugger.GetUseColor());
 
   LoadCommandDictionary();
 
@@ -2792,7 +2792,7 @@
 
   StartHandlingCommand();
 
-  lldb_private::CommandReturnObject result;
+  lldb_private::CommandReturnObject result(m_debugger.GetUseColor());
   HandleCommand(line.c_str(), eLazyBoolCalculate, result);
 
   // Now emit the command output text from the command we just executed
Index: lldb/include/lldb/Utility/Stream.h
===================================================================
--- lldb/include/lldb/Utility/Stream.h
+++ lldb/include/lldb/Utility/Stream.h
@@ -14,6 +14,7 @@
 #include "lldb/lldb-enumerations.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/Process.h"
 #include "llvm/Support/raw_ostream.h"
 
 #include <stdarg.h>
@@ -402,6 +403,43 @@
       return m_target.GetWrittenBytes();
     }
 
+    raw_ostream &changeColor(enum Colors colors, bool bold = false,
+                             bool bg = false) override {
+      if (llvm::sys::Process::ColorNeedsFlush())
+        flush();
+      const char *colorcode = (colors == SAVEDCOLOR)
+                                  ? llvm::sys::Process::OutputBold(bg)
+                                  : llvm::sys::Process::OutputColor(
+                                        static_cast<char>(colors), bold, bg);
+      if (colorcode) {
+        size_t len = strlen(colorcode);
+        write(colorcode, len);
+      }
+      return *this;
+    }
+
+    raw_ostream &resetColor() override {
+      if (llvm::sys::Process::ColorNeedsFlush())
+        flush();
+      const char *colorcode = llvm::sys::Process::ResetColor();
+      if (colorcode) {
+        size_t len = strlen(colorcode);
+        write(colorcode, len);
+      }
+      return *this;
+    }
+
+    raw_ostream &reverseColor() override {
+      if (llvm::sys::Process::ColorNeedsFlush())
+        flush();
+      const char *colorcode = llvm::sys::Process::OutputReverse();
+      if (colorcode) {
+        size_t len = strlen(colorcode);
+        write(colorcode, len);
+      }
+      return *this;
+    }
+
   public:
     RawOstreamForward(Stream &target)
         : llvm::raw_ostream(/*unbuffered*/ true), m_target(target) {}
Index: lldb/include/lldb/Interpreter/CommandReturnObject.h
===================================================================
--- lldb/include/lldb/Interpreter/CommandReturnObject.h
+++ lldb/include/lldb/Interpreter/CommandReturnObject.h
@@ -16,6 +16,7 @@
 
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/WithColor.h"
 
 #include <memory>
 
@@ -23,7 +24,7 @@
 
 class CommandReturnObject {
 public:
-  CommandReturnObject();
+  CommandReturnObject(bool colors = false);
 
   ~CommandReturnObject();
 
@@ -150,6 +151,7 @@
   StreamTee m_err_stream;
 
   lldb::ReturnStatus m_status;
+  llvm::ColorMode m_colors;
   bool m_did_change_process_state;
   bool m_interactive; // If true, then the input handle from the debugger will
                       // be hooked up
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to