[PATCH 05/19] Add style-escape methods to ui_file

Tom Tromey tom@tromey.com
Sat Jan 22 01:37:47 GMT 2022


This adds emit_style_escape and reset_style methods to ui_file.  These
aren't used yet, but they will be once the pager is converted to be a
ui_file subclass.
---
 gdb/ui-file.c | 24 ++++++++++++++++++++++++
 gdb/ui-file.h | 19 +++++++++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/gdb/ui-file.c b/gdb/ui-file.c
index 354a7c3e3b6..2072fd81954 100644
--- a/gdb/ui-file.c
+++ b/gdb/ui-file.c
@@ -73,6 +73,30 @@ ui_file::vprintf (const char *format, va_list args)
 
 /* See ui-file.h.  */
 
+void
+ui_file::emit_style_escape (const ui_file_style &style)
+{
+  if (can_emit_style_escape () && style != m_applied_style)
+    {
+      m_applied_style = style;
+      this->puts (style.to_ansi ().c_str ());
+    }
+}
+
+/* See ui-file.h.  */
+
+void
+ui_file::reset_style ()
+{
+  if (can_emit_style_escape ())
+    {
+      m_applied_style = ui_file_style ();
+      this->puts (m_applied_style.to_ansi ().c_str ());
+    }
+}
+
+/* See ui-file.h.  */
+
 void
 ui_file::printchar (int c, int quoter, bool async_safe)
 {
diff --git a/gdb/ui-file.h b/gdb/ui-file.h
index 101769d73a2..1ea2f60591c 100644
--- a/gdb/ui-file.h
+++ b/gdb/ui-file.h
@@ -109,6 +109,12 @@ class ui_file
 
   void wrap_here (int indent);
 
+  /* Emit an ANSI style escape for STYLE.  */
+  virtual void emit_style_escape (const ui_file_style &style);
+
+  /* Rest the current output style to the empty style.  */
+  virtual void reset_style ();
+
   /* Print STR, bypassing any paging that might be done by this
      ui_file.  Note that nearly no code should call this -- it's
      intended for use by printf_filtered, but nothing else.  */
@@ -117,6 +123,11 @@ class ui_file
     this->puts (str);
   }
 
+protected:
+
+  /* The currently applied style.  */
+  ui_file_style m_applied_style;
+
 private:
 
   /* Helper function for putstr and putstrn.  Print the character C on
@@ -351,6 +362,14 @@ class no_terminal_escape_file : public stdio_file
      sequences.  */
   void write (const char *buf, long length_buf) override;
   void puts (const char *linebuffer) override;
+
+  void emit_style_escape (const ui_file_style &style) override
+  {
+  }
+
+  void reset_style () override
+  {
+  }
 };
 
 /* A ui_file that optionally puts a timestamp at the start of each
-- 
2.31.1



More information about the Gdb-patches mailing list