diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 16:40:16 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 16:40:16 +0000 |
commit | 3f25952c13d5847d510c0cae22a8ba876638d570 (patch) | |
tree | 02f505f016ed5a1029277dcae520d5e2a75906fb /powerline/renderers/ipython/since_7.py | |
parent | Initial commit. (diff) | |
download | powerline-94b2348b64f996ba22ecd041b5328a1e04f26d70.tar.xz powerline-94b2348b64f996ba22ecd041b5328a1e04f26d70.zip |
Adding upstream version 2.8.3.upstream/2.8.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'powerline/renderers/ipython/since_7.py')
-rw-r--r-- | powerline/renderers/ipython/since_7.py | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/powerline/renderers/ipython/since_7.py b/powerline/renderers/ipython/since_7.py new file mode 100644 index 0000000..ca2ea0e --- /dev/null +++ b/powerline/renderers/ipython/since_7.py @@ -0,0 +1,91 @@ +# vim:fileencoding=utf-8:noet +import operator + +try: + from __builtin__ import reduce +except ImportError: + from functools import reduce + +from pygments.token import Token +from prompt_toolkit.styles import DynamicStyle + +from powerline.renderers.ipython import IPythonRenderer +from powerline.ipython import IPythonInfo +from powerline.colorscheme import ATTR_BOLD, ATTR_ITALIC, ATTR_UNDERLINE + +used_styles = [] +seen = set() + +class PowerlinePromptStyle(DynamicStyle): + @property + def style_rules(self): + return (self.get_style() or self._dummy).style_rules + used_styles + + def invalidation_hash(self): + return (h + 1 for h in tuple(super(PowerlinePromptStyle, self).invalidation_hash())) + + +class IPythonPygmentsRenderer(IPythonRenderer): + reduce_initial = [] + + def __init__(self, **kwargs): + super(IPythonPygmentsRenderer, self).__init__(**kwargs) + self.character_translations[ord(' ')] = ' ' + + def get_segment_info(self, segment_info, mode): + return super(IPythonPygmentsRenderer, self).get_segment_info( + IPythonInfo(segment_info), mode) + + @staticmethod + def hl_join(segments): + return reduce(operator.iadd, segments, []) + + def hl(self, escaped_contents, fg=None, bg=None, attrs=None, *args, **kwargs): + '''Output highlighted chunk. + + This implementation outputs a list containing a single pair + (:py:class:`string`, + :py:class:`powerline.lib.unicode.unicode`). + ''' + guifg = None + guibg = None + att = [] + if fg is not None and fg is not False: + guifg = fg[1] + if bg is not None and bg is not False: + guibg = bg[1] + if attrs: + att = [] + if attrs & ATTR_BOLD: + att.append('bold') + if attrs & ATTR_ITALIC: + att.append('italic') + if attrs & ATTR_UNDERLINE: + att.append('underline') + + fg = (('%06x' % guifg) if guifg is not None else '') + bg = (('%06x' % guibg) if guibg is not None else '') + name = ( + 'pl' + + ''.join(('_a' + attr for attr in att)) + + '_f' + fg + '_b' + bg + ) + + global seen + if not (name in seen): + global used_styles + used_styles += [('pygments.' + name, + ''.join((' ' + attr for attr in att)) + + (' fg:#' + fg if fg != '' else ' fg:') + + (' bg:#' + bg if bg != '' else ' bg:'))] + seen.add(name) + return [((name,), escaped_contents)] + + def hlstyle(self, *args, **kwargs): + return [] + + def get_client_id(self, segment_info): + return id(self) + + +renderer = IPythonPygmentsRenderer |