Files
binutils-gdb/gdb/testsuite/gdb.python/py-color-pagination.py
Andrew Burgess 3c72459681 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>
2025-10-05 13:48:06 +01:00

83 lines
2.4 KiB
Python

# Copyright (C) 2025 Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import gdb
basic_colors = ["black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"]
def write(mode, text):
if mode == "write":
gdb.write(text)
else:
print(text, end="")
class ColorTester(gdb.Command):
def __init__(self):
super().__init__("color-fill", gdb.COMMAND_USER)
def invoke(self, args, from_tty):
mode = args
str = "<" + "-" * 78 + ">"
for i in range(0, 20):
for color_name in basic_colors:
c = gdb.Color(color_name)
write(mode, c.escape_sequence(True))
write(mode, str)
default = gdb.Color("none")
write(mode, default.escape_sequence(True))
write(mode, "\n")
class StyleTester(gdb.Command):
def __init__(self):
super().__init__("style-fill", gdb.COMMAND_USER)
def invoke(self, args, from_tty):
mode = args
str = "<" + "-" * 78 + ">"
for i in range(0, 20):
for color_name in basic_colors:
c = gdb.Color(color_name)
s = gdb.Style(foreground=c)
write(mode, s.escape_sequence())
write(mode, str)
default = gdb.Style()
write(mode, default.escape_sequence())
write(mode, "\n")
class StyleTester2(gdb.Command):
def __init__(self):
super().__init__("style-fill-v2", gdb.COMMAND_USER)
def invoke(self, args, from_tty):
str = "<" + "-" * 78 + ">"
for i in range(0, 20):
for color_name in basic_colors:
c = gdb.Color(color_name)
s = gdb.Style(foreground=c)
gdb.write(str, style=s)
gdb.write("\n")
ColorTester()
StyleTester()
StyleTester2()