]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Add output styles to gdb
authorTom Tromey <tom@tromey.com>
Tue, 4 Sep 2018 04:56:33 +0000 (22:56 -0600)
committerTom Tromey <tom@tromey.com>
Fri, 28 Dec 2018 19:49:49 +0000 (12:49 -0700)
This adds some output styling to the CLI.

A style is currently a foreground color, a background color, and an
intensity (dim or bold).  (This list could be expanded depending on
terminal capabilities.)

A style can be applied while printing.  For ui-out, this is done by
passing the style constant as an argument.  For low-level cases,
fprintf_styled and fputs_styled are provided.

Users can control the style via a number of new set/show commands.  In
the interest of not typing many nearly-identical documentation
strings, I automated this.  On the down side, this is not very
i18n-friendly.

I've chose some default colors to use.  I think it would be good to
enable this by default, so that when users start the new gdb, they
will see the new feature.

Stylizing is done if TERM is set and is not "dumb".  This could be
improved when the TUI is available by using the curses has_colors
call.  That is, the lowest layer could call this without committing to
using curses everywhere; see my other patch for TUI colorizing.

I considered adding a new "set_style" method to ui_file.  However,
because the implementation had to interact with the pager code, I
didn't take this approach.  But, one idea might be to put the isatty
check there and then have it defer to the lower layers.

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

* utils.h (set_output_style, fprintf_styled)
(fputs_styled): Declare.
* utils.c (applied_style, desired_style): New globals.
(emit_style_escape, set_output_style): New function.
(prompt_for_continue): Emit style escapes.
(fputs_maybe_filtered): Likewise.
(fputs_styled, fprintf_styled): New functions.
* ui-out.h (enum class ui_out_style_kind): New.
(class ui_out) <field_string, field_stream, do_field_string>: Add
style parameter.
* ui-out.c (ui_out::field_stream, ui_out::field_string): Add style
parameter.
* tui/tui-out.h (class tui_ui_out) <do_field_string>: Add style
parameter.
* tui/tui-out.c (tui_ui_out::do_field_string): Add style
parameter.
(tui_ui_out::do_field_string): Update.
* tracepoint.c (print_one_static_tracepoint_marker): Style
output.
* stack.c (print_frame_info, print_frame): Style output.
* source.c (print_source_lines_base): Style output.
* skip.c (info_skip_command): Style output.
* record-btrace.c (btrace_call_history_src_line): Style output.
(btrace_call_history): Likewise.
* python/py-framefilter.c (py_print_frame): Style output.
* mi/mi-out.h (class mi_ui_out) <do_field_string>: Add style
parameter.
* mi/mi-out.c (mi_ui_out::do_table_header)
(mi_ui_out::do_field_int): Update.
(mi_ui_out::do_field_string): Update.
* disasm.c (gdb_pretty_print_disassembler::pretty_print_insn):
Style output.
* cli/cli-style.h: New file.
* cli/cli-style.c: New file.
* cli-out.h (class cli_ui_out) <do_field_string>: Add style
parameter.
* cli-out.c (cli_ui_out::do_table_header)
(cli_ui_out::do_field_int, cli_ui_out::do_field_skip): Update.
(cli_ui_out::do_field_string): Add style parameter.  Style the
output.
* breakpoint.c (print_breakpoint_location): Style output.
(update_static_tracepoint): Likewise.
* Makefile.in (SUBDIR_CLI_SRCS): Add cli-style.c.
(HFILES_NO_SRCDIR): Add cli-style.h.

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

* gdb.base/style.exp: New file.
* gdb.base/style.c: New file.

25 files changed:
gdb/ChangeLog
gdb/Makefile.in
gdb/breakpoint.c
gdb/cli-out.c
gdb/cli-out.h
gdb/cli/cli-style.c [new file with mode: 0644]
gdb/cli/cli-style.h [new file with mode: 0644]
gdb/disasm.c
gdb/mi/mi-out.c
gdb/mi/mi-out.h
gdb/python/py-framefilter.c
gdb/record-btrace.c
gdb/skip.c
gdb/source.c
gdb/stack.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.base/style.c [new file with mode: 0644]
gdb/testsuite/gdb.base/style.exp [new file with mode: 0644]
gdb/tracepoint.c
gdb/tui/tui-out.c
gdb/tui/tui-out.h
gdb/ui-out.c
gdb/ui-out.h
gdb/utils.c
gdb/utils.h

index 6e7e7f2ec59d30f2dc7b3bf850eed82ac53ee616..65b3ee69890465f3010f4ad1a31595b1e8991a8b 100644 (file)
@@ -1,3 +1,50 @@
+2018-12-28  Tom Tromey  <tom@tromey.com>
+
+       * utils.h (set_output_style, fprintf_styled)
+       (fputs_styled): Declare.
+       * utils.c (applied_style, desired_style): New globals.
+       (emit_style_escape, set_output_style): New function.
+       (prompt_for_continue): Emit style escapes.
+       (fputs_maybe_filtered): Likewise.
+       (fputs_styled, fprintf_styled): New functions.
+       * ui-out.h (enum class ui_out_style_kind): New.
+       (class ui_out) <field_string, field_stream, do_field_string>: Add
+       style parameter.
+       * ui-out.c (ui_out::field_stream, ui_out::field_string): Add style
+       parameter.
+       * tui/tui-out.h (class tui_ui_out) <do_field_string>: Add style
+       parameter.
+       * tui/tui-out.c (tui_ui_out::do_field_string): Add style
+       parameter.
+       (tui_ui_out::do_field_string): Update.
+       * tracepoint.c (print_one_static_tracepoint_marker): Style
+       output.
+       * stack.c (print_frame_info, print_frame): Style output.
+       * source.c (print_source_lines_base): Style output.
+       * skip.c (info_skip_command): Style output.
+       * record-btrace.c (btrace_call_history_src_line): Style output.
+       (btrace_call_history): Likewise.
+       * python/py-framefilter.c (py_print_frame): Style output.
+       * mi/mi-out.h (class mi_ui_out) <do_field_string>: Add style
+       parameter.
+       * mi/mi-out.c (mi_ui_out::do_table_header)
+       (mi_ui_out::do_field_int): Update.
+       (mi_ui_out::do_field_string): Update.
+       * disasm.c (gdb_pretty_print_disassembler::pretty_print_insn):
+       Style output.
+       * cli/cli-style.h: New file.
+       * cli/cli-style.c: New file.
+       * cli-out.h (class cli_ui_out) <do_field_string>: Add style
+       parameter.
+       * cli-out.c (cli_ui_out::do_table_header)
+       (cli_ui_out::do_field_int, cli_ui_out::do_field_skip): Update.
+       (cli_ui_out::do_field_string): Add style parameter.  Style the
+       output.
+       * breakpoint.c (print_breakpoint_location): Style output.
+       (update_static_tracepoint): Likewise.
+       * Makefile.in (SUBDIR_CLI_SRCS): Add cli-style.c.
+       (HFILES_NO_SRCDIR): Add cli-style.h.
+
 2018-12-28  Tom Tromey  <tom@tromey.com>
 
        * unittests/style-selftests.c: New file.
index ec81263ad013190108eb50ed36858e86b450c4fb..b2a1281701d65cddc6ce0987fd837790b87335e0 100644 (file)
@@ -239,6 +239,7 @@ SUBDIR_CLI_SRCS = \
        cli/cli-logging.c \
        cli/cli-script.c \
        cli/cli-setshow.c \
+       cli/cli-style.c \
        cli/cli-utils.c
 
 SUBDIR_CLI_OBS = $(patsubst %.c,%.o,$(SUBDIR_CLI_SRCS))
@@ -1429,6 +1430,7 @@ HFILES_NO_SRCDIR = \
        cli/cli-decode.h \
        cli/cli-script.h \
        cli/cli-setshow.h \
+       cli/cli-style.h \
        cli/cli-utils.h \
        common/buffer.h \
        common/cleanups.h \
index 8af3d54a77e7bd7bc207fb5085d433b54eb441a7..53763e0254cf5be16a5ed8ae213c22998fc8d9dd 100644 (file)
@@ -5860,13 +5860,15 @@ print_breakpoint_location (struct breakpoint *b,
       if (sym)
        {
          uiout->text ("in ");
-         uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
+         uiout->field_string ("func", SYMBOL_PRINT_NAME (sym),
+                              ui_out_style_kind::FUNCTION);
          uiout->text (" ");
          uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
          uiout->text ("at ");
        }
       uiout->field_string ("file",
-                          symtab_to_filename_for_display (loc->symtab));
+                          symtab_to_filename_for_display (loc->symtab),
+                          ui_out_style_kind::FILE);
       uiout->text (":");
 
       if (uiout->is_mi_like_p ())
@@ -13427,11 +13429,13 @@ update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
          uiout->text ("Now in ");
          if (sym)
            {
-             uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
+             uiout->field_string ("func", SYMBOL_PRINT_NAME (sym),
+                                  ui_out_style_kind::FUNCTION);
              uiout->text (" at ");
            }
          uiout->field_string ("file",
-                              symtab_to_filename_for_display (sal2.symtab));
+                              symtab_to_filename_for_display (sal2.symtab),
+                              ui_out_style_kind::FILE);
          uiout->text (":");
 
          if (uiout->is_mi_like_p ())
index 7e3ee3e54c1a6bf6cce7c2f4fd783f142b8bcc71..4b5fc173904b493a5ba79f12bfb977b0e2105fb5 100644 (file)
@@ -25,6 +25,7 @@
 #include "cli-out.h"
 #include "completer.h"
 #include "readline/readline.h"
+#include "cli/cli-style.h"
 
 /* These are the CLI output functions */
 
@@ -71,7 +72,8 @@ cli_ui_out::do_table_header (int width, ui_align alignment,
   if (m_suppress_output)
     return;
 
-  do_field_string (0, width, alignment, 0, col_hdr.c_str ());
+  do_field_string (0, width, alignment, 0, col_hdr.c_str (),
+                  ui_out_style_kind::DEFAULT);
 }
 
 /* Mark beginning of a list */
@@ -99,7 +101,8 @@ cli_ui_out::do_field_int (int fldno, int width, ui_align alignment,
 
   std::string str = string_printf ("%d", value);
 
-  do_field_string (fldno, width, alignment, fldname, str.c_str ());
+  do_field_string (fldno, width, alignment, fldname, str.c_str (),
+                  ui_out_style_kind::DEFAULT);
 }
 
 /* used to omit a field */
@@ -111,7 +114,8 @@ cli_ui_out::do_field_skip (int fldno, int width, ui_align alignment,
   if (m_suppress_output)
     return;
 
-  do_field_string (fldno, width, alignment, fldname, "");
+  do_field_string (fldno, width, alignment, fldname, "",
+                  ui_out_style_kind::DEFAULT);
 }
 
 /* other specific cli_field_* end up here so alignment and field
@@ -119,7 +123,8 @@ cli_ui_out::do_field_skip (int fldno, int width, ui_align alignment,
 
 void
 cli_ui_out::do_field_string (int fldno, int width, ui_align align,
-                            const char *fldname, const char *string)
+                            const char *fldname, const char *string,
+                            ui_out_style_kind style)
 {
   int before = 0;
   int after = 0;
@@ -154,7 +159,25 @@ cli_ui_out::do_field_string (int fldno, int width, ui_align align,
     spaces (before);
 
   if (string)
-    fputs_filtered (string, m_streams.back ());
+    {
+      ui_file_style fstyle;
+      switch (style)
+       {
+       case ui_out_style_kind::DEFAULT:
+         /* Nothing.  */
+         break;
+       case ui_out_style_kind::FILE:
+         /* Nothing.  */
+         fstyle = file_name_style.style ();
+         break;
+       case ui_out_style_kind::FUNCTION:
+         fstyle = function_name_style.style ();
+         break;
+       default:
+         gdb_assert_not_reached ("missing case");
+       }
+      fputs_styled (string, fstyle, m_streams.back ());
+    }
 
   if (after)
     spaces (after);
@@ -175,7 +198,8 @@ cli_ui_out::do_field_fmt (int fldno, int width, ui_align align,
 
   std::string str = string_vprintf (format, args);
 
-  do_field_string (fldno, width, align, fldname, str.c_str ());
+  do_field_string (fldno, width, align, fldname, str.c_str (),
+                  ui_out_style_kind::DEFAULT);
 }
 
 void
index f701da7bf382e3612f6e875871680a50eaeb42b2..4fe10a40a2f2ab2842cc5881148b81042bdac393 100644 (file)
@@ -51,7 +51,8 @@ protected:
                              const char *fldname) override;
   virtual void do_field_string (int fldno, int width, ui_align align,
                                const char *fldname,
-                               const char *string) override;
+                               const char *string,
+                               ui_out_style_kind style) override;
   virtual void do_field_fmt (int fldno, int width, ui_align align,
                             const char *fldname, const char *format,
                             va_list args)
diff --git a/gdb/cli/cli-style.c b/gdb/cli/cli-style.c
new file mode 100644 (file)
index 0000000..9f16824
--- /dev/null
@@ -0,0 +1,257 @@
+/* CLI colorizing
+
+   Copyright (C) 2018 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "defs.h"
+#include "cli/cli-cmds.h"
+#include "cli/cli-style.h"
+
+/* True if styling is enabled.  */
+
+#if defined(_WIN32) || defined (__CYGWIN__)
+int cli_styling = 0;
+#else
+int cli_styling = 1;
+#endif
+
+/* Name of colors; must correspond to ui_file_style::basic_color.  */
+static const char * const cli_colors[] = {
+  "none",
+  "black",
+  "red",
+  "green",
+  "yellow",
+  "blue",
+  "magenta",
+  "cyan",
+  "white",
+  nullptr
+};
+
+/* Names of intensities; must correspond to
+   ui_file_style::intensity.  */
+static const char * const cli_intensities[] = {
+  "normal",
+  "bold",
+  "dim",
+  nullptr
+};
+
+/* See cli-style.h.  */
+
+cli_style_option file_name_style (ui_file_style::GREEN);
+
+/* See cli-style.h.  */
+
+cli_style_option function_name_style (ui_file_style::YELLOW);
+
+/* See cli-style.h.  */
+
+cli_style_option::cli_style_option (ui_file_style::basic_color fg)
+  : m_foreground (cli_colors[fg - ui_file_style::NONE]),
+    m_background (cli_colors[0]),
+    m_intensity (cli_intensities[ui_file_style::NORMAL])
+{
+}
+
+/* Return the color number corresponding to COLOR.  */
+
+static int
+color_number (const char *color)
+{
+  for (int i = 0; i < ARRAY_SIZE (cli_colors); ++i)
+    {
+      if (color == cli_colors[i])
+       return i - 1;
+    }
+  gdb_assert_not_reached ("color not found");
+}
+
+/* See cli-style.h.  */
+
+ui_file_style
+cli_style_option::style () const
+{
+  int fg = color_number (m_foreground);
+  int bg = color_number (m_background);
+  ui_file_style::intensity intensity = ui_file_style::NORMAL;
+
+  for (int i = 0; i < ARRAY_SIZE (cli_intensities); ++i)
+    {
+      if (m_intensity == cli_intensities[i])
+       {
+         intensity = (ui_file_style::intensity) i;
+         break;
+       }
+    }
+
+  return ui_file_style (fg, bg, intensity);
+}
+
+/* See cli-style.h.  */
+
+void
+cli_style_option::do_set (const char *args, int from_tty)
+{
+}
+
+/* See cli-style.h.  */
+
+void
+cli_style_option::do_show (const char *args, int from_tty)
+{
+}
+
+/* See cli-style.h.  */
+
+void
+cli_style_option::do_show_foreground (struct ui_file *file, int from_tty,
+                                     struct cmd_list_element *cmd,
+                                     const char *value)
+{
+  const char *name = (const char *) get_cmd_context (cmd);
+  fprintf_filtered (file, _("The \"%s\" foreground color is: %s\n"),
+                   name, value);
+}
+
+/* See cli-style.h.  */
+
+void
+cli_style_option::do_show_background (struct ui_file *file, int from_tty,
+                                     struct cmd_list_element *cmd,
+                                     const char *value)
+{
+  const char *name = (const char *) get_cmd_context (cmd);
+  fprintf_filtered (file, _("The \"%s\" background color is: %s\n"),
+                   name, value);
+}
+
+/* See cli-style.h.  */
+
+void
+cli_style_option::do_show_intensity (struct ui_file *file, int from_tty,
+                                    struct cmd_list_element *cmd,
+                                    const char *value)
+{
+  const char *name = (const char *) get_cmd_context (cmd);
+  fprintf_filtered (file, _("The \"%s\" display intensity is: %s\n"),
+                   name, value);
+}
+
+/* See cli-style.h.  */
+
+void
+cli_style_option::add_setshow_commands (const char *name,
+                                       enum command_class theclass,
+                                       const char *prefix_doc,
+                                       const char *prefixname,
+                                       struct cmd_list_element **set_list,
+                                       struct cmd_list_element **show_list)
+{
+  m_show_prefix = std::string ("set ") + prefixname + " ";
+  m_show_prefix = std::string ("show ") + prefixname + " ";
+
+  add_prefix_cmd (name, no_class, do_set, prefix_doc, &m_set_list,
+                 m_show_prefix.c_str (), 0, set_list);
+  add_prefix_cmd (name, no_class, do_show, prefix_doc, &m_show_list,
+                 m_set_prefix.c_str (), 0, show_list);
+
+  add_setshow_enum_cmd ("foreground", theclass, cli_colors,
+                       &m_foreground,
+                       _("Set the foreground color for this property"),
+                       _("Show the foreground color for this property"),
+                       nullptr,
+                       nullptr,
+                       do_show_foreground,
+                       &m_set_list, &m_show_list, (void *) name);
+  add_setshow_enum_cmd ("background", theclass, cli_colors,
+                       &m_background,
+                       _("Set the background color for this property"),
+                       _("Show the background color for this property"),
+                       nullptr,
+                       nullptr,
+                       do_show_background,
+                       &m_set_list, &m_show_list, (void *) name);
+  add_setshow_enum_cmd ("intensity", theclass, cli_intensities,
+                       &m_intensity,
+                       _("Set the display intensity color for this property"),
+                       _("\
+Show the display intensity color for this property"),
+                       nullptr,
+                       nullptr,
+                       do_show_intensity,
+                       &m_set_list, &m_show_list, (void *) name);
+}
+
+static void
+set_style (const char *arg, int from_tty)
+{
+}
+
+static void
+show_style (const char *arg, int from_tty)
+{
+}
+
+static void
+show_style_enabled (struct ui_file *file, int from_tty,
+                   struct cmd_list_element *c, const char *value)
+{
+  if (cli_styling)
+    fprintf_filtered (file, _("CLI output styling is enabled.\n"));
+  else
+    fprintf_filtered (file, _("CLI output styling is disabled.\n"));
+}
+
+void
+_initialize_cli_style ()
+{
+  static cmd_list_element *style_set_list;
+  static cmd_list_element *style_show_list;
+
+  add_prefix_cmd ("style", no_class, set_style, _("\
+Style-specific settings\n\
+Configure various style-related variables, such as colors"),
+                 &style_set_list, "set style ", 0, &setlist);
+  add_prefix_cmd ("style", no_class, show_style, _("\
+Style-specific settings\n\
+Configure various style-related variables, such as colors"),
+                 &style_show_list, "show style ", 0, &showlist);
+
+  add_setshow_boolean_cmd ("enabled", no_class, &cli_styling, _("\
+Set whether CLI styling is enabled."), _("\
+Show whether CLI is enabled."), _("\
+If enabled, output to the terminal is styled."),
+                          NULL, show_style_enabled,
+                          &style_set_list, &style_show_list);
+
+  file_name_style.add_setshow_commands ("filename", no_class,
+                                       _("\
+Filename display styling\n\
+Configure filename colors and display intensity."),
+                                       "style filename",
+                                       &style_set_list,
+                                       &style_show_list);
+  function_name_style.add_setshow_commands ("function", no_class,
+                                           _("\
+Function name display styling\n\
+Configure function name colors and display intensity"),
+                                           "style function",
+                                           &style_set_list,
+                                           &style_show_list);
+}
diff --git a/gdb/cli/cli-style.h b/gdb/cli/cli-style.h
new file mode 100644 (file)
index 0000000..7d5370c
--- /dev/null
@@ -0,0 +1,89 @@
+/* CLI stylizing
+
+   Copyright (C) 2018 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef CLI_STYLE_H
+#define CLI_STYLE_H
+
+#include "ui-file.h"
+
+/* A single CLI style option.  */
+class cli_style_option
+{
+public:
+
+  /* Construct a CLI style option with a foreground color.  */
+  cli_style_option (ui_file_style::basic_color fg);
+
+  /* Return a ui_file_style corresponding to the settings in this CLI
+     style.  */
+  ui_file_style style () const;
+
+  /* Call once to register this CLI style with the CLI engine.  */
+  void add_setshow_commands (const char *name,
+                            enum command_class theclass,
+                            const char *prefix_doc,
+                            const char *prefixname,
+                            struct cmd_list_element **set_list,
+                            struct cmd_list_element **show_list);
+
+private:
+
+  /* The foreground.  */
+  const char *m_foreground;
+  /* The background.  */
+  const char *m_background;
+  /* The intensity.  */
+  const char *m_intensity;
+
+  /* Storage for prefixes needed when registering the commands.  */
+  std::string m_show_prefix;
+  std::string m_set_prefix;
+  /* Storage for command lists needed when registering
+     subcommands.  */
+  struct cmd_list_element *m_set_list = nullptr;
+  struct cmd_list_element *m_show_list = nullptr;
+
+  /* Callback to set a value.  */
+  static void do_set (const char *args, int from_tty);
+  /* Callback to show a value.  */
+  static void do_show (const char *args, int from_tty);
+  /* Callback to show the foreground.  */
+  static void do_show_foreground (struct ui_file *file, int from_tty,
+                                 struct cmd_list_element *cmd,
+                                 const char *value);
+  /* Callback to show the background.  */
+  static void do_show_background (struct ui_file *file, int from_tty,
+                                 struct cmd_list_element *cmd,
+                                 const char *value);
+  /* Callback to show the intensity.  */
+  static void do_show_intensity (struct ui_file *file, int from_tty,
+                                struct cmd_list_element *cmd,
+                                const char *value);
+};
+
+/* The file name style.  */
+extern cli_style_option file_name_style;
+
+/* The function name style.  */
+extern cli_style_option function_name_style;
+
+/* True if styling is enabled.  */
+extern int cli_styling;
+
+#endif /* CLI_STYLE_H */
index 128c3abfde4c6a2349b5d736a35f91c1ef8db6c5..5e016be79f2149f3374db2f7ee0ccd70737ec9a7 100644 (file)
@@ -244,7 +244,8 @@ gdb_pretty_print_disassembler::pretty_print_insn (struct ui_out *uiout,
           the future.  */
        uiout->text (" <");
        if ((flags & DISASSEMBLY_OMIT_FNAME) == 0)
-         uiout->field_string ("func-name", name.c_str ());
+         uiout->field_string ("func-name", name.c_str (),
+                              ui_out_style_kind::FUNCTION);
        uiout->text ("+");
        uiout->field_int ("offset", offset);
        uiout->text (">:\t");
index 39b18b03a0197e699f401413b71b219385a7adf3..4aa4a5c601e1b678cdf93d54808f8241806c0444 100644 (file)
@@ -65,8 +65,10 @@ mi_ui_out::do_table_header (int width, ui_align alignment,
   open (NULL, ui_out_type_tuple);
   do_field_int (0, 0, ui_center, "width", width);
   do_field_int (0, 0, ui_center, "alignment", alignment);
-  do_field_string (0, 0, ui_center, "col_name", col_name.c_str ());
-  do_field_string (0, width, alignment, "colhdr", col_hdr.c_str ());
+  do_field_string (0, 0, ui_center, "col_name", col_name.c_str (),
+                  ui_out_style_kind::DEFAULT);
+  do_field_string (0, width, alignment, "colhdr", col_hdr.c_str (),
+                  ui_out_style_kind::DEFAULT);
   close (ui_out_type_tuple);
 }
 
@@ -95,7 +97,8 @@ mi_ui_out::do_field_int (int fldno, int width, ui_align alignment,
   char buffer[20];     /* FIXME: how many chars long a %d can become? */
 
   xsnprintf (buffer, sizeof (buffer), "%d", value);
-  do_field_string (fldno, width, alignment, fldname, buffer);
+  do_field_string (fldno, width, alignment, fldname, buffer,
+                  ui_out_style_kind::DEFAULT);
 }
 
 /* Used to omit a field.  */
@@ -111,7 +114,8 @@ mi_ui_out::do_field_skip (int fldno, int width, ui_align alignment,
 
 void
 mi_ui_out::do_field_string (int fldno, int width, ui_align align,
-                           const char *fldname, const char *string)
+                           const char *fldname, const char *string,
+                           ui_out_style_kind style)
 {
   ui_file *stream = m_streams.back ();
   field_separator ();
index 89ff88cd3271b8f9052bf75c826db3f43a42266c..b576263d2bde6c4d33fa51991d660119b34fa893 100644 (file)
@@ -57,7 +57,8 @@ protected:
   virtual void do_field_skip (int fldno, int width, ui_align align,
                           const char *fldname) override;
   virtual void do_field_string (int fldno, int width, ui_align align,
-                            const char *fldname, const char *string) override;
+                               const char *fldname, const char *string,
+                               ui_out_style_kind style) override;
   virtual void do_field_fmt (int fldno, int width, ui_align align,
                          const char *fldname, const char *format, va_list args)
     override ATTRIBUTE_PRINTF (6,0);
index b4bc41306bfdc079613a9523e224dc1ce5fe7d68..54426f9a722df8e7268ac612d49eec9983f376b8 100644 (file)
@@ -898,7 +898,7 @@ py_print_frame (PyObject *filter, frame_filter_flags flags,
          if (function == NULL)
            out->field_skip ("func");
          else
-           out->field_string ("func", function);
+           out->field_string ("func", function, ui_out_style_kind::FUNCTION);
        }
     }
 
@@ -934,7 +934,8 @@ py_print_frame (PyObject *filter, frame_filter_flags flags,
              out->wrap_hint ("   ");
              out->text (" at ");
              annotate_frame_source_file ();
-             out->field_string ("file", filename.get ());
+             out->field_string ("file", filename.get (),
+                                ui_out_style_kind::FILE);
              annotate_frame_source_file_end ();
            }
        }
index 92c0821ec924332f370f346066759b0555e76294..3b6709649399a4ec8c2486cebb6fefe7c34b3fac 100644 (file)
@@ -1090,7 +1090,8 @@ btrace_call_history_src_line (struct ui_out *uiout,
     return;
 
   uiout->field_string ("file",
-                      symtab_to_filename_for_display (symbol_symtab (sym)));
+                      symtab_to_filename_for_display (symbol_symtab (sym)),
+                      ui_out_style_kind::FILE);
 
   btrace_compute_src_line_range (bfun, &begin, &end);
   if (end < begin)
@@ -1181,11 +1182,14 @@ btrace_call_history (struct ui_out *uiout,
        }
 
       if (sym != NULL)
-       uiout->field_string ("function", SYMBOL_PRINT_NAME (sym));
+       uiout->field_string ("function", SYMBOL_PRINT_NAME (sym),
+                            ui_out_style_kind::FUNCTION);
       else if (msym != NULL)
-       uiout->field_string ("function", MSYMBOL_PRINT_NAME (msym));
+       uiout->field_string ("function", MSYMBOL_PRINT_NAME (msym),
+                            ui_out_style_kind::FUNCTION);
       else if (!uiout->is_mi_like_p ())
-       uiout->field_string ("function", "??");
+       uiout->field_string ("function", "??",
+                            ui_out_style_kind::FUNCTION);
 
       if ((flags & RECORD_PRINT_INSN_RANGE) != 0)
        {
index 77373e85e6552a181d58c86d5b1bf65e474fb1bc..a8f8ee19628ec6cee3d3ed2bdf5b6b4403b0a518 100644 (file)
@@ -413,7 +413,8 @@ info_skip_command (const char *arg, int from_tty)
 
       current_uiout->field_string ("file",
                                   e.file ().empty () ? "<none>"
-                                  : e.file ().c_str ()); /* 4 */
+                                  : e.file ().c_str (),
+                                  ui_out_style_kind::FILE); /* 4 */
       if (e.function_is_regexp ())
        current_uiout->field_string ("regexp", "y"); /* 5 */
       else
@@ -421,7 +422,8 @@ info_skip_command (const char *arg, int from_tty)
 
       current_uiout->field_string ("function",
                                   e.function ().empty () ? "<none>"
-                                  : e.function ().c_str ()); /* 6 */
+                                  : e.function ().c_str (),
+                                  ui_out_style_kind::FUNCTION); /* 6 */
 
       current_uiout->text ("\n");
     }
index 952fc3f889fa4db02ab7ddcd7541cfd960964e71..575e46c2123e66a58eb20f3f227b881601e421cd 100644 (file)
@@ -1320,7 +1320,8 @@ print_source_lines_base (struct symtab *s, int line, int stopline,
             MI expects both fields.  ui_source_list is set only for CLI,
             not for TUI.  */
          if (uiout->is_mi_like_p () || uiout->test_flags (ui_source_list))
-           uiout->field_string ("file", symtab_to_filename_for_display (s));
+           uiout->field_string ("file", symtab_to_filename_for_display (s),
+                                ui_out_style_kind::FILE);
          if (uiout->is_mi_like_p () || !uiout->test_flags (ui_source_list))
            {
              const char *s_fullname = symtab_to_fullname (s);
index f34d7b2a17e835fe4f2a598aa167cca3ca329fc9..b32bf8f4bfbca113afe82e1134c719ef5df0294f 100644 (file)
@@ -824,16 +824,19 @@ print_frame_info (struct frame_info *frame, int print_level,
       if (get_frame_type (frame) == DUMMY_FRAME)
         {
           annotate_function_call ();
-          uiout->field_string ("func", "<function called from gdb>");
+          uiout->field_string ("func", "<function called from gdb>",
+                              ui_out_style_kind::FUNCTION);
        }
       else if (get_frame_type (frame) == SIGTRAMP_FRAME)
         {
          annotate_signal_handler_caller ();
-          uiout->field_string ("func", "<signal handler called>");
+          uiout->field_string ("func", "<signal handler called>",
+                              ui_out_style_kind::FUNCTION);
         }
       else if (get_frame_type (frame) == ARCH_FRAME)
         {
-          uiout->field_string ("func", "<cross-architecture call>");
+          uiout->field_string ("func", "<cross-architecture call>",
+                              ui_out_style_kind::FUNCTION);
        }
       uiout->text ("\n");
       annotate_frame_end ();
@@ -1182,10 +1185,10 @@ print_frame (struct frame_info *frame, int print_level,
     string_file stb;
     fprintf_symbol_filtered (&stb, funname ? funname.get () : "??",
                             funlang, DMGL_ANSI);
-    uiout->field_stream ("func", stb);
+    uiout->field_stream ("func", stb, ui_out_style_kind::FUNCTION);
     uiout->wrap_hint ("   ");
     annotate_frame_args ();
-      
+
     uiout->text (" (");
     if (print_args)
       {
@@ -1225,7 +1228,7 @@ print_frame (struct frame_info *frame, int print_level,
        uiout->wrap_hint ("   ");
        uiout->text (" at ");
        annotate_frame_source_file ();
-       uiout->field_string ("file", filename_display);
+       uiout->field_string ("file", filename_display, ui_out_style_kind::FILE);
        if (uiout->is_mi_like_p ())
          {
            const char *fullname = symtab_to_fullname (sal.symtab);
index 951ad77e99fae6af52122857fb52573071ae51c9..6b3ca5adf68dd92e4890d53a4178e300e731b2f2 100644 (file)
@@ -1,3 +1,8 @@
+2018-12-28  Tom Tromey  <tom@tromey.com>
+
+       * gdb.base/style.exp: New file.
+       * gdb.base/style.c: New file.
+
 2018-12-28  Tom Tromey  <tom@tromey.com>
 
        * lib/gdb.exp (gdb_init): Set the TERM environment variable to
diff --git a/gdb/testsuite/gdb.base/style.c b/gdb/testsuite/gdb.base/style.c
new file mode 100644 (file)
index 0000000..58fc243
--- /dev/null
@@ -0,0 +1,20 @@
+/* Copyright 2018 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+int
+main (int argc, char **argv)
+{
+  return 0; /* break here */
+}
diff --git a/gdb/testsuite/gdb.base/style.exp b/gdb/testsuite/gdb.base/style.exp
new file mode 100644 (file)
index 0000000..20b7b8c
--- /dev/null
@@ -0,0 +1,41 @@
+# Copyright 2018 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test CLI output styling.
+
+standard_testfile
+
+save_vars { env(TERM) } {
+    # We need an ANSI-capable terminal to get the output.
+    setenv TERM ansi
+
+    if {[prepare_for_testing "failed to prepare" $testfile $srcfile]} {
+       return -1
+    }
+
+    if {![runto_main]} {
+       fail "style tests failed"
+       return
+    }
+
+    gdb_test_no_output "set style enabled on"
+
+    set main_expr "\033\\\[33mmain\033\\\[m"
+    set file_expr "\033\\\[32m.*style\\.c\033\\\[m:\[0-9\]"
+
+    gdb_test "frame" \
+       "$main_expr.*$file_expr.*"
+    gdb_test "info breakpoints" "$main_expr at $file_expr.*"
+}
index 8cd53374f91c34cf82c6075e08a139081012cec4..5c1111297cc3b555b444804524926649259984cb 100644 (file)
@@ -3684,8 +3684,8 @@ print_one_static_tracepoint_marker (int count,
   if (sym)
     {
       uiout->text ("in ");
-      uiout->field_string ("func",
-                          SYMBOL_PRINT_NAME (sym));
+      uiout->field_string ("func", SYMBOL_PRINT_NAME (sym),
+                          ui_out_style_kind::FUNCTION);
       uiout->wrap_hint (wrap_indent);
       uiout->text (" at ");
     }
@@ -3695,7 +3695,8 @@ print_one_static_tracepoint_marker (int count,
   if (sal.symtab != NULL)
     {
       uiout->field_string ("file",
-                          symtab_to_filename_for_display (sal.symtab));
+                          symtab_to_filename_for_display (sal.symtab),
+                          ui_out_style_kind::FILE);
       uiout->text (":");
 
       if (uiout->is_mi_like_p ())
index 6d4e2363f353b17b4ccda2549d560fc974cb74d9..29d7bfa84ba4439a1dc8da9b37828027e1c5970a 100644 (file)
@@ -51,7 +51,8 @@ tui_ui_out::do_field_int (int fldno, int width, ui_align alignment,
 
 void
 tui_ui_out::do_field_string (int fldno, int width, ui_align align,
-                            const char *fldname, const char *string)
+                            const char *fldname, const char *string,
+                            ui_out_style_kind style)
 {
   if (suppress_output ())
     return;
@@ -68,7 +69,7 @@ tui_ui_out::do_field_string (int fldno, int width, ui_align align,
   
   m_start_of_line++;
 
-  cli_ui_out::do_field_string (fldno, width, align, fldname, string);
+  cli_ui_out::do_field_string (fldno, width, align, fldname, string, style);
 }
 
 void
index d191c6581084f07c401ade35c79d3a90988da5df..a4903eef611410bf02ff83760eca7debb9552978 100644 (file)
@@ -31,7 +31,7 @@ protected:
   void do_field_int (int fldno, int width, ui_align align, const char *fldname,
                  int value) override;
   void do_field_string (int fldno, int width, ui_align align, const char *fldname,
-                    const char *string) override;
+                       const char *string, ui_out_style_kind style) override;
   void do_field_fmt (int fldno, int width, ui_align align, const char *fldname,
                  const char *format, va_list args) override
     ATTRIBUTE_PRINTF (6,0);
index 3d74f785434b6e29701c70204c3709bd22306cd2..761d6c0745865a5d36d54a2f2bb783f9400c9bcb 100644 (file)
@@ -473,10 +473,11 @@ ui_out::field_core_addr (const char *fldname, struct gdbarch *gdbarch,
 }
 
 void
-ui_out::field_stream (const char *fldname, string_file &stream)
+ui_out::field_stream (const char *fldname, string_file &stream,
+                     ui_out_style_kind style)
 {
   if (!stream.empty ())
-    field_string (fldname, stream.c_str ());
+    field_string (fldname, stream.c_str (), style);
   else
     field_skip (fldname);
   stream.clear ();
@@ -497,7 +498,8 @@ ui_out::field_skip (const char *fldname)
 }
 
 void
-ui_out::field_string (const char *fldname, const char *string)
+ui_out::field_string (const char *fldname, const char *string,
+                     ui_out_style_kind style)
 {
   int fldno;
   int width;
@@ -505,7 +507,7 @@ ui_out::field_string (const char *fldname, const char *string)
 
   verify_field (&fldno, &width, &align);
 
-  do_field_string (fldno, width, align, fldname, string);
+  do_field_string (fldno, width, align, fldname, string, style);
 }
 
 void
index 81e2e0b20db27c027ea59419e5d232b5b688d98e..8604105c09ae621b8dbf2dacc8c2664f3891210a 100644 (file)
@@ -66,6 +66,18 @@ enum ui_out_type
     ui_out_type_list
   };
 
+/* Possible kinds of styling.  */
+
+enum class ui_out_style_kind
+{
+  /* The default (plain) style.  */
+  DEFAULT,
+  /* File name.  */
+  FILE,
+  /* Function name.  */
+  FUNCTION
+};
+
 class ui_out
 {
  public:
@@ -95,9 +107,11 @@ class ui_out
                      int value);
   void field_core_addr (const char *fldname, struct gdbarch *gdbarch,
                        CORE_ADDR address);
-  void field_string (const char *fldname, const char *string);
+  void field_string (const char *fldname, const char *string,
+                    ui_out_style_kind style = ui_out_style_kind::DEFAULT);
   void field_string (const char *fldname, const std::string &string);
-  void field_stream (const char *fldname, string_file &stream);
+  void field_stream (const char *fldname, string_file &stream,
+                    ui_out_style_kind style = ui_out_style_kind::DEFAULT);
   void field_skip (const char *fldname);
   void field_fmt (const char *fldname, const char *format, ...)
     ATTRIBUTE_PRINTF (3, 4);
@@ -141,7 +155,8 @@ class ui_out
   virtual void do_field_skip (int fldno, int width, ui_align align,
                              const char *fldname) = 0;
   virtual void do_field_string (int fldno, int width, ui_align align,
-                               const char *fldname, const char *string) = 0;
+                               const char *fldname, const char *string,
+                               ui_out_style_kind style) = 0;
   virtual void do_field_fmt (int fldno, int width, ui_align align,
                             const char *fldname, const char *format,
                             va_list args)
index 2019e20b0cda0e88e142bd0c3acd9dc363f4845d..2fb7476a35ac14ec5a90d217daad94bb1f369371 100644 (file)
@@ -71,6 +71,7 @@
 #include "cp-support.h"
 #include <algorithm>
 #include "common/pathstuff.h"
+#include "cli/cli-style.h"
 
 void (*deprecated_error_begin_hook) (void);
 
@@ -1422,6 +1423,46 @@ set_screen_width_and_height (int width, int height)
   set_width ();
 }
 
+/* The currently applied style.  */
+
+static ui_file_style applied_style;
+
+/* The currently desired style.  This can differ from the applied
+   style when showing the pagination prompt.  */
+
+static ui_file_style desired_style;
+
+/* Emit an ANSI style escape for STYLE to the wrap buffer.  */
+
+static void
+emit_style_escape (const ui_file_style &style)
+{
+  if (applied_style == style)
+    return;
+  applied_style = style;
+
+  wrap_buffer.append (style.to_ansi ());
+}
+
+/* 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 (stream != gdb_stdout
+      || !cli_styling
+      || style == desired_style
+      || !ui_file_isatty (stream))
+    return;
+  const char *term = getenv ("TERM");
+  if (term == nullptr || !strcmp (term, "dumb"))
+    return;
+
+  desired_style = style;
+  emit_style_escape (style);
+}
+
 /* 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
@@ -1437,6 +1478,9 @@ prompt_for_continue (void)
   steady_clock::time_point prompt_started = steady_clock::now ();
   bool disable_pagination = pagination_disabled_for_command;
 
+  /* Clear the current styling.  */
+  emit_style_escape (ui_file_style ());
+
   if (annotation_level > 1)
     printf_unfiltered (("\n\032\032pre-prompt-for-continue\n"));
 
@@ -1481,6 +1525,9 @@ prompt_for_continue (void)
   reinitialize_more_filter ();
   pagination_disabled_for_command = disable_pagination;
 
+  /* Restore the current styling.  */
+  emit_style_escape (desired_style);
+
   dont_repeat ();              /* Forget prev cmd -- CR won't repeat it.  */
 }
 
@@ -1714,7 +1761,11 @@ fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream,
                 if chars_per_line is right, we probably just overflowed
                 anyway; if it's wrong, let us keep going.  */
              if (wrap_column)
-               fputc_unfiltered ('\n', stream);
+               {
+                 emit_style_escape (ui_file_style ());
+                 flush_wrap_buffer (stream);
+                 fputc_unfiltered ('\n', stream);
+               }
 
              /* Possible new page.  Note that
                 PAGINATION_DISABLED_FOR_COMMAND might be set during
@@ -1727,6 +1778,7 @@ fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream,
              if (wrap_column)
                {
                  fputs_unfiltered (wrap_indent, stream);
+                 emit_style_escape (desired_style);
                  flush_wrap_buffer (stream);
                  /* FIXME, this strlen is what prevents wrap_indent from
                     containing tabs.  However, if we recurse to print it
@@ -1759,6 +1811,17 @@ fputs_filtered (const char *linebuffer, struct ui_file *stream)
   fputs_maybe_filtered (linebuffer, stream, 1);
 }
 
+/* See utils.h.  */
+
+void
+fputs_styled (const char *linebuffer, const ui_file_style &style,
+             struct ui_file *stream)
+{
+  set_output_style (stream, style);
+  fputs_maybe_filtered (linebuffer, stream, 1);
+  set_output_style (stream, ui_file_style ());
+}
+
 int
 putchar_unfiltered (int c)
 {
@@ -1986,6 +2049,21 @@ fprintfi_filtered (int spaces, struct ui_file *stream, const char *format,
   va_end (args);
 }
 
+/* See utils.h.  */
+
+void
+fprintf_styled (struct ui_file *stream, const ui_file_style &style,
+               const char *format, ...)
+{
+  va_list args;
+
+  set_output_style (stream, style);
+  va_start (args, format);
+  vfprintf_filtered (stream, format, args);
+  va_end (args);
+  set_output_style (stream, ui_file_style ());
+}
+
 
 void
 printf_filtered (const char *format, ...)
index 08a29af1dc3679cb461a3213594bd9d8fa8683c9..9872a15fd7f3f9e9a5e02229fa6982b34b1c8672 100644 (file)
@@ -423,6 +423,22 @@ extern void fputstrn_unfiltered (const char *str, int n, int quotr,
 /* Return nonzero if filtered printing is initialized.  */
 extern int filtered_printing_initialized (void);
 
+/* Like fprintf_filtered, but styles the output according to STYLE,
+   when appropriate.  */
+
+extern void fprintf_styled (struct ui_file *stream,
+                           const ui_file_style &style,
+                           const char *fmt,
+                           ...)
+  ATTRIBUTE_PRINTF (3, 4);
+
+/* Like fputs_filtered, but styles the output according to STYLE, when
+   appropriate.  */
+
+extern void fputs_styled (const char *linebuffer,
+                         const ui_file_style &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);