diff options
author | Andrew Burgess <aburgess@redhat.com> | 2025-04-29 17:57:06 +0100 |
---|---|---|
committer | Andrew Burgess <aburgess@redhat.com> | 2025-05-06 11:24:28 +0100 |
commit | 41b0ab843fb8f1639c34776512f0886af96a36a9 (patch) | |
tree | 55a6a3281d6d41bb1aa510552361959d2fb92d75 /gdb/python | |
parent | 4dd03f30caa7cae1d41ff070e7519b09d5d95648 (diff) | |
download | binutils-41b0ab843fb8f1639c34776512f0886af96a36a9.zip binutils-41b0ab843fb8f1639c34776512f0886af96a36a9.tar.gz binutils-41b0ab843fb8f1639c34776512f0886af96a36a9.tar.bz2 |
gdb/python/guile: check if styling is disabled in Color.escape_sequence
I noticed that the gdb.Color.escape_sequence() method would produce an
escape sequence even when styling is disabled.
I think this is the wrong choice. Ideally, when styling is
disabled (e.g. with 'set style enabled off'), GDB should not be
producing styled output.
If a GDB extension is using gdb.Color to apply styling to the output,
then currently, the extension should be checking 'show style enabled'
any time Color.escape_sequence() is used. This means lots of code
duplication, and the possibility that some locations will be missed,
which means disabling styling no longer does what it says.
I propose that Color.escape_sequence() should return the empty string
if styling is disabled. A Python extension can then do:
python
c_none = gdb.Color('none')
c_red = gdb.Color('red')
print(c_red.escape_sequence(True)
+ "Text in red."
+ c_none.escape_sequence(True))
end
If styling is enable this will print some red text. And if styling is
disabled, then it will print text in the terminal's default color.
I have applied the same fix to the guile API.
I have extended the tests to cover this case.
Approved-By: Tom Tromey <tom@tromey.com>
Diffstat (limited to 'gdb/python')
-rw-r--r-- | gdb/python/py-color.c | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/gdb/python/py-color.c b/gdb/python/py-color.c index e208506..3bbd22d 100644 --- a/gdb/python/py-color.c +++ b/gdb/python/py-color.c @@ -21,6 +21,7 @@ #include "python-internal.h" #include "py-color.h" #include "cli/cli-decode.h" +#include "cli/cli-style.h" /* Colorspace constants and their values. */ static struct { @@ -152,8 +153,12 @@ colorpy_escape_sequence (PyObject *self, PyObject *args, PyObject *kwargs) /* The argument parsing ensures we have a bool. */ gdb_assert (PyBool_Check (is_fg_obj)); - bool is_fg = is_fg_obj == Py_True; - std::string s = gdbpy_get_color (self).to_ansi (is_fg); + std::string s; + if (term_cli_styling ()) + { + bool is_fg = is_fg_obj == Py_True; + s = gdbpy_get_color (self).to_ansi (is_fg); + } return host_string_to_python_string (s.c_str ()).release (); } |