forked from Imagelibrary/binutils-gdb
gdb/python/guile: check if styling is disabled in Color.escape_sequence
I noticed that the gdb.Color.escape_sequence() method would produce an
escape sequence even when styling is disabled.
I think this is the wrong choice. Ideally, when styling is
disabled (e.g. with 'set style enabled off'), GDB should not be
producing styled output.
If a GDB extension is using gdb.Color to apply styling to the output,
then currently, the extension should be checking 'show style enabled'
any time Color.escape_sequence() is used. This means lots of code
duplication, and the possibility that some locations will be missed,
which means disabling styling no longer does what it says.
I propose that Color.escape_sequence() should return the empty string
if styling is disabled. A Python extension can then do:
python
c_none = gdb.Color('none')
c_red = gdb.Color('red')
print(c_red.escape_sequence(True)
+ "Text in red."
+ c_none.escape_sequence(True))
end
If styling is enable this will print some red text. And if styling is
disabled, then it will print text in the terminal's default color.
I have applied the same fix to the guile API.
I have extended the tests to cover this case.
Approved-By: Tom Tromey <tom@tromey.com>
This commit is contained in:
@@ -3899,6 +3899,9 @@ Return string to change terminal's color to this.
|
||||
If @var{is_foreground} is @code{#t}, then the returned sequence will change
|
||||
foreground color. Otherwise, the returned sequence will change background
|
||||
color.
|
||||
|
||||
If styling is currently disabled (@pxref{Output Styling,,@kbd{set style
|
||||
enabled}}), then this procedure will return an empty string.
|
||||
@end deffn
|
||||
|
||||
When color is initialized, its color space must be specified. The
|
||||
|
||||
@@ -7101,6 +7101,9 @@ Returns string to change terminal's color to this.
|
||||
If @var{is_foreground} is @code{True}, then the returned sequence will change
|
||||
foreground color. Otherwise, the returned sequence will change background
|
||||
color.
|
||||
|
||||
If styling is currently disabled (@pxref{Output Styling,,@kbd{set style
|
||||
enabled}}), then this method will return an empty string.
|
||||
@end defun
|
||||
|
||||
When color is initialized, its color space must be specified. The
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "language.h"
|
||||
#include "arch-utils.h"
|
||||
#include "guile-internal.h"
|
||||
#include "cli/cli-style.h"
|
||||
|
||||
/* A GDB color. */
|
||||
|
||||
@@ -354,8 +355,14 @@ gdbscm_color_escape_sequence (SCM self, SCM is_fg_scm)
|
||||
const ui_file_style::color &color = coscm_get_color (self);
|
||||
SCM_ASSERT_TYPE (gdbscm_is_bool (is_fg_scm), is_fg_scm, SCM_ARG2, FUNC_NAME,
|
||||
_("boolean"));
|
||||
bool is_fg = gdbscm_is_true (is_fg_scm);
|
||||
std::string s = color.to_ansi (is_fg);
|
||||
|
||||
std::string s;
|
||||
if (term_cli_styling ())
|
||||
{
|
||||
bool is_fg = gdbscm_is_true (is_fg_scm);
|
||||
s = color.to_ansi (is_fg);
|
||||
}
|
||||
|
||||
return gdbscm_scm_from_host_string (s.c_str (), s.size ());
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "python-internal.h"
|
||||
#include "py-color.h"
|
||||
#include "cli/cli-decode.h"
|
||||
#include "cli/cli-style.h"
|
||||
|
||||
/* Colorspace constants and their values. */
|
||||
static struct {
|
||||
@@ -152,8 +153,12 @@ colorpy_escape_sequence (PyObject *self, PyObject *args, PyObject *kwargs)
|
||||
/* The argument parsing ensures we have a bool. */
|
||||
gdb_assert (PyBool_Check (is_fg_obj));
|
||||
|
||||
bool is_fg = is_fg_obj == Py_True;
|
||||
std::string s = gdbpy_get_color (self).to_ansi (is_fg);
|
||||
std::string s;
|
||||
if (term_cli_styling ())
|
||||
{
|
||||
bool is_fg = is_fg_obj == Py_True;
|
||||
s = gdbpy_get_color (self).to_ansi (is_fg);
|
||||
}
|
||||
|
||||
return host_string_to_python_string (s.c_str ()).release ();
|
||||
}
|
||||
|
||||
@@ -20,7 +20,10 @@ load_lib gdb-guile.exp
|
||||
|
||||
require allow_guile_tests
|
||||
|
||||
clean_restart
|
||||
# Start GDB with styling support.
|
||||
with_ansi_styling_terminal {
|
||||
clean_restart
|
||||
}
|
||||
|
||||
gdb_install_guile_utils
|
||||
gdb_install_guile_module
|
||||
@@ -108,3 +111,8 @@ gdb_test [concat "guile " \
|
||||
"\033\\\[31m\033\\\[42mred on green\033\\\[49m red on default\033\\\[39m" \
|
||||
"escape sequences"
|
||||
|
||||
# Ensure that turning styling off means no escape sequences.
|
||||
gdb_test_no_output "set style enabled off"
|
||||
gdb_test_no_output "guile (display (color-escape-sequence c_red #t))"
|
||||
gdb_test_no_output "guile (display (color-escape-sequence c_red #f))"
|
||||
gdb_test_no_output "set style enabled on"
|
||||
|
||||
@@ -19,8 +19,10 @@ load_lib gdb-python.exp
|
||||
|
||||
require allow_python_tests
|
||||
|
||||
# Start with a fresh gdb.
|
||||
clean_restart
|
||||
# Start with a fresh GDB, but enable color support.
|
||||
with_ansi_styling_terminal {
|
||||
clean_restart
|
||||
}
|
||||
|
||||
gdb_test_no_output "python get_color_attrs = lambda c: \"%s %s %s %s %s\" % (str(c), c.colorspace, c.is_none, c.is_indexed, c.is_direct)" \
|
||||
"get_color_attrs helper"
|
||||
@@ -115,6 +117,12 @@ gdb_test [concat "python print (c_red.escape_sequence (is_foreground = True) + "
|
||||
"\033\\\[31m\033\\\[42mred on green\033\\\[49m red on default\033\\\[39m" \
|
||||
"escape sequences using keyword arguments"
|
||||
|
||||
# Ensure that turning styling off means no escape sequences.
|
||||
gdb_test_no_output "set style enabled off"
|
||||
gdb_test_no_output "python print (c_red.escape_sequence (True), end='')"
|
||||
gdb_test_no_output "python print (c_red.escape_sequence (False), end='')"
|
||||
gdb_test_no_output "set style enabled on"
|
||||
|
||||
gdb_test_multiline "Try to sub-class gdb.Color" \
|
||||
"python" "" \
|
||||
"class my_color(gdb.Color):" "" \
|
||||
|
||||
Reference in New Issue
Block a user