diff options
author | arcadia-devtools <arcadia-devtools@yandex-team.ru> | 2022-02-12 14:35:15 +0300 |
---|---|---|
committer | arcadia-devtools <arcadia-devtools@yandex-team.ru> | 2022-02-12 14:35:15 +0300 |
commit | 46a8b83899dd321edf511c0483f9c479ce2c1bc4 (patch) | |
tree | e5debc03beecbd10e7d1bf78c889c8d54e8c4523 /contrib/python/prompt-toolkit/py3/prompt_toolkit/formatted_text/ansi.py | |
parent | b56bbcc9f63bf31991a8aa118555ce0c12875a74 (diff) | |
download | ydb-46a8b83899dd321edf511c0483f9c479ce2c1bc4.tar.gz |
intermediate changes
ref:7c971b97c72bbbcbf889118d39017bd14f99365a
Diffstat (limited to 'contrib/python/prompt-toolkit/py3/prompt_toolkit/formatted_text/ansi.py')
-rw-r--r-- | contrib/python/prompt-toolkit/py3/prompt_toolkit/formatted_text/ansi.py | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/contrib/python/prompt-toolkit/py3/prompt_toolkit/formatted_text/ansi.py b/contrib/python/prompt-toolkit/py3/prompt_toolkit/formatted_text/ansi.py index 3d57063357..2a30b09c21 100644 --- a/contrib/python/prompt-toolkit/py3/prompt_toolkit/formatted_text/ansi.py +++ b/contrib/python/prompt-toolkit/py3/prompt_toolkit/formatted_text/ansi.py @@ -1,4 +1,5 @@ -from typing import Generator, List, Optional +from string import Formatter +from typing import Generator, List, Optional, Tuple, Union from prompt_toolkit.output.vt100 import BG_ANSI_COLORS, FG_ANSI_COLORS from prompt_toolkit.output.vt100 import _256_colors as _256_colors_table @@ -207,7 +208,7 @@ class ANSI: # True colors. if n == 2 and len(attrs) >= 3: try: - color_str = "#%02x%02x%02x" % ( + color_str = "#{:02x}{:02x}{:02x}".format( attrs.pop(), attrs.pop(), attrs.pop(), @@ -247,7 +248,7 @@ class ANSI: return " ".join(result) def __repr__(self) -> str: - return "ANSI(%r)" % (self.value,) + return f"ANSI({self.value!r})" def __pt_formatted_text__(self) -> StyleAndTextTuples: return self._formatted_text @@ -257,11 +258,17 @@ class ANSI: Like `str.format`, but make sure that the arguments are properly escaped. (No ANSI escapes can be injected.) """ - # Escape all the arguments. - args = tuple(ansi_escape(a) for a in args) - kwargs = {k: ansi_escape(v) for k, v in kwargs.items()} + return ANSI(FORMATTER.vformat(self.value, args, kwargs)) - return ANSI(self.value.format(*args, **kwargs)) + def __mod__(self, value: object) -> "ANSI": + """ + ANSI('<b>%s</b>') % value + """ + if not isinstance(value, tuple): + value = (value,) + + value = tuple(ansi_escape(i) for i in value) + return ANSI(self.value % value) # Mapping of the ANSI color codes to their names. @@ -272,11 +279,19 @@ _bg_colors = {v: k for k, v in BG_ANSI_COLORS.items()} _256_colors = {} for i, (r, g, b) in enumerate(_256_colors_table.colors): - _256_colors[i] = "#%02x%02x%02x" % (r, g, b) + _256_colors[i] = f"#{r:02x}{g:02x}{b:02x}" -def ansi_escape(text: str) -> str: +def ansi_escape(text: object) -> str: """ Replace characters with a special meaning. """ - return text.replace("\x1b", "?").replace("\b", "?") + return str(text).replace("\x1b", "?").replace("\b", "?") + + +class ANSIFormatter(Formatter): + def format_field(self, value: object, format_spec: str) -> str: + return ansi_escape(format(value, format_spec)) + + +FORMATTER = ANSIFormatter() |