Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: Andrew Burgess <aburgess@redhat.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCHv2 1/2] gdb/python: add gdb.Style class
Date: Fri, 06 Jun 2025 17:21:32 +0300	[thread overview]
Message-ID: <86h60tkl1v.fsf@gnu.org> (raw)
In-Reply-To: <7034bcb2562c13b580689a221f8c313e39940883.1749216302.git.aburgess@redhat.com> (message from Andrew Burgess on Fri, 6 Jun 2025 14:38:13 +0100)

> From: Andrew Burgess <aburgess@redhat.com>
> Cc: Andrew Burgess <aburgess@redhat.com>
> Date: Fri,  6 Jun 2025 14:38:13 +0100
> 
> This commit adds a new gdb.Style class.  This class represents a
> complete style within GDB.  A complete style is a collection of
> foreground color, background color, and an intensity.
> 
> A gdb.Style comes in two flavours, named, and unnamed.
> 
> A named style is one that is based on an existing style within GDB.
> For example, we have 'set style filename ...', the name of this style
> is 'filename'.  We also have 'set style disassembler mnemonic ...',
> the name of this style is 'disassembler mnemonic'.  A named style is
> created by passing the name of the style, like this:
> 
>   (gdb) python s1 = gdb.Style("filename")
>   (gdb) python s2 = gdb.Style("disassembler mnemonic")
> 
> The other type of style is an unnamed style.  An unnamed style is
> created using a foreground and background color, along with an
> intensity.  Colors are specified using gdb.Color objects.  An example
> of creating an unnamed style is:
> 
>   (gdb) python s3 = gdb.Style(foreground=gdb.Color('red'),
>                               background=gdb.Color('green'),
> 			      intensity=gdb.INTENSITY_BOLD)
> 
> We can see here an example of the new intensity constants that have
> been added in this commit, there is gdb.INTENSITY_NORMAL,
> gdb.INTENSITY_BOLD, and gdb.INTENSITY_DIM.  All of the arguments are
> optional, the default for the colors is gdb.Color(), which will apply
> the terminal default, and the default intensity is
> gdb.INTENSITY_NORMAL.
> 
> Having created a gdb.Style object there are two ways that it can be
> used to style GDB's output.  The Style.escape_sequence() method
> returns the escape sequence needed to apply this style, this can be
> used as in:
> 
>   (gdb) python print(s1.escape_sequence() + "Filename Style")
> 
> The problem with this approach is that it is the users responsibility
> to restore the style to the default when they are done.  In the above
> example, all output after the escape sequence is printed, including
> the next GDB prompt, will be in the s1 (filename) style.  Which is why
> the Style.apply method exists.  This method takes a string and returns
> the same string with escape sequences added before and after.  The
> before sequence switches to the style, while the after escape sequence
> restores the terminal default style.  This can be used like:
> 
>   (gdb) python print(s1.apply("Filename Style"))
> 
> Now only the 'Filename Style' text will be styled.  The next GDB
> prompt will be in the default terminal style.  Personally, I think the
> apply method is the more useful, but having 'escape_sequence' matches
> what gdb.Color offers, though if/when this patch is merged, I might
> propose a similar 'apply' type method for the gdb.Color class.
> 
> The gdb.Style class has 'foreground', 'background', and 'intensity'
> attributes which, when read, return the obvious values.  These
> attributes can also be written too.
> 
> When writing to an attribute of an unnamed Style object then the Style
> object itself is updated, as you might expect.
> 
> When writing to an attribute of a named Style then the style setting
> itself is updated as the following example shows:
> 
>   (gdb) python s1 = gdb.Style("filename")
>   (gdb) python print(s1.foreground)
>   green
>   (gdb) show style filename foreground
>   The "filename" style foreground color is: green
>   (gdb) python s1.foreground=gdb.Color("red")
>   (gdb) python print(s1.foreground)
>   red
>   (gdb) show style filename foreground
>   The "filename" style foreground color is: red
>   (gdb)
> 
> We can see that a gdb.Style object is connected to the underlying
> style settings, it doesn't take a copy of the style settings at
> creation time.  And the relationship works both ways.  Continuing the
> above example:
> 
>   (gdb) set style filename foreground blue
>   (gdb) python print(s1.foreground)
>   blue
>   (gdb)
> 
> Here we see that changing the setting value causes the gdb.Style
> object to update.  And this is what you would want.  I imagine this
> being used in a Python extension to GDB where a user might create
> global objects for some named styles, and then use these globals to
> format output from some custom commands.  If a user of an extension
> changes a style setting then the extension wants to adapt to that
> change.
> 
> Both the Style.escape_sequence and Style.apply methods take the global
> style enabled setting into consideration.  If styling is disabled then
> Style.escape_sequence will return an empty string, and Style.apply
> will return an unmodified copy of the original string object (actually
> the input object with Py_INCREF applied).
> 
> There is also support for representing a gdb.Style as a string:
> 
>   (gdb) python s1 = gdb.Style("filename")
>   (gdb) python print(s1)
>   <gdb.Style name='filename', fg=green, bg=none, intensity=normal>
>   (gdb)
> 
> Unnamed styles are similar, but don't have a 'name' field.
> ---
>  gdb/Makefile.in                       |   1 +
>  gdb/NEWS                              |   7 +
>  gdb/doc/python.texi                   | 140 +++++
>  gdb/python/py-style.c                 | 797 ++++++++++++++++++++++++++
>  gdb/testsuite/gdb.python/py-style.exp | 339 +++++++++++
>  gdb/ui-style.h                        |   6 +
>  6 files changed, 1290 insertions(+)
>  create mode 100644 gdb/python/py-style.c
>  create mode 100644 gdb/testsuite/gdb.python/py-style.exp

Thanks, the documentation parts are okay.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>

  reply	other threads:[~2025-06-06 14:22 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-04  9:39 [PATCH 0/2] Python Style API Andrew Burgess
2025-06-04  9:39 ` [PATCH 1/2] gdb/python: add gdb.Style class Andrew Burgess
2025-06-04  9:39 ` [PATCH 2/2] gdb/python: new class gdb.StyleParameterSet Andrew Burgess
2025-06-06 13:38 ` [PATCHv2 0/2] Python Style API Andrew Burgess
2025-06-06 13:38   ` [PATCHv2 1/2] gdb/python: add gdb.Style class Andrew Burgess
2025-06-06 14:21     ` Eli Zaretskii [this message]
2025-06-06 13:38   ` [PATCHv2 2/2] gdb/python: new class gdb.StyleParameterSet Andrew Burgess
2025-06-07 10:44   ` [PATCHv3 0/2] Python Style API Andrew Burgess
2025-06-07 10:44     ` [PATCHv3 1/2] gdb/python: add gdb.Style class Andrew Burgess
2025-06-07 10:44     ` [PATCHv3 2/2] gdb/python: new class gdb.StyleParameterSet Andrew Burgess
2025-06-09 15:54     ` [PATCHv3 0/2] Python Style API Andrew Burgess
2025-06-18 19:30     ` [PATCHv4 0/4] " Andrew Burgess
2025-06-18 19:30       ` [PATCHv4 1/4] gdb: allow gdb.Color to work correctly with pagination Andrew Burgess
2025-06-18 19:30       ` [PATCHv4 2/4] gdb/python: add gdb.Style class Andrew Burgess
2025-06-18 19:30       ` [PATCHv4 3/4] gdb/python: new class gdb.StyleParameterSet Andrew Burgess
2025-06-18 19:30       ` [PATCHv4 4/4] gdb: extend gdb.write to support styled output Andrew Burgess
2025-06-25 14:42       ` [PATCHv5 0/4] Python Style API Andrew Burgess
2025-06-25 14:42         ` [PATCHv5 1/4] gdb: allow gdb.Color to work correctly with pagination Andrew Burgess
2025-06-25 14:42         ` [PATCHv5 2/4] gdb/python: add gdb.Style class Andrew Burgess
2025-06-25 14:42         ` [PATCHv5 3/4] gdb/python: new class gdb.StyleParameterSet Andrew Burgess
2025-06-25 14:42         ` [PATCHv5 4/4] gdb/python: extend gdb.write to support styled output Andrew Burgess
2025-07-07 14:43         ` [PATCHv6 0/4] Python Style API Andrew Burgess
2025-07-07 14:43           ` [PATCHv6 1/4] gdb: allow gdb.Color to work correctly with pagination Andrew Burgess
2025-07-07 14:43           ` [PATCHv6 2/4] gdb/python: add gdb.Style class Andrew Burgess
2025-07-07 14:43           ` [PATCHv6 3/4] gdb/python: new class gdb.StyleParameterSet Andrew Burgess
2025-07-07 14:43           ` [PATCHv6 4/4] gdb/python: extend gdb.write to support styled output Andrew Burgess
2025-08-13 10:25           ` [PATCHv7 0/4] Fix for gdb.Color + pagination AND new gdb.Style API Andrew Burgess
2025-08-13 10:25             ` [PATCHv7 1/4] gdb: allow gdb.Color to work correctly with pagination Andrew Burgess
2025-08-24 16:13               ` Andrew Burgess
2025-08-25 10:21                 ` Tom de Vries
2025-08-26 14:35                   ` [PATCH] gdb/testsuite: work around empty substring bug in expect Andrew Burgess
2025-08-27  6:24                     ` Tom de Vries
2025-08-13 10:29             ` [PATCHv7 2/4] gdb/python: add gdb.Style class Andrew Burgess
2025-08-13 10:30             ` [PATCHv7 4/4] gdb/python: extend gdb.write to support styled output Andrew Burgess
2025-08-13 12:37               ` Eli Zaretskii
2025-08-13 10:34             ` [PATCHv7 3/4] gdb/python: new class gdb.StyleParameterSet Andrew Burgess
2025-08-13 13:05               ` Eli Zaretskii
2025-08-13 13:05               ` Eli Zaretskii
2025-08-27 11:34             ` [PATCHv8 0/3] new gdb.Style API Andrew Burgess
2025-08-27 11:34               ` [PATCHv8 1/3] gdb/python: add gdb.Style class Andrew Burgess
2025-09-16 16:47                 ` Tom Tromey
2025-09-23 16:59                   ` Andrew Burgess
2025-08-27 11:34               ` [PATCHv8 2/3] gdb/python: new class gdb.StyleParameterSet Andrew Burgess
2025-09-16 16:50                 ` Tom Tromey
2025-08-27 11:34               ` [PATCHv8 3/3] gdb/python: extend gdb.write to support styled output Andrew Burgess
2025-09-16 16:56                 ` Tom Tromey
2025-09-23 17:05                   ` Andrew Burgess
2025-09-23 18:38                     ` Tom Tromey
2025-09-23 16:54               ` [PATCHv9 0/3] new gdb.Style API Andrew Burgess
2025-09-23 16:54                 ` [PATCHv9 1/3] gdb/python: add gdb.Style class Andrew Burgess
2025-09-23 16:54                 ` [PATCHv9 2/3] gdb/python: new class gdb.StyleParameterSet Andrew Burgess
2025-09-23 16:54                 ` [PATCHv9 3/3] gdb/python: extend gdb.write to support styled output Andrew Burgess
2025-10-03 19:27                 ` [PATCHv9 0/3] new gdb.Style API Tom Tromey
2025-10-05 12:51                   ` Andrew Burgess
2025-10-05 15:16                     ` Tom de Vries
2025-10-05 17:59                       ` Tom Tromey
2025-10-06  9:01                         ` Andrew Burgess
2025-10-05 18:03                       ` Andrew Burgess
2025-10-05 18:41                         ` Tom de Vries
2025-10-06  9:01                           ` Andrew Burgess
2025-10-06 12:29                             ` Tom de Vries

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86h60tkl1v.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox