Remove fputs_styled_unfiltered
[binutils-gdb.git] / gdb / cli-out.c
index 702e4e4dfe2eb187f019a5e7a26ff57092984eb8..f6a508393efd29b2d8dcfe915910c8552bb2fd9f 100644 (file)
@@ -1,6 +1,6 @@
 /* Output generating routines for GDB CLI.
 
-   Copyright (C) 1999-2019 Free Software Foundation, Inc.
+   Copyright (C) 1999-2022 Free Software Foundation, Inc.
 
    Contributed by Cygnus Solutions.
    Written by Fernando Nasser for Cygnus.
@@ -171,10 +171,13 @@ cli_ui_out::do_field_string (int fldno, int width, ui_align align,
 
   if (string)
     {
+      ui_file *stream = m_streams.back ();
+      stream->emit_style_escape (style);
       if (test_flags (unfiltered_output))
-       fputs_styled_unfiltered (string, style, m_streams.back ());
+       stream->puts_unfiltered (string);
       else
-       fputs_styled (string, style, m_streams.back ());
+       stream->puts (string);
+      stream->emit_style_escape (ui_file_style ());
     }
 
   if (after)
@@ -206,7 +209,7 @@ cli_ui_out::do_spaces (int numspaces)
     return;
 
   if (test_flags (unfiltered_output))
-    print_spaces (numspaces, m_streams.back ());
+    fprintf_unfiltered (m_streams.back (), "%*s", numspaces, "");
   else
     print_spaces_filtered (numspaces, m_streams.back ());
 }
@@ -230,20 +233,26 @@ cli_ui_out::do_message (const ui_file_style &style,
   if (m_suppress_output)
     return;
 
-  /* Use the "no_gdbfmt" variant here to avoid recursion.
-     vfprintf_styled calls into cli_ui_out::message to handle the
-     gdb-specific printf formats.  */
-  vfprintf_styled_no_gdbfmt (m_streams.back (), style,
-                            !test_flags (unfiltered_output), format, args);
+  std::string str = string_vprintf (format, args);
+  if (!str.empty ())
+    {
+      ui_file *stream = m_streams.back ();
+      stream->emit_style_escape (style);
+      if (test_flags (unfiltered_output))
+       stream->puts_unfiltered (str.c_str ());
+      else
+       stream->puts (str.c_str ());
+      stream->emit_style_escape (ui_file_style ());
+    }
 }
 
 void
-cli_ui_out::do_wrap_hint (const char *identstring)
+cli_ui_out::do_wrap_hint (int indent)
 {
   if (m_suppress_output)
     return;
 
-  wrap_here (identstring);
+  m_streams.back ()->wrap_here (indent);
 }
 
 void
@@ -265,6 +274,107 @@ cli_ui_out::do_redirect (ui_file *outstream)
     m_streams.pop_back ();
 }
 
+/* The cli_ui_out::do_progress_* functions result in the following:
+   - printed for tty, SHOULD_PRINT == true:
+     <NAME
+      [#####                      ]\r>
+   - printed for tty, SHOULD_PRINT == false:
+     <>
+   - printed for not-a-tty:
+     <NAME...
+     >
+*/
+
+void
+cli_ui_out::do_progress_start (const std::string &name, bool should_print)
+{
+  struct ui_file *stream = m_streams.back ();
+  cli_progress_info meter;
+
+  meter.last_value = 0;
+  meter.name = name;
+  if (!stream->isatty ())
+    {
+      fprintf_unfiltered (stream, "%s...", meter.name.c_str ());
+      gdb_flush (stream);
+      meter.printing = WORKING;
+    }
+  else
+    {
+      /* Don't actually emit anything until the first call notifies us
+        of progress.  This makes it so a second progress message can
+        be started before the first one has been notified, without
+        messy output.  */
+      meter.printing = should_print ? START : NO_PRINT;
+    }
+
+  m_meters.push_back (std::move (meter));
+}
+
+void
+cli_ui_out::do_progress_notify (double howmuch)
+{
+  struct ui_file *stream = m_streams.back ();
+  cli_progress_info &meter (m_meters.back ());
+
+  if (meter.printing == NO_PRINT)
+    return;
+
+  if (meter.printing == START)
+    {
+      fprintf_unfiltered (stream, "%s\n", meter.name.c_str ());
+      gdb_flush (stream);
+      meter.printing = WORKING;
+    }
+
+  if (meter.printing == WORKING && howmuch >= 1.0)
+    return;
+
+  if (!stream->isatty ())
+    return;
+
+  int chars_per_line = get_chars_per_line ();
+  if (chars_per_line > 0)
+    {
+      int i, max;
+      int width = chars_per_line - 3;
+
+      max = width * howmuch;
+      fprintf_unfiltered (stream, "\r[");
+      for (i = 0; i < width; ++i)
+       fprintf_unfiltered (stream, i < max ? "#" : " ");
+      fprintf_unfiltered (stream, "]");
+      gdb_flush (stream);
+      meter.printing = PROGRESS;
+    }
+}
+
+void
+cli_ui_out::do_progress_end ()
+{
+  struct ui_file *stream = m_streams.back ();
+  cli_progress_info &meter = m_meters.back ();
+
+  if (!stream->isatty ())
+    {
+      fprintf_unfiltered (stream, "\n");
+      gdb_flush (stream);
+    }
+  else if (meter.printing == PROGRESS)
+    {
+      int i;
+      int width = get_chars_per_line () - 3;
+
+      fprintf_unfiltered (stream, "\r");
+      for (i = 0; i < width + 2; ++i)
+       fprintf_unfiltered (stream, " ");
+      fprintf_unfiltered (stream, "\r");
+      gdb_flush (stream);
+    }
+
+  m_meters.pop_back ();
+}
+
 /* local functions */
 
 void