From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id l58SMZH5QmgNSQMAWB0awg (envelope-from ) for ; Fri, 06 Jun 2025 10:22:09 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=gnu.org header.i=@gnu.org header.a=rsa-sha256 header.s=fencepost-gnu-org header.b=hPnOAdMH; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id BAB691E11C; Fri, 6 Jun 2025 10:22:09 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-10.1 required=5.0 tests=ARC_SIGNED,ARC_VALID, BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED, RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE autolearn=ham autolearn_force=no version=4.0.1 Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 8638F1E0C2 for ; Fri, 6 Jun 2025 10:22:08 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3DCAB385AC32 for ; Fri, 6 Jun 2025 14:22:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3DCAB385AC32 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gnu.org header.i=@gnu.org header.a=rsa-sha256 header.s=fencepost-gnu-org header.b=hPnOAdMH Received: from eggs.gnu.org (eggs.gnu.org [IPv6:2001:470:142:3::10]) by sourceware.org (Postfix) with ESMTPS id 47B893858C50 for ; Fri, 6 Jun 2025 14:21:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 47B893858C50 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gnu.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gnu.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 47B893858C50 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:470:142:3::10 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1749219696; cv=none; b=XKxH8y3ZRD/LVN5PI2mSUQENTz/m8+/L7VcgTwCZGIFrXsWsdl0YeDUjEM4ADOk7/RqsbVMVpJQDXzo52s+p4PJvhDTNUjHphFwmre32kunI7KnhgLIwJ0xPQobQNd5KiuNWtawwi9adQzv3ZMleXUNmspXfz84IVz7WqwSxkp0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1749219696; c=relaxed/simple; bh=rIHpkYy3TSfIZ0AHeSGeVUX7Yb7r5AgmikdYlhMZCyY=; h=DKIM-Signature:Date:Message-Id:From:To:Subject; b=G4MRN6G1iNE9mgKv6khUpgXBtf2DvDuoS1qRyXhA8Iws4i3rTFj1qcumG8WRaAH2L9omgdmqhzAXEOan2WHpv4CTEzQiyPtYgUqhbDHNMlCz4+dU7z9nPazTAq4Mzv2fYD7r1H0ciGIx+j6+bxd4lLlmSbZK5g23uSvEQXgftKY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 47B893858C50 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uNXwt-0001dH-Dg; Fri, 06 Jun 2025 10:21:35 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=v/n/hr/SBm8wt9d0GYqBxzJWf7NBok1kJMYPwBwPpJo=; b=hPnOAdMHwS07 fmX95mnYsfV+rNc/I3Haqr4EsELHFPZBMxpFGdWQb2nAKX1R9i7moUQWM6yWXv1UzhXUXvgIZwAD4 g1Ov7EsKKff3WDewQOYLtWl/KcfeV7E2XuQt7/Zzp7EN1b2dJABB2Wbnu1kpox6qwUkCEhquREgUS QdCTuWMKo6yVdVf6HHwL5TM69HfNE1FkH9yMV8Fc97/CQxr7PG9YE7lN31yAMOKtJ3a6/e7/30dk2 3s6nBanpcq30Uve1snRS7P0jsg1kykWaFYxxoqpK+BLwSa6S0w0fq70PMtWKPFZ14jFOkDwOh26co d5UMrtReI8Ziqx1nCrAFkw==; Date: Fri, 06 Jun 2025 17:21:32 +0300 Message-Id: <86h60tkl1v.fsf@gnu.org> From: Eli Zaretskii To: Andrew Burgess Cc: gdb-patches@sourceware.org In-Reply-To: <7034bcb2562c13b580689a221f8c313e39940883.1749216302.git.aburgess@redhat.com> (message from Andrew Burgess on Fri, 6 Jun 2025 14:38:13 +0100) Subject: Re: [PATCHv2 1/2] gdb/python: add gdb.Style class References: <7034bcb2562c13b580689a221f8c313e39940883.1749216302.git.aburgess@redhat.com> X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org > From: Andrew Burgess > Cc: Andrew Burgess > 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) > > 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