[PATCH 0/2] Python Style API

Andrew Burgess
2025-06-04T09:40:29Z

After the gdb.Color support was added to the Python API I wondered if
this would be enough to easily add styling to custom Python commands.
But I don't think it is, there's still lots that would need to be
handled in user code, for example, the intensity setting. And having
styles that track named styles (e.g. if I want to apply the 'filename'
style to some text from Python).

So this series builds on top of the gdb.Color API to provide a new
gdb.Style API which, I hope, makes it easy to start writing Python
commands that can use GDB's styles.

Here's an example of a simple custome command that uses styles:

import os

class user_home_cmd(gdb.Command):
def __init__(self):
super().__init__("user-home", gdb.COMMAND_USER)
self._style = gdb.Style('filename')

def invoke(self, args, from_tty):
print("The filename is %s."
% (self._style.apply(os.environ['HOME'])))

user_home_cmd()

The new 'user-home' command prints the current users home directory
using 'filename' style. Changing the 'filename' style and re-running
the command will track the style changes.

Thanks,
Andrew

---

Andrew Burgess (2):
gdb/python: add gdb.Style class
gdb/python: new class gdb.StyleParameterSet

gdb/Makefile.in | 1 +
gdb/NEWS | 11 +
gdb/doc/python.texi | 253 ++++++
gdb/python/lib/gdb/__init__.py | 209 +++++
gdb/python/py-style.c | 797 ++++++++++++++++++
.../gdb.python/py-style-parameter-set.exp | 361 ++++++++
gdb/testsuite/gdb.python/py-style.exp | 339 ++++++++
gdb/ui-style.h | 6 +
8 files changed, 1977 insertions(+)
create mode 100644 gdb/python/py-style.c
create mode 100644 gdb/testsuite/gdb.python/py-style-parameter-set.exp
create mode 100644 gdb/testsuite/gdb.python/py-style.exp

base-commit: f84a4db958af0ee1521acdf69eb2be38ea2e66a3
--
2.47.1