Merge pull request #1429 from lmjohns3/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
click.style: add support for 256/RGB colors
authored by David Lord · committed by GitHub · parents 4ae204e, 5949dad · Browse files · 3 files·+34 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+34 added−2 removed
- tests:+2 added−0 removed
- other:+32 added−2 removed
Provenance
- Commit
0d43812- Tree
a2ffb05- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
4ae204e- Parent 2
5949dad
3 files changed+34 added−2 removed
modified:src/click/termui.py+30 added-2 removed
439 unmodified lines440 else:441 sys.stdout.write("\033[2J\033[1;1H")442 443 444+def _interpret_color(color, offset=0):445+ if isinstance(color, int):446+ return f"{38 + offset};5;{color:d}"447+448+ if isinstance(color, (tuple, list)):449+ r, g, b = color450+ return f"{38 + offset};2;{r:d};{g:d};{b:d}"451+452+ return str(_ansi_colors[color] + offset)453+454+455 def style(456 text,457 fg=None,458 bg=None,13 unmodified lines472 473 click.echo(click.style('Hello World!', fg='green'))474 click.echo(click.style('ATTENTION!', blink=True))475 click.echo(click.style('Some things', reverse=True, fg='cyan'))476+ click.echo(click.style('More colors', fg=(255, 12, 128), bg=117))477 478 Supported color names:479 480 * ``black`` (might be a gray)13 unmodified lines494 * ``bright_cyan``495 * ``bright_white``496 * ``reset`` (reset the color code only)497 498+ If the terminal supports it, color may also be specified as:499+500+ - An integer in the interval [0, 255]. The terminal must support501+ 8-bit/256-color mode.502+ - An RGB tuple of three integers in [0, 255]. The terminal must503+ support 24-bit/true-color mode.504+505+ See https://en.wikipedia.org/wiki/ANSI_color and506+ https://gist.github.com/XVilka/8346728 for more information.507+508 :param text: the string to style with ansi codes.509 :param fg: if provided this will become the foreground color.510 :param bg: if provided this will become the background color.511 :param bold: if provided this will enable or disable bold mode.10 unmodified lines522 523 .. versionchanged:: 8.0524 A non-string ``message`` is converted to a string.525 526+ .. versionchanged:: 8.0527+ Added support for 256 and RGB color codes.528+529 .. versionchanged:: 7.0530 Added support for bright colors.531 532 .. versionadded:: 2.01 unmodified line534 if not isinstance(text, str):535 text = str(text)536 537 bits = []538+539 if fg:540 try:515- bits.append(f"\033[{_ansi_colors[fg]}m")541+ bits.append(f"\033[{_interpret_color(fg)}m")542 except KeyError:543 raise TypeError(f"Unknown color {fg!r}")544+545 if bg:546 try:520- bits.append(f"\033[{_ansi_colors[bg] + 10}m")547+ bits.append(f"\033[{_interpret_color(bg, 10)}m")548 except KeyError:549 raise TypeError(f"Unknown color {bg!r}")550+551 if bold is not None:552 bits.append(f"\033[{1 if bold else 22}m")553 if dim is not None:554 bits.append(f"\033[{2 if dim else 22}m")182 unmodified linesmodified:tests/test_utils.py+2 added
60 unmodified lines61 ({"bg": "blue"}, "\x1b[44mx y\x1b[0m"),62 ({"bg": "magenta"}, "\x1b[45mx y\x1b[0m"),63 ({"bg": "cyan"}, "\x1b[46mx y\x1b[0m"),64 ({"bg": "white"}, "\x1b[47mx y\x1b[0m"),65+ ({"bg": 91}, "\x1b[48;5;91mx y\x1b[0m"),66+ ({"bg": (135, 0, 175)}, "\x1b[48;2;135;0;175mx y\x1b[0m"),67 ({"blink": True}, "\x1b[5mx y\x1b[0m"),68 ({"underline": True}, "\x1b[4mx y\x1b[0m"),69 ({"bold": True}, "\x1b[1mx y\x1b[0m"),70 ({"dim": True}, "\x1b[2mx y\x1b[0m"),346 unmodified linesmodified:CHANGES.rst+2 added
75 unmodified lines76 - If only a name is passed to ``option()``, Click suggests renaming it77 to ``--name``. :pr:`1355`78 - A context's ``show_default`` parameter defaults to the value from79 the parent context. :issue:`1565`80+- ``click.style()`` can output 256 and RGB color codes. Most modern81+ terminals support these codes. :pr:`1429`82 83 84 Version 7.1.285 -------------635 unmodified lines