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 @@ -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 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 @@ -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(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/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/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,