From 645c335fc6cf3088feb6e657b298d01929e23e55 Mon Sep 17 00:00:00 2001 From: Andy Kipp Date: Thu, 11 Jul 2024 13:29:35 +0200 Subject: [PATCH] builtins: add color to printx (#5588) * color * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update xonsh/tools.py Co-authored-by: Jason R. Coombs --------- Co-authored-by: a <1@1.1> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Jason R. Coombs --- news/printx_color.rst | 23 +++++++++++++++++++++++ xonsh/tools.py | 5 ++++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 news/printx_color.rst diff --git a/news/printx_color.rst b/news/printx_color.rst new file mode 100644 index 000000000..9de621e52 --- /dev/null +++ b/news/printx_color.rst @@ -0,0 +1,23 @@ +**Added:** + +* builtins: Added color to printx e.g. ``printx('Good', color='GREEN')``. + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/xonsh/tools.py b/xonsh/tools.py index 1001b1301..4d4e7d90b 100644 --- a/xonsh/tools.py +++ b/xonsh/tools.py @@ -2020,11 +2020,14 @@ def format_color(string, **kwargs): return ansi_partial_color_format(string, style=style) -def print_color(string, **kwargs): +def print_color(string, color=None, **kwargs): """Prints a string that may contain colors. This dispatched to the shell method of the same name. Colors will be formatted if they have not already been. """ + if color: + string = f"{{{color.upper()}}}{string}{{RESET}}" + if hasattr(xsh.shell, "shell"): xsh.shell.shell.print_color(string, **kwargs) else: