diff options
| author | nkozlovskiy <[email protected]> | 2023-09-29 12:24:06 +0300 |
|---|---|---|
| committer | nkozlovskiy <[email protected]> | 2023-09-29 12:41:34 +0300 |
| commit | e0e3e1717e3d33762ce61950504f9637a6e669ed (patch) | |
| tree | bca3ff6939b10ed60c3d5c12439963a1146b9711 /contrib/python/prompt-toolkit/py2/prompt_toolkit/styles | |
| parent | 38f2c5852db84c7b4d83adfcb009eb61541d1ccd (diff) | |
add ydb deps
Diffstat (limited to 'contrib/python/prompt-toolkit/py2/prompt_toolkit/styles')
6 files changed, 475 insertions, 0 deletions
diff --git a/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/__init__.py b/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/__init__.py new file mode 100644 index 00000000000..9d641f04475 --- /dev/null +++ b/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/__init__.py @@ -0,0 +1,21 @@ +""" +Styling for prompt_toolkit applications. +""" +from __future__ import unicode_literals + +from .base import * +from .defaults import * +from .from_dict import * +from .from_pygments import * +from .utils import * + + +#: The default built-in style. +#: (For backwards compatibility, when Pygments is installed, this includes the +#: default Pygments style.) +try: + import pygments +except ImportError: + DEFAULT_STYLE = style_from_dict(DEFAULT_STYLE_EXTENSIONS) +else: + DEFAULT_STYLE = style_from_pygments() diff --git a/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/base.py b/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/base.py new file mode 100644 index 00000000000..e9ddaa524dc --- /dev/null +++ b/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/base.py @@ -0,0 +1,86 @@ +""" +The base classes for the styling. +""" +from __future__ import unicode_literals +from abc import ABCMeta, abstractmethod +from collections import namedtuple +from six import with_metaclass + +__all__ = ( + 'Attrs', + 'DEFAULT_ATTRS', + 'ANSI_COLOR_NAMES', + 'Style', + 'DynamicStyle', +) + + +#: Style attributes. +Attrs = namedtuple('Attrs', 'color bgcolor bold underline italic blink reverse') +""" +:param color: Hexadecimal string. E.g. '000000' or Ansi color name: e.g. 'ansiblue' +:param bgcolor: Hexadecimal string. E.g. 'ffffff' or Ansi color name: e.g. 'ansired' +:param bold: Boolean +:param underline: Boolean +:param italic: Boolean +:param blink: Boolean +:param reverse: Boolean +""" + +#: The default `Attrs`. +DEFAULT_ATTRS = Attrs(color=None, bgcolor=None, bold=False, underline=False, + italic=False, blink=False, reverse=False) + + +#: ``Attrs.bgcolor/fgcolor`` can be in either 'ffffff' format, or can be any of +#: the following in case we want to take colors from the 8/16 color palette. +#: Usually, in that case, the terminal application allows to configure the RGB +#: values for these names. +ANSI_COLOR_NAMES = [ + 'ansiblack', 'ansiwhite', 'ansidefault', + + # Low intensity. + 'ansired', 'ansigreen', 'ansiyellow', 'ansiblue', 'ansifuchsia', 'ansiturquoise', 'ansilightgray', + + # High intensity. (Not supported everywhere.) + 'ansidarkgray', 'ansidarkred', 'ansidarkgreen', 'ansibrown', 'ansidarkblue', + 'ansipurple', 'ansiteal', +] + + +class Style(with_metaclass(ABCMeta, object)): + """ + Abstract base class for prompt_toolkit styles. + """ + @abstractmethod + def get_attrs_for_token(self, token): + """ + Return :class:`.Attrs` for the given token. + """ + + @abstractmethod + def invalidation_hash(self): + """ + Invalidation hash for the style. When this changes over time, the + renderer knows that something in the style changed, and that everything + has to be redrawn. + """ + + +class DynamicStyle(Style): + """ + Style class that can dynamically returns an other Style. + + :param get_style: Callable that returns a :class:`.Style` instance. + """ + def __init__(self, get_style): + self.get_style = get_style + + def get_attrs_for_token(self, token): + style = self.get_style() + assert isinstance(style, Style) + + return style.get_attrs_for_token(token) + + def invalidation_hash(self): + return self.get_style().invalidation_hash() diff --git a/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/defaults.py b/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/defaults.py new file mode 100644 index 00000000000..5357eaa111b --- /dev/null +++ b/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/defaults.py @@ -0,0 +1,95 @@ +""" +The default styling. +""" +from __future__ import unicode_literals + +from prompt_toolkit.token import Token + +__all__ = ( + 'DEFAULT_STYLE_EXTENSIONS', + 'default_style_extensions', +) + + +#: Styling of prompt-toolkit specific tokens, that are not know by the default +#: Pygments style. +DEFAULT_STYLE_EXTENSIONS = { + # Highlighting of search matches in document. + Token.SearchMatch: 'noinherit reverse', + Token.SearchMatch.Current: 'noinherit #ffffff bg:#448844 underline', + + # Highlighting of select text in document. + Token.SelectedText: 'reverse', + + Token.CursorColumn: 'bg:#dddddd', + Token.CursorLine: 'underline', + Token.ColorColumn: 'bg:#ccaacc', + + # Highlighting of matching brackets. + Token.MatchingBracket: '', + Token.MatchingBracket.Other: '#000000 bg:#aacccc', + Token.MatchingBracket.Cursor: '#ff8888 bg:#880000', + + Token.MultipleCursors.Cursor: '#000000 bg:#ccccaa', + + # Line numbers. + Token.LineNumber: '#888888', + Token.LineNumber.Current: 'bold', + Token.Tilde: '#8888ff', + + # Default prompt. + Token.Prompt: '', + Token.Prompt.Arg: 'noinherit', + Token.Prompt.Search: 'noinherit', + Token.Prompt.Search.Text: '', + + # Search toolbar. + Token.Toolbar.Search: 'bold', + Token.Toolbar.Search.Text: 'nobold', + + # System toolbar + Token.Toolbar.System: 'bold', + Token.Toolbar.System.Text: 'nobold', + + # "arg" toolbar. + Token.Toolbar.Arg: 'bold', + Token.Toolbar.Arg.Text: 'nobold', + + # Validation toolbar. + Token.Toolbar.Validation: 'bg:#550000 #ffffff', + Token.WindowTooSmall: 'bg:#550000 #ffffff', + + # Completions toolbar. + Token.Toolbar.Completions: 'bg:#bbbbbb #000000', + Token.Toolbar.Completions.Arrow: 'bg:#bbbbbb #000000 bold', + Token.Toolbar.Completions.Completion: 'bg:#bbbbbb #000000', + Token.Toolbar.Completions.Completion.Current: 'bg:#444444 #ffffff', + + # Completions menu. + Token.Menu.Completions: 'bg:#bbbbbb #000000', + Token.Menu.Completions.Completion: '', + Token.Menu.Completions.Completion.Current: 'bg:#888888 #ffffff', + Token.Menu.Completions.Meta: 'bg:#999999 #000000', + Token.Menu.Completions.Meta.Current: 'bg:#aaaaaa #000000', + Token.Menu.Completions.MultiColumnMeta: 'bg:#aaaaaa #000000', + + # Scrollbars. + Token.Scrollbar: 'bg:#888888', + Token.Scrollbar.Button: 'bg:#444444', + Token.Scrollbar.Arrow: 'bg:#222222 #888888 bold', + + # Auto suggestion text. + Token.AutoSuggestion: '#666666', + + # Trailing whitespace and tabs. + Token.TrailingWhiteSpace: '#999999', + Token.Tab: '#999999', + + # When Control-C has been pressed. Grayed. + Token.Aborted: '#888888', + + # Entering a Vi digraph. + Token.Digraph: '#4444ff', +} + +default_style_extensions = DEFAULT_STYLE_EXTENSIONS # Old name. diff --git a/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/from_dict.py b/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/from_dict.py new file mode 100644 index 00000000000..b50325710f8 --- /dev/null +++ b/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/from_dict.py @@ -0,0 +1,151 @@ +""" +Tool for creating styles from a dictionary. + +This is very similar to the Pygments style dictionary, with some additions: +- Support for reverse and blink. +- Support for ANSI color names. (These will map directly to the 16 terminal + colors.) +""" +try: + from collections.abc import Mapping +except ImportError: + from collections import Mapping + +from .base import Style, DEFAULT_ATTRS, ANSI_COLOR_NAMES +from .defaults import DEFAULT_STYLE_EXTENSIONS +from .utils import merge_attrs, split_token_in_parts +from six.moves import range + +__all__ = ( + 'style_from_dict', +) + + +def _colorformat(text): + """ + Parse/validate color format. + + Like in Pygments, but also support the ANSI color names. + (These will map to the colors of the 16 color palette.) + """ + if text[0:1] == '#': + col = text[1:] + if col in ANSI_COLOR_NAMES: + return col + elif len(col) == 6: + return col + elif len(col) == 3: + return col[0]*2 + col[1]*2 + col[2]*2 + elif text == '': + return text + + raise ValueError('Wrong color format %r' % text) + + +def style_from_dict(style_dict, include_defaults=True): + """ + Create a ``Style`` instance from a dictionary or other mapping. + + The dictionary is equivalent to the ``Style.styles`` dictionary from + pygments, with a few additions: it supports 'reverse' and 'blink'. + + Usage:: + + style_from_dict({ + Token: '#ff0000 bold underline', + Token.Title: 'blink', + Token.SomethingElse: 'reverse', + }) + + :param include_defaults: Include the defaults (built-in) styling for + selected text, etc...) + """ + assert isinstance(style_dict, Mapping) + + if include_defaults: + s2 = {} + s2.update(DEFAULT_STYLE_EXTENSIONS) + s2.update(style_dict) + style_dict = s2 + + # Expand token inheritance and turn style description into Attrs. + token_to_attrs = {} + + # (Loop through the tokens in order. Sorting makes sure that + # we process the parent first.) + for ttype, styledef in sorted(style_dict.items()): + # Start from parent Attrs or default Attrs. + attrs = DEFAULT_ATTRS + + if 'noinherit' not in styledef: + for i in range(1, len(ttype) + 1): + try: + attrs = token_to_attrs[ttype[:-i]] + except KeyError: + pass + else: + break + + # Now update with the given attributes. + for part in styledef.split(): + if part == 'noinherit': + pass + elif part == 'bold': + attrs = attrs._replace(bold=True) + elif part == 'nobold': + attrs = attrs._replace(bold=False) + elif part == 'italic': + attrs = attrs._replace(italic=True) + elif part == 'noitalic': + attrs = attrs._replace(italic=False) + elif part == 'underline': + attrs = attrs._replace(underline=True) + elif part == 'nounderline': + attrs = attrs._replace(underline=False) + + # prompt_toolkit extensions. Not in Pygments. + elif part == 'blink': + attrs = attrs._replace(blink=True) + elif part == 'noblink': + attrs = attrs._replace(blink=False) + elif part == 'reverse': + attrs = attrs._replace(reverse=True) + elif part == 'noreverse': + attrs = attrs._replace(reverse=False) + + # Pygments properties that we ignore. + elif part in ('roman', 'sans', 'mono'): + pass + elif part.startswith('border:'): + pass + + # Colors. + + elif part.startswith('bg:'): + attrs = attrs._replace(bgcolor=_colorformat(part[3:])) + else: + attrs = attrs._replace(color=_colorformat(part)) + + token_to_attrs[ttype] = attrs + + return _StyleFromDict(token_to_attrs) + + +class _StyleFromDict(Style): + """ + Turn a dictionary that maps `Token` to `Attrs` into a style class. + + :param token_to_attrs: Dictionary that maps `Token` to `Attrs`. + """ + def __init__(self, token_to_attrs): + self.token_to_attrs = token_to_attrs + + def get_attrs_for_token(self, token): + # Split Token. + list_of_attrs = [] + for token in split_token_in_parts(token): + list_of_attrs.append(self.token_to_attrs.get(token, DEFAULT_ATTRS)) + return merge_attrs(list_of_attrs) + + def invalidation_hash(self): + return id(self.token_to_attrs) diff --git a/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/from_pygments.py b/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/from_pygments.py new file mode 100644 index 00000000000..0af8c53cf2d --- /dev/null +++ b/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/from_pygments.py @@ -0,0 +1,77 @@ +""" +Adaptor for building prompt_toolkit styles, starting from a Pygments style. + +Usage:: + + from pygments.styles.tango import TangoStyle + style = style_from_pygments(pygments_style_cls=TangoStyle) +""" +from __future__ import unicode_literals + +from .base import Style +from .from_dict import style_from_dict + +__all__ = ( + 'PygmentsStyle', + 'style_from_pygments', +) + + +# Following imports are only needed when a ``PygmentsStyle`` class is used. +try: + from pygments.style import Style as pygments_Style + from pygments.styles.default import DefaultStyle as pygments_DefaultStyle +except ImportError: + pygments_Style = None + pygments_DefaultStyle = None + + +def style_from_pygments(style_cls=pygments_DefaultStyle, + style_dict=None, + include_defaults=True): + """ + Shortcut to create a :class:`.Style` instance from a Pygments style class + and a style dictionary. + + Example:: + + from prompt_toolkit.styles.from_pygments import style_from_pygments + from pygments.styles import get_style_by_name + style = style_from_pygments(get_style_by_name('monokai')) + + :param style_cls: Pygments style class to start from. + :param style_dict: Dictionary for this style. `{Token: style}`. + :param include_defaults: (`bool`) Include prompt_toolkit extensions. + """ + assert style_dict is None or isinstance(style_dict, dict) + assert style_cls is None or issubclass(style_cls, pygments_Style) + + styles_dict = {} + + if style_cls is not None: + styles_dict.update(style_cls.styles) + + if style_dict is not None: + styles_dict.update(style_dict) + + return style_from_dict(styles_dict, include_defaults=include_defaults) + + +class PygmentsStyle(Style): + " Deprecated. " + def __new__(cls, pygments_style_cls): + assert issubclass(pygments_style_cls, pygments_Style) + return style_from_dict(pygments_style_cls.styles) + + def invalidation_hash(self): + pass + + @classmethod + def from_defaults(cls, style_dict=None, + pygments_style_cls=pygments_DefaultStyle, + include_extensions=True): + " Deprecated. " + return style_from_pygments( + style_cls=pygments_style_cls, + style_dict=style_dict, + include_defaults=include_extensions) diff --git a/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/utils.py b/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/utils.py new file mode 100644 index 00000000000..6087e76abe7 --- /dev/null +++ b/contrib/python/prompt-toolkit/py2/prompt_toolkit/styles/utils.py @@ -0,0 +1,45 @@ +from __future__ import unicode_literals +from .base import DEFAULT_ATTRS, Attrs + +__all__ = ( + 'split_token_in_parts', + 'merge_attrs', +) + + +def split_token_in_parts(token): + """ + Take a Token, and turn it in a list of tokens, by splitting + it on ':' (taking that as a separator.) + """ + result = [] + current = [] + for part in token + (':', ): + if part == ':': + if current: + result.append(tuple(current)) + current = [] + else: + current.append(part) + + return result + + +def merge_attrs(list_of_attrs): + """ + Take a list of :class:`.Attrs` instances and merge them into one. + Every `Attr` in the list can override the styling of the previous one. + """ + result = DEFAULT_ATTRS + + for attr in list_of_attrs: + result = Attrs( + color=attr.color or result.color, + bgcolor=attr.bgcolor or result.bgcolor, + bold=attr.bold or result.bold, + underline=attr.underline or result.underline, + italic=attr.italic or result.italic, + blink=attr.blink or result.blink, + reverse=attr.reverse or result.reverse) + + return result |
