codekingpro/portable-devtools
114k
1"""2SPDX-License-Identifier: BSD-3-Clause3 4A vendored copy of click.style() @ 4f7b2555"""6import typing as t7 8_ansi_colors = {9 "black": 30,10 "red": 31,11 "green": 32,12 "yellow": 33,13 "blue": 34,14 "magenta": 35,15 "cyan": 36,16 "white": 37,17 "reset": 39,18 "bright_black": 90,19 "bright_red": 91,20 "bright_green": 92,21 "bright_yellow": 93,22 "bright_blue": 94,23 "bright_magenta": 95,24 "bright_cyan": 96,25 "bright_white": 97,26}27_ansi_reset_all = "\033[0m"28 29 30def _interpret_color(31 color: t.Union[int, t.Tuple[int, int, int], str], offset: int = 032) -> str:33 if isinstance(color, int):34 return f"{38 + offset};5;{color:d}"35 36 if isinstance(color, (tuple, list)):37 r, g, b = color38 return f"{38 + offset};2;{r:d};{g:d};{b:d}"39 40 return str(_ansi_colors[color] + offset)41 42 43def style(44 text: t.Any,45 fg: t.Optional[t.Union[int, t.Tuple[int, int, int], str]] = None,46 bg: t.Optional[t.Union[int, t.Tuple[int, int, int], str]] = None,47 bold: t.Optional[bool] = None,48 dim: t.Optional[bool] = None,49 underline: t.Optional[bool] = None,50 overline: t.Optional[bool] = None,51 italic: t.Optional[bool] = None,52 blink: t.Optional[bool] = None,53 reverse: t.Optional[bool] = None,54 strikethrough: t.Optional[bool] = None,55 reset: bool = True,56) -> str:57 """Styles a text with ANSI styles and returns the new string. By58 default the styling is self contained which means that at the end59 of the string a reset code is issued. This can be prevented by60 passing ``reset=False``.61 Examples::62 click.echo(click.style('Hello World!', fg='green'))63 click.echo(click.style('ATTENTION!', blink=True))64 click.echo(click.style('Some things', reverse=True, fg='cyan'))65 click.echo(click.style('More colors', fg=(255, 12, 128), bg=117))66 Supported color names:67 * ``black`` (might be a gray)68 * ``red``69 * ``green``70 * ``yellow`` (might be an orange)71 * ``blue``72 * ``magenta``73 * ``cyan``74 * ``white`` (might be light gray)75 * ``bright_black``76 * ``bright_red``77 * ``bright_green``78 * ``bright_yellow``79 * ``bright_blue``80 * ``bright_magenta``81 * ``bright_cyan``82 * ``bright_white``83 * ``reset`` (reset the color code only)84 If the terminal supports it, color may also be specified as:85 - An integer in the interval [0, 255]. The terminal must support86 8-bit/256-color mode.87 - An RGB tuple of three integers in [0, 255]. The terminal must88 support 24-bit/true-color mode.89 See https://en.wikipedia.org/wiki/ANSI_color and90 https://gist.github.com/XVilka/8346728 for more information.91 :param text: the string to style with ansi codes.92 :param fg: if provided this will become the foreground color.93 :param bg: if provided this will become the background color.94 :param bold: if provided this will enable or disable bold mode.95 :param dim: if provided this will enable or disable dim mode. This is96 badly supported.97 :param underline: if provided this will enable or disable underline.98 :param overline: if provided this will enable or disable overline.99 :param italic: if provided this will enable or disable italic.100 :param blink: if provided this will enable or disable blinking.101 :param reverse: if provided this will enable or disable inverse102 rendering (foreground becomes background and the103 other way round).104 :param strikethrough: if provided this will enable or disable105 striking through text.106 :param reset: by default a reset-all code is added at the end of the107 string which means that styles do not carry over. This108 can be disabled to compose styles.109 .. versionchanged:: 8.0110 A non-string ``message`` is converted to a string.111 .. versionchanged:: 8.0112 Added support for 256 and RGB color codes.113 .. versionchanged:: 8.0114 Added the ``strikethrough``, ``italic``, and ``overline``115 parameters.116 .. versionchanged:: 7.0117 Added support for bright colors.118 .. versionadded:: 2.0119 """120 if not isinstance(text, str):121 text = str(text)122 123 bits = []124 125 if fg:126 try:127 bits.append(f"\033[{_interpret_color(fg)}m")128 except KeyError:129 raise TypeError(f"Unknown color {fg!r}") from None130 131 if bg:132 try:133 bits.append(f"\033[{_interpret_color(bg, 10)}m")134 except KeyError:135 raise TypeError(f"Unknown color {bg!r}") from None136 137 if bold is not None:138 bits.append(f"\033[{1 if bold else 22}m")139 if dim is not None:140 bits.append(f"\033[{2 if dim else 22}m")141 if underline is not None:142 bits.append(f"\033[{4 if underline else 24}m")143 if overline is not None:144 bits.append(f"\033[{53 if overline else 55}m")145 if italic is not None:146 bits.append(f"\033[{3 if italic else 23}m")147 if blink is not None:148 bits.append(f"\033[{5 if blink else 25}m")149 if reverse is not None:150 bits.append(f"\033[{7 if reverse else 27}m")151 if strikethrough is not None:152 bits.append(f"\033[{9 if strikethrough else 29}m")153 bits.append(text)154 if reset:155 bits.append(_ansi_reset_all)156 return "".join(bits)157 158 159__all__ = ["style"]160 