Skip to content
Myr

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

3 files+34 added−2 removed

modified:src/click/termui.py+30 added-2 removed
439 unmodified lines
440 else:441 sys.stdout.write("\033[2J\033[1;1H")442443444def _interpret_color(color, offset=0):445 if isinstance(color, int):446 return f"{38 + offset};5;{color:d}"447448 if isinstance(color, (tuple, list)):449 r, g, b = color450 return f"{38 + offset};2;{r:d};{g:d};{b:d}"451452 return str(_ansi_colors[color] + offset)453454455def style(456 text,457 fg=None,458 bg=None,
13 unmodified lines
472473 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))477478 Supported color names:479480 * ``black`` (might be a gray)
13 unmodified lines
494 * ``bright_cyan``495 * ``bright_white``496 * ``reset`` (reset the color code only)497498 If the terminal supports it, color may also be specified as:499500 - 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.504505 See https://en.wikipedia.org/wiki/ANSI_color and506 https://gist.github.com/XVilka/8346728 for more information.507508 :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 lines
522523 .. versionchanged:: 8.0524 A non-string ``message`` is converted to a string.525526 .. versionchanged:: 8.0527 Added support for 256 and RGB color codes.528529 .. versionchanged:: 7.0530 Added support for bright colors.531532 .. versionadded:: 2.0
1 unmodified line
534 if not isinstance(text, str):535 text = str(text)536537 bits = []538539 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}")544545 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}")550551 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 lines
modified:tests/test_utils.py+2 added
60 unmodified lines
61 ({"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 lines
modified:CHANGES.rst+2 added
75 unmodified lines
76- 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`828384Version 7.1.285-------------
635 unmodified lines