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 <jaraco@jaraco.com>

---------

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 <jaraco@jaraco.com>
This commit is contained in:
Andy Kipp 2024-07-11 13:29:35 +02:00 committed by GitHub
parent 247ecba38a
commit 645c335fc6
Failed to generate hash of commit
2 changed files with 27 additions and 1 deletions

23
news/printx_color.rst Normal file
View file

@ -0,0 +1,23 @@
**Added:**
* builtins: Added color to printx e.g. ``printx('Good', color='GREEN')``.
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* <news item>
**Security:**
* <news item>

View file

@ -2020,11 +2020,14 @@ def format_color(string, **kwargs):
return ansi_partial_color_format(string, style=style) 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 """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 method of the same name. Colors will be formatted if they have not already
been. been.
""" """
if color:
string = f"{{{color.upper()}}}{string}{{RESET}}"
if hasattr(xsh.shell, "shell"): if hasattr(xsh.shell, "shell"):
xsh.shell.shell.print_color(string, **kwargs) xsh.shell.shell.print_color(string, **kwargs)
else: else: