]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/python/guile: check if styling is disabled in Color.escape_sequence
authorAndrew Burgess <aburgess@redhat.com>
Tue, 29 Apr 2025 16:57:06 +0000 (17:57 +0100)
committerAndrew Burgess <aburgess@redhat.com>
Tue, 6 May 2025 10:24:28 +0000 (11:24 +0100)
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>
gdb/doc/guile.texi
gdb/doc/python.texi
gdb/guile/scm-color.c
gdb/python/py-color.c
gdb/testsuite/gdb.guile/scm-color.exp
gdb/testsuite/gdb.python/py-color.exp

index c6808efa432bb96212f4f019f8b7789a52de77f4..c6d889fb692da8ced25926f93372f55ced14687b 100644 (file)
@@ -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
index afbd62f192c1efc517c9d23b15d567d16f9b6069..7bb650347f7735217f90932b81be9b1e3a5c40d8 100644 (file)
@@ -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
index 4850575f512c786cb75cb103f374f28e235243ac..cde22e55bf4d8808f7a96b0834bd232b4f9f1f0a 100644 (file)
@@ -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 ());
 }
 
index e208506c9d35ce4f13c5c3f65033c93d3c26e96e..3bbd22d97dfa198be6a1722e7719de81692b44e7 100644 (file)
@@ -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 ();
 }
index bbc9e7185b75c9ff0f27da849159c1f960f52466..578f712201d122a53f60f55a41d75c976647b91b 100644 (file)
@@ -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"
index 3563d2212003107c2356e0caa7debc7d5b269fa0..2601cf3783685ec584f0f9e3f3471ee9c1e2e156 100644 (file)
@@ -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):" "" \