diff options
Diffstat (limited to 'gdb/cli/cli-style.c')
-rw-r--r-- | gdb/cli/cli-style.c | 292 |
1 files changed, 244 insertions, 48 deletions
diff --git a/gdb/cli/cli-style.c b/gdb/cli/cli-style.c index 5928998..d6829f0 100644 --- a/gdb/cli/cli-style.c +++ b/gdb/cli/cli-style.c @@ -1,6 +1,6 @@ /* CLI colorizing - Copyright (C) 2018-2024 Free Software Foundation, Inc. + Copyright (C) 2018-2025 Free Software Foundation, Inc. This file is part of GDB. @@ -23,13 +23,14 @@ #include "cli/cli-style.h" #include "source-cache.h" #include "observable.h" +#include "charset.h" /* True if styling is enabled. */ #if defined (__MSDOS__) -bool cli_styling = false; +static bool cli_styling = false; #else -bool cli_styling = true; +static bool cli_styling = true; #endif /* True if source styling is enabled. Note that this is only @@ -42,19 +43,9 @@ bool source_styling = true; bool disassembler_styling = true; -/* 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 -}; +/* User-settable variable controlling emoji output. */ + +static auto_boolean emoji_styling = AUTO_BOOLEAN_AUTO; /* Names of intensities; must correspond to ui_file_style::intensity. */ @@ -65,6 +56,81 @@ static const char * const cli_intensities[] = { nullptr }; +/* When true styling is being temporarily suppressed. */ + +static bool scoped_disable_styling_p = false; + +/* See cli/cli-style.h. */ + +scoped_disable_styling::scoped_disable_styling () +{ + m_old_value = scoped_disable_styling_p; + scoped_disable_styling_p = true; +} + +/* See cli/cli-style.h. */ + +scoped_disable_styling::~scoped_disable_styling () +{ + scoped_disable_styling_p = m_old_value; +} + +/* Return true if GDB's output terminal should support styling, otherwise, + return false. This function really checks for things that indicate + styling might not be supported, so a return value of false indicates + we've seen something to indicate we should not perform styling. A + return value of true is the default. */ + +static bool +terminal_supports_styling () +{ + const char *term = getenv ("TERM"); + + /* Windows doesn't by default define $TERM, but can support styles + regardless. */ +#ifndef _WIN32 + if (term == nullptr || strcmp (term, "dumb") == 0) + return false; +#else + /* But if they do define $TERM, let us behave the same as on Posix + platforms, for the benefit of programs which invoke GDB as their + back-end. */ + if (term != nullptr && strcmp (term, "dumb") == 0) + return false; +#endif + + return true; +} + +/* See cli/cli-style.h. */ + +void +disable_cli_styling () +{ + cli_styling = false; +} + +/* See cli/cli-style.h. */ + +bool +term_cli_styling () +{ + return cli_styling && !scoped_disable_styling_p; +} + +/* See cli/cli-style.h. */ + +void +disable_styling_from_environment () +{ + const char *no_color = getenv ("NO_COLOR"); + if (no_color != nullptr && *no_color != '\0') + cli_styling = false; + + if (!terminal_supports_styling ()) + cli_styling = false; +} + /* See cli-style.h. */ cli_style_option file_name_style ("filename", ui_file_style::GREEN); @@ -91,6 +157,10 @@ cli_style_option title_style ("title", ui_file_style::BOLD); /* See cli-style.h. */ +cli_style_option command_style ("command", ui_file_style::BOLD); + +/* See cli-style.h. */ + cli_style_option tui_border_style ("tui-border", ui_file_style::CYAN); /* See cli-style.h. */ @@ -126,13 +196,17 @@ cli_style_option disasm_comment_style ("comment", ui_file_style::WHITE, /* See cli-style.h. */ +cli_style_option line_number_style ("line-number", ui_file_style::DIM); + +/* See cli-style.h. */ + cli_style_option::cli_style_option (const char *name, ui_file_style::basic_color fg, ui_file_style::intensity intensity) : changed (name), m_name (name), - m_foreground (cli_colors[fg - ui_file_style::NONE]), - m_background (cli_colors[0]), + m_foreground (fg), + m_background (ui_file_style::NONE), m_intensity (cli_intensities[intensity]) { } @@ -143,32 +217,17 @@ cli_style_option::cli_style_option (const char *name, ui_file_style::intensity i) : changed (name), m_name (name), - m_foreground (cli_colors[0]), - m_background (cli_colors[0]), + m_foreground (ui_file_style::NONE), + m_background (ui_file_style::NONE), m_intensity (cli_intensities[i]) { } -/* 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) @@ -180,7 +239,7 @@ cli_style_option::style () const } } - return ui_file_style (fg, bg, intensity); + return ui_file_style (m_foreground, m_background, intensity); } /* See cli-style.h. */ @@ -253,9 +312,8 @@ cli_style_option::add_setshow_commands (enum command_class theclass, set_show_commands commands; - commands = add_setshow_enum_cmd - ("foreground", theclass, cli_colors, - &m_foreground, + commands = add_setshow_color_cmd + ("foreground", theclass, &m_foreground, _("Set the foreground color for this property."), _("Show the foreground color for this property."), nullptr, @@ -265,9 +323,8 @@ cli_style_option::add_setshow_commands (enum command_class theclass, commands.set->set_context (this); commands.show->set_context (this); - commands = add_setshow_enum_cmd - ("background", theclass, cli_colors, - &m_background, + commands = add_setshow_color_cmd + ("background", theclass, &m_background, _("Set the background color for this property."), _("Show the background color for this property."), nullptr, @@ -309,7 +366,20 @@ static cmd_list_element *style_disasm_show_list; static void set_style_enabled (const char *args, int from_tty, struct cmd_list_element *c) { - g_source_cache.clear (); + /* This finds the 'set style enabled' command. */ + struct cmd_list_element *set_style_enabled_cmd + = lookup_cmd_exact ("enabled", style_set_list); + + /* If the user does 'set style enabled on', but the terminal doesn't + appear to support styling, then warn the user. */ + if (c == set_style_enabled_cmd && cli_styling + && !terminal_supports_styling ()) + warning ("The current terminal doesn't support styling. Styled output " + "might not appear as expected."); + + /* It is not necessary to flush the source cache here. The source cache + tracks whether entries are styled or not. */ + gdb::observers::styling_changed.notify (); } @@ -345,9 +415,86 @@ show_style_disassembler (struct ui_file *file, int from_tty, gdb_printf (file, _("Disassembler output styling is disabled.\n")); } -void _initialize_cli_style (); +/* Implement 'show style emoji'. */ + +static void +show_emoji_styling (struct ui_file *file, int from_tty, + struct cmd_list_element *c, const char *value) +{ + if (emoji_styling == AUTO_BOOLEAN_TRUE) + gdb_printf (file, _("CLI emoji styling is enabled.\n")); + else if (emoji_styling == AUTO_BOOLEAN_FALSE) + gdb_printf (file, _("CLI emoji styling is disabled.\n")); + else + gdb_printf (file, _("CLI emoji styling is automatic (currently %s).\n"), + emojis_ok () ? _("enabled") : _("disabled")); +} + +/* See cli-style.h. */ + +bool +emojis_ok () +{ + if (!cli_styling || emoji_styling == AUTO_BOOLEAN_FALSE) + return false; + if (emoji_styling == AUTO_BOOLEAN_TRUE) + return true; + return strcmp (host_charset (), "UTF-8") == 0; +} + +/* See cli-style.h. */ + +void +no_emojis () +{ + emoji_styling = AUTO_BOOLEAN_FALSE; +} + +/* Emoji warning prefix. */ +static std::string warning_prefix = "⚠️ "; + +/* Implement 'show style warning-prefix'. */ + +static void +show_warning_prefix (struct ui_file *file, int from_tty, + struct cmd_list_element *c, const char *value) +{ + gdb_printf (file, _("Warning prefix is \"%s\".\n"), + warning_prefix.c_str ()); +} + +/* See cli-style.h. */ + +void +print_warning_prefix (ui_file *file) +{ + if (emojis_ok ()) + gdb_puts (warning_prefix.c_str (), file); +} + +/* Emoji error prefix. */ +static std::string error_prefix = "❌️ "; + +/* Implement 'show style error-prefix'. */ + +static void +show_error_prefix (struct ui_file *file, int from_tty, + struct cmd_list_element *c, const char *value) +{ + gdb_printf (file, _("Error prefix is \"%s\".\n"), + error_prefix.c_str ()); +} + +/* See cli-style.h. */ + void -_initialize_cli_style () +print_error_prefix (ui_file *file) +{ + if (emojis_ok ()) + gdb_puts (error_prefix.c_str (), file); +} + +INIT_GDB_FILE (cli_style) { add_setshow_prefix_cmd ("style", no_class, _("\ @@ -366,6 +513,13 @@ If enabled, output to the terminal is styled."), set_style_enabled, show_style_enabled, &style_set_list, &style_show_list); + add_setshow_auto_boolean_cmd ("emoji", no_class, &emoji_styling, _("\ +Set whether emoji output is enabled."), _("\ +Show whether emoji output is enabled."), _("\ +If enabled, emojis may be displayed."), + nullptr, show_emoji_styling, + &style_set_list, &style_show_list); + add_setshow_boolean_cmd ("sources", no_class, &source_styling, _("\ Set whether source code styling is enabled."), _("\ Show whether source code styling is enabled."), _("\ @@ -394,9 +548,17 @@ Configure various disassembler style-related variables."), add_setshow_boolean_cmd ("enabled", no_class, &disassembler_styling, _("\ Set whether disassembler output styling is enabled."), _("\ Show whether disassembler output styling is enabled."), _("\ -If enabled, disassembler output is styled. Disassembler highlighting\n\ -requires the Python Pygments library, if this library is not available\n\ -then disassembler highlighting will not be possible." +If enabled, disassembler output is styled.\n\ +\n\ +Disassembler styling requires a library that is able to style the current\n\ +instruction architecture. By default, GDB will use its builtin library\n\ +for disassembler styling, but this cannot style every architecture.\n\ +\n\ +For architectures that cannot be styled by the builtin disassembler library\n\ +GDB will use the Python Pygments library, if this library is available.\n\ +\n\ +If neither option is able to style the current architecture, then\n\ +disassembler output will be unstyled, even when this option is enabled." ), set_style_enabled, show_style_disassembler, &style_disasm_set_list, &style_disasm_show_list); @@ -435,6 +597,13 @@ readability."), &style_set_list, &style_show_list, false); + command_style.add_setshow_commands (no_class, _("\ +Command display styling.\n\ +Configure the colors and display intensity for GDB commands mentioned\n\ +in the output."), + &style_set_list, &style_show_list, + false); + highlight_style.add_setshow_commands (no_class, _("\ Highlight display styling.\n\ Configure highlight colors and display intensity\n\ @@ -529,6 +698,14 @@ then this style has no effect."), &style_disasm_show_list, false); + line_number_style.add_setshow_commands (no_class, _("\ +Line number display styling.\n\ +Configure colors and display intensity for line numbers\n\ +The \"line-number\" style is used when GDB displays line numbers\n\ +coming from your source code."), + &style_set_list, &style_show_list, + false); + /* Setup 'disassembler address' style and 'disassembler symbol' style, these are aliases for 'address' and 'function' styles respectively. */ add_alias_cmd ("address", address_prefix_cmds.set, no_class, 0, @@ -539,4 +716,23 @@ then this style has no effect."), &style_disasm_set_list); add_alias_cmd ("symbol", function_prefix_cmds.show, no_class, 0, &style_disasm_show_list); + + add_setshow_string_cmd ("warning-prefix", no_class, + &warning_prefix, + _("Set the warning prefix text."), + _("Show the warning prefix text."), + _("\ +The warning prefix text is displayed before any warning, when\n\ +emoji output is enabled."), + nullptr, show_warning_prefix, + &style_set_list, &style_show_list); + add_setshow_string_cmd ("error-prefix", no_class, + &error_prefix, + _("Set the error prefix text."), + _("Show the error prefix text."), + _("\ +The error prefix text is displayed before any error, when\n\ +emoji output is enabled."), + nullptr, show_error_prefix, + &style_set_list, &style_show_list); } |