diff options
author | Andrew Burgess <aburgess@redhat.com> | 2025-06-17 18:09:49 +0100 |
---|---|---|
committer | Andrew Burgess <aburgess@redhat.com> | 2025-10-05 13:48:06 +0100 |
commit | 3c724596812882cc0c3b653330551fae132d6475 (patch) | |
tree | 5607b83fa5e5fba0dd3ff4b9b0123bcb990b284b /gdb/python/py-style.c | |
parent | d5214580a5f24cb1ca47f095e6d3554cb48eebbe (diff) | |
download | binutils-3c724596812882cc0c3b653330551fae132d6475.zip binutils-3c724596812882cc0c3b653330551fae132d6475.tar.gz binutils-3c724596812882cc0c3b653330551fae132d6475.tar.bz2 |
gdb/python: extend gdb.write to support styled output
It is already possible to produce styled output from Python by
converting the gdb.Style to its escape code sequence, and writing that
to the output stream.
But this commit adds an alternative option to the mix by extending the
existing gdb.write() function to accept a 'style' argument. The value
of this argument can be 'None' to indicate no style change should be
performed, this is the default, and matches the existing behaviour.
Or the new 'style' argument can be a gdb.Style object, in which case
the specified style is applied only for the string passed to
gdb.write, after which the default style is re-applied.
Using gdb.write with a style object more closely matches how GDB
handles styling internally, and has the benefit that the user doesn't
need to remember to restore the default style when they are done.
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Approved-By: Tom Tromey <tom@tromey.com>
Diffstat (limited to 'gdb/python/py-style.c')
-rw-r--r-- | gdb/python/py-style.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/gdb/python/py-style.c b/gdb/python/py-style.c index 51b35f2..cf65e31 100644 --- a/gdb/python/py-style.c +++ b/gdb/python/py-style.c @@ -353,6 +353,15 @@ stylepy_init (PyObject *self, PyObject *args, PyObject *kwargs) +/* See python-internal.h. */ + +bool +gdbpy_is_style (PyObject *obj) +{ + gdb_assert (obj != nullptr); + return PyObject_TypeCheck (obj, &style_object_type); +} + /* Return the ui_file_style for STYLEPY. If the style cannot be found, then return an empty optional, and set a Python error. */ @@ -369,6 +378,18 @@ stylepy_to_style (style_object *stylepy) return style; } +/* See python-internal.h. */ + +std::optional<ui_file_style> +gdbpy_style_object_to_ui_file_style (PyObject *obj) +{ + gdb_assert (obj != nullptr); + gdb_assert (gdbpy_is_style (obj)); + + style_object *style_obj = (style_object *) obj; + return stylepy_to_style (style_obj); +} + /* Implementation of gdb.Style.escape_sequence(). Return the escape sequence to apply Style. If styling is turned off, then this returns the empty string. Can raise an exception if a named style can no longer |