]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Reset terminal styles
authorTom Tromey <tom@tromey.com>
Sat, 17 Nov 2018 18:49:25 +0000 (11:49 -0700)
committerTom Tromey <tom@tromey.com>
Fri, 28 Dec 2018 19:49:49 +0000 (12:49 -0700)
This adds a function that can be used to reset terminal styles,
regardless of what style the low-level output routines currently think
is applied.

This is used to make "echo" and "printf" work properly when emitting
ANSI terminal escapes -- now gdb will reset the style at the end of
the command.

gdb/ChangeLog
2018-12-28  Tom Tromey  <tom@tromey.com>

* utils.h (reset_terminal_style): Declare.
* utils.c (can_emit_style_escape): New function.
(set_output_style): Use it.
(reset_terminal_style): New function.
* printcmd.c (printf_command): Call reset_terminal_style.
* cli/cli-cmds.c (echo_command): Call reset_terminal_style.

gdb/ChangeLog
gdb/cli/cli-cmds.c
gdb/printcmd.c
gdb/utils.c
gdb/utils.h

index 65b3ee69890465f3010f4ad1a31595b1e8991a8b..cb126ff65d0612398c12d8107726e38d345faeec 100644 (file)
@@ -1,3 +1,12 @@
+2018-12-28  Tom Tromey  <tom@tromey.com>
+
+       * utils.h (reset_terminal_style): Declare.
+       * utils.c (can_emit_style_escape): New function.
+       (set_output_style): Use it.
+       (reset_terminal_style): New function.
+       * printcmd.c (printf_command): Call reset_terminal_style.
+       * cli/cli-cmds.c (echo_command): Call reset_terminal_style.
+
 2018-12-28  Tom Tromey  <tom@tromey.com>
 
        * utils.h (set_output_style, fprintf_styled)
index 135f550b80136afc840e03e804c223cc5c352069..12ac74c7e95bd82eb023383b556e17f206c0efc5 100644 (file)
@@ -689,6 +689,8 @@ echo_command (const char *text, int from_tty)
          printf_filtered ("%c", c);
       }
 
+  reset_terminal_style (gdb_stdout);
+
   /* Force this output to appear now.  */
   wrap_here ("");
   gdb_flush (gdb_stdout);
index 8c999188d71edc4d20674a34b5dfc56c02e167b5..79c3d2d2ffb0250099cae8c6d76c816847a77e69 100644 (file)
@@ -2609,6 +2609,8 @@ static void
 printf_command (const char *arg, int from_tty)
 {
   ui_printf (arg, gdb_stdout);
+  reset_terminal_style (gdb_stdout);
+  wrap_here ("");
   gdb_flush (gdb_stdout);
 }
 
index 2fb7476a35ac14ec5a90d217daad94bb1f369371..4bb2f34fc9af29b177a0cddad7abdb17c6d26b86 100644 (file)
@@ -1444,25 +1444,50 @@ emit_style_escape (const ui_file_style &style)
   wrap_buffer.append (style.to_ansi ());
 }
 
-/* Set the current output style.  This will affect future uses of the
-   _filtered output functions.  */
+/* Return true if ANSI escapes can be used on STREAM.  */
 
-static void
-set_output_style (struct ui_file *stream, const ui_file_style &style)
+static bool
+can_emit_style_escape (struct ui_file *stream)
 {
   if (stream != gdb_stdout
       || !cli_styling
-      || style == desired_style
       || !ui_file_isatty (stream))
-    return;
+    return false;
   const char *term = getenv ("TERM");
   if (term == nullptr || !strcmp (term, "dumb"))
+    return false;
+  return true;
+}
+
+/* Set the current output style.  This will affect future uses of the
+   _filtered output functions.  */
+
+static void
+set_output_style (struct ui_file *stream, const ui_file_style &style)
+{
+  if (!can_emit_style_escape (stream)
+      || style == desired_style)
     return;
 
   desired_style = style;
   emit_style_escape (style);
 }
 
+/* See utils.h.  */
+
+void
+reset_terminal_style (struct ui_file *stream)
+{
+  if (can_emit_style_escape (stream))
+    {
+      /* Force the setting, regardless of what we think the setting
+        might already be.  */
+      desired_style = ui_file_style ();
+      applied_style = desired_style;
+      wrap_buffer.append (desired_style.to_ansi ());
+    }
+}
+
 /* Wait, so the user can read what's on the screen.  Prompt the user
    to continue by pressing RETURN.  'q' is also provided because
    telling users what to do in the prompt is more user-friendly than
index 9872a15fd7f3f9e9a5e02229fa6982b34b1c8672..1f09ec2d471c3f5f7f7ed4d614f1ccbf75f01ae4 100644 (file)
@@ -439,6 +439,10 @@ extern void fputs_styled (const char *linebuffer,
                          const ui_file_style &style,
                          struct ui_file *stream);
 
+/* Reset the terminal style to the default, if needed.  */
+
+extern void reset_terminal_style (struct ui_file *stream);
+
 /* Display the host ADDR on STREAM formatted as ``0x%x''.  */
 extern void gdb_print_host_address_1 (const void *addr, struct ui_file *stream);