Home | History | Annotate | Line # | Download | only in cli
cli-style.c revision 1.1.1.2
      1      1.1  christos /* CLI colorizing
      2      1.1  christos 
      3  1.1.1.2  christos    Copyright (C) 2018-2020 Free Software Foundation, Inc.
      4      1.1  christos 
      5      1.1  christos    This file is part of GDB.
      6      1.1  christos 
      7      1.1  christos    This program is free software; you can redistribute it and/or modify
      8      1.1  christos    it under the terms of the GNU General Public License as published by
      9      1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10      1.1  christos    (at your option) any later version.
     11      1.1  christos 
     12      1.1  christos    This program is distributed in the hope that it will be useful,
     13      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15      1.1  christos    GNU General Public License for more details.
     16      1.1  christos 
     17      1.1  christos    You should have received a copy of the GNU General Public License
     18      1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19      1.1  christos 
     20      1.1  christos #include "defs.h"
     21      1.1  christos #include "cli/cli-cmds.h"
     22      1.1  christos #include "cli/cli-style.h"
     23      1.1  christos #include "source-cache.h"
     24      1.1  christos #include "observable.h"
     25      1.1  christos 
     26      1.1  christos /* True if styling is enabled.  */
     27      1.1  christos 
     28  1.1.1.2  christos #if defined (__MSDOS__)
     29  1.1.1.2  christos bool cli_styling = false;
     30      1.1  christos #else
     31  1.1.1.2  christos bool cli_styling = true;
     32      1.1  christos #endif
     33      1.1  christos 
     34      1.1  christos /* True if source styling is enabled.  Note that this is only
     35      1.1  christos    consulted when cli_styling is true.  */
     36      1.1  christos 
     37  1.1.1.2  christos bool source_styling = true;
     38      1.1  christos 
     39      1.1  christos /* Name of colors; must correspond to ui_file_style::basic_color.  */
     40      1.1  christos static const char * const cli_colors[] = {
     41      1.1  christos   "none",
     42      1.1  christos   "black",
     43      1.1  christos   "red",
     44      1.1  christos   "green",
     45      1.1  christos   "yellow",
     46      1.1  christos   "blue",
     47      1.1  christos   "magenta",
     48      1.1  christos   "cyan",
     49      1.1  christos   "white",
     50      1.1  christos   nullptr
     51      1.1  christos };
     52      1.1  christos 
     53      1.1  christos /* Names of intensities; must correspond to
     54      1.1  christos    ui_file_style::intensity.  */
     55      1.1  christos static const char * const cli_intensities[] = {
     56      1.1  christos   "normal",
     57      1.1  christos   "bold",
     58      1.1  christos   "dim",
     59      1.1  christos   nullptr
     60      1.1  christos };
     61      1.1  christos 
     62      1.1  christos /* See cli-style.h.  */
     63      1.1  christos 
     64  1.1.1.2  christos cli_style_option file_name_style ("filename", ui_file_style::GREEN);
     65      1.1  christos 
     66      1.1  christos /* See cli-style.h.  */
     67      1.1  christos 
     68  1.1.1.2  christos cli_style_option function_name_style ("function", ui_file_style::YELLOW);
     69      1.1  christos 
     70      1.1  christos /* See cli-style.h.  */
     71      1.1  christos 
     72  1.1.1.2  christos cli_style_option variable_name_style ("variable", ui_file_style::CYAN);
     73      1.1  christos 
     74      1.1  christos /* See cli-style.h.  */
     75      1.1  christos 
     76  1.1.1.2  christos cli_style_option address_style ("address", ui_file_style::BLUE);
     77      1.1  christos 
     78      1.1  christos /* See cli-style.h.  */
     79      1.1  christos 
     80  1.1.1.2  christos cli_style_option highlight_style ("highlight", ui_file_style::RED);
     81  1.1.1.2  christos 
     82  1.1.1.2  christos /* See cli-style.h.  */
     83  1.1.1.2  christos 
     84  1.1.1.2  christos cli_style_option title_style ("title", ui_file_style::BOLD);
     85  1.1.1.2  christos 
     86  1.1.1.2  christos /* See cli-style.h.  */
     87  1.1.1.2  christos 
     88  1.1.1.2  christos cli_style_option tui_border_style ("tui-border", ui_file_style::CYAN);
     89  1.1.1.2  christos 
     90  1.1.1.2  christos /* See cli-style.h.  */
     91  1.1.1.2  christos 
     92  1.1.1.2  christos cli_style_option tui_active_border_style ("tui-active-border",
     93  1.1.1.2  christos 					  ui_file_style::CYAN);
     94  1.1.1.2  christos 
     95  1.1.1.2  christos /* See cli-style.h.  */
     96  1.1.1.2  christos 
     97  1.1.1.2  christos cli_style_option metadata_style ("metadata", ui_file_style::DIM);
     98  1.1.1.2  christos 
     99  1.1.1.2  christos /* See cli-style.h.  */
    100  1.1.1.2  christos 
    101  1.1.1.2  christos cli_style_option::cli_style_option (const char *name,
    102  1.1.1.2  christos 				    ui_file_style::basic_color fg)
    103  1.1.1.2  christos   : changed (name),
    104  1.1.1.2  christos     m_name (name),
    105  1.1.1.2  christos     m_foreground (cli_colors[fg - ui_file_style::NONE]),
    106      1.1  christos     m_background (cli_colors[0]),
    107      1.1  christos     m_intensity (cli_intensities[ui_file_style::NORMAL])
    108      1.1  christos {
    109      1.1  christos }
    110      1.1  christos 
    111  1.1.1.2  christos /* See cli-style.h.  */
    112  1.1.1.2  christos 
    113  1.1.1.2  christos cli_style_option::cli_style_option (const char *name,
    114  1.1.1.2  christos 				    ui_file_style::intensity i)
    115  1.1.1.2  christos   : changed (name),
    116  1.1.1.2  christos     m_name (name),
    117  1.1.1.2  christos     m_foreground (cli_colors[0]),
    118  1.1.1.2  christos     m_background (cli_colors[0]),
    119  1.1.1.2  christos     m_intensity (cli_intensities[i])
    120  1.1.1.2  christos {
    121  1.1.1.2  christos }
    122  1.1.1.2  christos 
    123      1.1  christos /* Return the color number corresponding to COLOR.  */
    124      1.1  christos 
    125      1.1  christos static int
    126      1.1  christos color_number (const char *color)
    127      1.1  christos {
    128      1.1  christos   for (int i = 0; i < ARRAY_SIZE (cli_colors); ++i)
    129      1.1  christos     {
    130      1.1  christos       if (color == cli_colors[i])
    131      1.1  christos 	return i - 1;
    132      1.1  christos     }
    133      1.1  christos   gdb_assert_not_reached ("color not found");
    134      1.1  christos }
    135      1.1  christos 
    136      1.1  christos /* See cli-style.h.  */
    137      1.1  christos 
    138      1.1  christos ui_file_style
    139      1.1  christos cli_style_option::style () const
    140      1.1  christos {
    141      1.1  christos   int fg = color_number (m_foreground);
    142      1.1  christos   int bg = color_number (m_background);
    143      1.1  christos   ui_file_style::intensity intensity = ui_file_style::NORMAL;
    144      1.1  christos 
    145      1.1  christos   for (int i = 0; i < ARRAY_SIZE (cli_intensities); ++i)
    146      1.1  christos     {
    147      1.1  christos       if (m_intensity == cli_intensities[i])
    148      1.1  christos 	{
    149      1.1  christos 	  intensity = (ui_file_style::intensity) i;
    150      1.1  christos 	  break;
    151      1.1  christos 	}
    152      1.1  christos     }
    153      1.1  christos 
    154      1.1  christos   return ui_file_style (fg, bg, intensity);
    155      1.1  christos }
    156      1.1  christos 
    157      1.1  christos /* See cli-style.h.  */
    158      1.1  christos 
    159      1.1  christos void
    160  1.1.1.2  christos cli_style_option::do_set_value (const char *ignore, int from_tty,
    161  1.1.1.2  christos 				struct cmd_list_element *cmd)
    162  1.1.1.2  christos {
    163  1.1.1.2  christos   cli_style_option *cso = (cli_style_option *) get_cmd_context (cmd);
    164  1.1.1.2  christos   cso->changed.notify ();
    165  1.1.1.2  christos }
    166  1.1.1.2  christos 
    167  1.1.1.2  christos /* Implements the cli_style_option::do_show_* functions.
    168  1.1.1.2  christos    WHAT and VALUE are the property and value to show.
    169  1.1.1.2  christos    The style for which WHAT is shown is retrieved from CMD context.  */
    170  1.1.1.2  christos 
    171  1.1.1.2  christos static void
    172  1.1.1.2  christos do_show (const char *what, struct ui_file *file,
    173  1.1.1.2  christos 	 struct cmd_list_element *cmd,
    174  1.1.1.2  christos 	 const char *value)
    175  1.1.1.2  christos {
    176  1.1.1.2  christos   cli_style_option *cso = (cli_style_option *) get_cmd_context (cmd);
    177  1.1.1.2  christos   fputs_filtered (_("The "), file);
    178  1.1.1.2  christos   fprintf_styled (file, cso->style (), _("\"%s\" style"), cso->name ());
    179  1.1.1.2  christos   fprintf_filtered (file, _(" %s is: %s\n"), what, value);
    180  1.1.1.2  christos }
    181  1.1.1.2  christos 
    182  1.1.1.2  christos /* See cli-style.h.  */
    183  1.1.1.2  christos 
    184  1.1.1.2  christos void
    185      1.1  christos cli_style_option::do_show_foreground (struct ui_file *file, int from_tty,
    186      1.1  christos 				      struct cmd_list_element *cmd,
    187      1.1  christos 				      const char *value)
    188      1.1  christos {
    189  1.1.1.2  christos   do_show (_("foreground color"), file, cmd, value);
    190      1.1  christos }
    191      1.1  christos 
    192      1.1  christos /* See cli-style.h.  */
    193      1.1  christos 
    194      1.1  christos void
    195      1.1  christos cli_style_option::do_show_background (struct ui_file *file, int from_tty,
    196      1.1  christos 				      struct cmd_list_element *cmd,
    197      1.1  christos 				      const char *value)
    198      1.1  christos {
    199  1.1.1.2  christos   do_show (_("background color"), file, cmd, value);
    200      1.1  christos }
    201      1.1  christos 
    202      1.1  christos /* See cli-style.h.  */
    203      1.1  christos 
    204      1.1  christos void
    205      1.1  christos cli_style_option::do_show_intensity (struct ui_file *file, int from_tty,
    206      1.1  christos 				     struct cmd_list_element *cmd,
    207      1.1  christos 				     const char *value)
    208      1.1  christos {
    209  1.1.1.2  christos   do_show (_("display intensity"), file, cmd, value);
    210      1.1  christos }
    211      1.1  christos 
    212      1.1  christos /* See cli-style.h.  */
    213      1.1  christos 
    214      1.1  christos void
    215  1.1.1.2  christos cli_style_option::add_setshow_commands (enum command_class theclass,
    216      1.1  christos 					const char *prefix_doc,
    217      1.1  christos 					struct cmd_list_element **set_list,
    218      1.1  christos 					struct cmd_list_element **show_list,
    219  1.1.1.2  christos 					bool skip_intensity)
    220      1.1  christos {
    221  1.1.1.2  christos   m_set_prefix = std::string ("set style ") + m_name + " ";
    222  1.1.1.2  christos   m_show_prefix = std::string ("show style ") + m_name + " ";
    223      1.1  christos 
    224  1.1.1.2  christos   add_basic_prefix_cmd (m_name, no_class, prefix_doc, &m_set_list,
    225  1.1.1.2  christos 			m_set_prefix.c_str (), 0, set_list);
    226  1.1.1.2  christos   add_show_prefix_cmd (m_name, no_class, prefix_doc, &m_show_list,
    227  1.1.1.2  christos 		       m_show_prefix.c_str (), 0, show_list);
    228      1.1  christos 
    229      1.1  christos   add_setshow_enum_cmd ("foreground", theclass, cli_colors,
    230      1.1  christos 			&m_foreground,
    231  1.1.1.2  christos 			_("Set the foreground color for this property."),
    232  1.1.1.2  christos 			_("Show the foreground color for this property."),
    233      1.1  christos 			nullptr,
    234  1.1.1.2  christos 			do_set_value,
    235      1.1  christos 			do_show_foreground,
    236  1.1.1.2  christos 			&m_set_list, &m_show_list, (void *) this);
    237      1.1  christos   add_setshow_enum_cmd ("background", theclass, cli_colors,
    238      1.1  christos 			&m_background,
    239  1.1.1.2  christos 			_("Set the background color for this property."),
    240  1.1.1.2  christos 			_("Show the background color for this property."),
    241      1.1  christos 			nullptr,
    242  1.1.1.2  christos 			do_set_value,
    243      1.1  christos 			do_show_background,
    244  1.1.1.2  christos 			&m_set_list, &m_show_list, (void *) this);
    245  1.1.1.2  christos   if (!skip_intensity)
    246  1.1.1.2  christos     add_setshow_enum_cmd ("intensity", theclass, cli_intensities,
    247  1.1.1.2  christos 			  &m_intensity,
    248  1.1.1.2  christos 			  _("Set the display intensity for this property."),
    249  1.1.1.2  christos 			  _("Show the display intensity for this property."),
    250  1.1.1.2  christos 			  nullptr,
    251  1.1.1.2  christos 			  do_set_value,
    252  1.1.1.2  christos 			  do_show_intensity,
    253  1.1.1.2  christos 			  &m_set_list, &m_show_list, (void *) this);
    254      1.1  christos }
    255      1.1  christos 
    256      1.1  christos static cmd_list_element *style_set_list;
    257      1.1  christos static cmd_list_element *style_show_list;
    258      1.1  christos 
    259      1.1  christos static void
    260      1.1  christos set_style_enabled  (const char *args, int from_tty, struct cmd_list_element *c)
    261      1.1  christos {
    262      1.1  christos   g_source_cache.clear ();
    263      1.1  christos   gdb::observers::source_styling_changed.notify ();
    264      1.1  christos }
    265      1.1  christos 
    266      1.1  christos static void
    267      1.1  christos show_style_enabled (struct ui_file *file, int from_tty,
    268      1.1  christos 		    struct cmd_list_element *c, const char *value)
    269      1.1  christos {
    270      1.1  christos   if (cli_styling)
    271      1.1  christos     fprintf_filtered (file, _("CLI output styling is enabled.\n"));
    272      1.1  christos   else
    273      1.1  christos     fprintf_filtered (file, _("CLI output styling is disabled.\n"));
    274      1.1  christos }
    275      1.1  christos 
    276      1.1  christos static void
    277      1.1  christos show_style_sources (struct ui_file *file, int from_tty,
    278      1.1  christos 		    struct cmd_list_element *c, const char *value)
    279      1.1  christos {
    280      1.1  christos   if (source_styling)
    281      1.1  christos     fprintf_filtered (file, _("Source code styling is enabled.\n"));
    282      1.1  christos   else
    283      1.1  christos     fprintf_filtered (file, _("Source code styling is disabled.\n"));
    284      1.1  christos }
    285      1.1  christos 
    286  1.1.1.2  christos void _initialize_cli_style ();
    287      1.1  christos void
    288      1.1  christos _initialize_cli_style ()
    289      1.1  christos {
    290  1.1.1.2  christos   add_basic_prefix_cmd ("style", no_class, _("\
    291  1.1.1.2  christos Style-specific settings.\n\
    292      1.1  christos Configure various style-related variables, such as colors"),
    293      1.1  christos 		  &style_set_list, "set style ", 0, &setlist);
    294  1.1.1.2  christos   add_show_prefix_cmd ("style", no_class, _("\
    295  1.1.1.2  christos Style-specific settings.\n\
    296      1.1  christos Configure various style-related variables, such as colors"),
    297      1.1  christos 		  &style_show_list, "show style ", 0, &showlist);
    298      1.1  christos 
    299      1.1  christos   add_setshow_boolean_cmd ("enabled", no_class, &cli_styling, _("\
    300      1.1  christos Set whether CLI styling is enabled."), _("\
    301      1.1  christos Show whether CLI is enabled."), _("\
    302      1.1  christos If enabled, output to the terminal is styled."),
    303      1.1  christos 			   set_style_enabled, show_style_enabled,
    304      1.1  christos 			   &style_set_list, &style_show_list);
    305      1.1  christos 
    306      1.1  christos   add_setshow_boolean_cmd ("sources", no_class, &source_styling, _("\
    307      1.1  christos Set whether source code styling is enabled."), _("\
    308      1.1  christos Show whether source code styling is enabled."), _("\
    309      1.1  christos If enabled, source code is styled.\n"
    310      1.1  christos #ifdef HAVE_SOURCE_HIGHLIGHT
    311      1.1  christos "Note that source styling only works if styling in general is enabled,\n\
    312      1.1  christos see \"show style enabled\"."
    313      1.1  christos #else
    314  1.1.1.2  christos "Source highlighting may be disabled in this installation of gdb, because\n\
    315  1.1.1.2  christos it was not linked against GNU Source Highlight.  However, it might still be\n\
    316  1.1.1.2  christos available if the appropriate extension is available at runtime."
    317      1.1  christos #endif
    318      1.1  christos 			   ), set_style_enabled, show_style_sources,
    319      1.1  christos 			   &style_set_list, &style_show_list);
    320      1.1  christos 
    321  1.1.1.2  christos   file_name_style.add_setshow_commands (no_class, _("\
    322  1.1.1.2  christos Filename display styling.\n\
    323  1.1.1.2  christos Configure filename colors and display intensity."),
    324  1.1.1.2  christos 					&style_set_list, &style_show_list,
    325  1.1.1.2  christos 					false);
    326  1.1.1.2  christos 
    327  1.1.1.2  christos   function_name_style.add_setshow_commands (no_class, _("\
    328  1.1.1.2  christos Function name display styling.\n\
    329  1.1.1.2  christos Configure function name colors and display intensity"),
    330  1.1.1.2  christos 					    &style_set_list, &style_show_list,
    331  1.1.1.2  christos 					    false);
    332  1.1.1.2  christos 
    333  1.1.1.2  christos   variable_name_style.add_setshow_commands (no_class, _("\
    334  1.1.1.2  christos Variable name display styling.\n\
    335  1.1.1.2  christos Configure variable name colors and display intensity"),
    336  1.1.1.2  christos 					    &style_set_list, &style_show_list,
    337  1.1.1.2  christos 					    false);
    338  1.1.1.2  christos 
    339  1.1.1.2  christos   address_style.add_setshow_commands (no_class, _("\
    340  1.1.1.2  christos Address display styling.\n\
    341  1.1.1.2  christos Configure address colors and display intensity"),
    342  1.1.1.2  christos 				      &style_set_list, &style_show_list,
    343  1.1.1.2  christos 				      false);
    344  1.1.1.2  christos 
    345  1.1.1.2  christos   title_style.add_setshow_commands (no_class, _("\
    346  1.1.1.2  christos Title display styling.\n\
    347  1.1.1.2  christos Configure title colors and display intensity\n\
    348  1.1.1.2  christos Some commands (such as \"apropos -v REGEXP\") use the title style to improve\n\
    349  1.1.1.2  christos readability."),
    350  1.1.1.2  christos 				    &style_set_list, &style_show_list,
    351  1.1.1.2  christos 				    false);
    352  1.1.1.2  christos 
    353  1.1.1.2  christos   highlight_style.add_setshow_commands (no_class, _("\
    354  1.1.1.2  christos Highlight display styling.\n\
    355  1.1.1.2  christos Configure highlight colors and display intensity\n\
    356  1.1.1.2  christos Some commands use the highlight style to draw the attention to a part\n\
    357  1.1.1.2  christos of their output."),
    358  1.1.1.2  christos 					&style_set_list, &style_show_list,
    359  1.1.1.2  christos 					false);
    360  1.1.1.2  christos 
    361  1.1.1.2  christos   metadata_style.add_setshow_commands (no_class, _("\
    362  1.1.1.2  christos Metadata display styling.\n\
    363  1.1.1.2  christos Configure metadata colors and display intensity\n\
    364  1.1.1.2  christos The \"metadata\" style is used when GDB displays information about\n\
    365  1.1.1.2  christos your data, for example \"<unavailable>\""),
    366  1.1.1.2  christos 				       &style_set_list, &style_show_list,
    367  1.1.1.2  christos 				       false);
    368  1.1.1.2  christos 
    369  1.1.1.2  christos   tui_border_style.add_setshow_commands (no_class, _("\
    370  1.1.1.2  christos TUI border display styling.\n\
    371  1.1.1.2  christos Configure TUI border colors\n\
    372  1.1.1.2  christos The \"tui-border\" style is used when GDB displays the border of a\n\
    373  1.1.1.2  christos TUI window that does not have the focus."),
    374  1.1.1.2  christos 					 &style_set_list, &style_show_list,
    375  1.1.1.2  christos 					 true);
    376  1.1.1.2  christos 
    377  1.1.1.2  christos   tui_active_border_style.add_setshow_commands (no_class, _("\
    378  1.1.1.2  christos TUI active border display styling.\n\
    379  1.1.1.2  christos Configure TUI active border colors\n\
    380  1.1.1.2  christos The \"tui-active-border\" style is used when GDB displays the border of a\n\
    381  1.1.1.2  christos TUI window that does have the focus."),
    382  1.1.1.2  christos 						&style_set_list,
    383  1.1.1.2  christos 						&style_show_list,
    384  1.1.1.2  christos 						true);
    385      1.1  christos }
    386