Python pygments.styles.get_style_by_name() Examples

The following are 30 code examples of pygments.styles.get_style_by_name(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pygments.styles , or try the search function .
Example #1
Source File: styles.py    From Computable with MIT License 6 votes vote down vote up
def get_colors(stylename):
    """Construct the keys to be used building the base stylesheet
    from a templatee."""
    style = get_style_by_name(stylename)
    fgcolor = style.style_for_token(Token.Text)['color'] or ''
    if len(fgcolor) in (3,6):
        # could be 'abcdef' or 'ace' hex, which needs '#' prefix
        try:
            int(fgcolor, 16)
        except TypeError:
            pass
        else:
            fgcolor = "#"+fgcolor

    return dict(
        bgcolor = style.background_color,
        select = style.highlight_color,
        fgcolor = fgcolor
    ) 
Example #2
Source File: styles.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_colors(stylename):
    """Construct the keys to be used building the base stylesheet
    from a templatee."""
    style = get_style_by_name(stylename)
    fgcolor = style.style_for_token(Token.Text)['color'] or ''
    if len(fgcolor) in (3,6):
        # could be 'abcdef' or 'ace' hex, which needs '#' prefix
        try:
            int(fgcolor, 16)
        except TypeError:
            pass
        else:
            fgcolor = "#"+fgcolor

    return dict(
        bgcolor = style.background_color,
        select = style.highlight_color,
        fgcolor = fgcolor
    ) 
Example #3
Source File: printers.py    From fuzzowski with GNU General Public License v2.0 6 votes vote down vote up
def print_packets(path: list, nodes: dict) -> None:
    tokens = []
    for e in path[:-1]:
        node = nodes[e.dst]
        p = node.render()
        line = '{} = {}'.format(node.name.replace('-', '_'), repr(p))
        tokens.extend(list(pygments.lex(line, lexer=Python3Lexer())))

    # p = self.fuzz_node.render()
    node = nodes[path[-1].dst]
    p = node.render()
    line = '{} = {}'.format(node.name.replace('-', '_'), repr(p))

    print(pygments.highlight(line, Python3Lexer(), Terminal256Formatter(style='rrt')))

    # tokens.extend(list(pygments.lex(line, lexer=Python3Lexer())))
    # style = style_from_pygments_cls(get_style_by_name('colorful'))
    # print_formatted_text(PygmentsTokens(tokens), style=style)


# --------------------------------------------------------------- # 
Example #4
Source File: arm.py    From deen with Apache License 2.0 6 votes vote down vote up
def _syntax_highlighting(self, data):
        try:
            from pygments import highlight
            from pygments.lexers import GasLexer
            from pygments.formatters import TerminalFormatter, Terminal256Formatter
            from pygments.styles import get_style_by_name
            style = get_style_by_name('colorful')
            import curses
            curses.setupterm()
            if curses.tigetnum('colors') >= 256:
                FORMATTER = Terminal256Formatter(style=style)
            else:
                FORMATTER = TerminalFormatter()
            # When pygments is available, we
            # can print the disassembled
            # instructions with syntax
            # highlighting.
            data = highlight(data, GasLexer(), FORMATTER)
        except ImportError:
            pass
        finally:
            data = data.encode()
        return data 
Example #5
Source File: styles.py    From qtconsole with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_colors(stylename):
    """Construct the keys to be used building the base stylesheet
    from a templatee."""
    style = get_style_by_name(stylename)
    fgcolor = style.style_for_token(Token.Text)['color'] or ''
    if len(fgcolor) in (3,6):
        # could be 'abcdef' or 'ace' hex, which needs '#' prefix
        try:
            int(fgcolor, 16)
        except TypeError:
            pass
        else:
            fgcolor = "#"+fgcolor

    return dict(
        bgcolor = style.background_color,
        select = style.highlight_color,
        fgcolor = fgcolor
    ) 
Example #6
Source File: ipython.py    From prettyprinter with MIT License 6 votes vote down vote up
def pygments_style_from_name_or_cls(name_or_cls, ishell):
    if name_or_cls == 'legacy':
        legacy = ishell.colors.lower()
        if legacy == 'linux':
            return get_style_by_name('monokai')
        elif legacy == 'lightbg':
            return get_style_by_name('pastie')
        elif legacy == 'neutral':
            return get_style_by_name('default')
        elif legacy == 'nocolor':
            return _NoStyle
        else:
            raise ValueError('Got unknown colors: ', legacy)
    else:
        if isinstance(name_or_cls, str):
            return get_style_by_name(name_or_cls)
        else:
            return name_or_cls 
Example #7
Source File: pygments.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def style_from_pygments_cls(pygments_style_cls: Type["PygmentsStyle"]) -> Style:
    """
    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_cls
        from pygments.styles import get_style_by_name
        style = style_from_pygments_cls(get_style_by_name('monokai'))

    :param pygments_style_cls: Pygments style class to start from.
    """
    # Import inline.
    from pygments.style import Style as PygmentsStyle

    assert issubclass(pygments_style_cls, PygmentsStyle)

    return style_from_pygments_dict(pygments_style_cls.styles) 
Example #8
Source File: pygments_sh.py    From Turing with MIT License 6 votes vote down vote up
def _update_style(self):
        """ Sets the style to the specified Pygments style.
        """
        try:
            self._style = get_style_by_name(self._pygments_style)
        except ClassNotFound:
            # unknown style, also happen with plugins style when used from a
            # frozen app.
            if self._pygments_style == 'qt':
                from pyqode.core.styles import QtStyle
                self._style = QtStyle
            elif self._pygments_style == 'darcula':
                from pyqode.core.styles import DarculaStyle
                self._style = DarculaStyle
            else:
                self._style = get_style_by_name('default')
                self._pygments_style = 'default'
        self._clear_caches() 
Example #9
Source File: syntax_highlighter.py    From Turing with MIT License 6 votes vote down vote up
def __init__(self, style):
        """
        :param style: name of the pygments style to load
        """
        self._name = style
        self._brushes = {}
        #: Dictionary of formats colors (keys are the same as for
        #: :attr:`pyqode.core.api.COLOR_SCHEME_KEYS`
        self.formats = {}
        try:
            style = get_style_by_name(style)
        except ClassNotFound:
            if style == 'darcula':
                from pyqode.core.styles.darcula import DarculaStyle
                style = DarculaStyle
            else:
                from pyqode.core.styles.qt import QtStyle
                style = QtStyle
        self._load_formats_from_style(style) 
Example #10
Source File: codeinput.py    From Tickeys-linux with MIT License 6 votes vote down vote up
def __init__(self, **kwargs):
        stylename = kwargs.get('style_name', 'default')
        style = kwargs['style'] if 'style' in kwargs \
            else styles.get_style_by_name(stylename)
        self.formatter = BBCodeFormatter(style=style)
        self.lexer = lexers.PythonLexer()
        self.text_color = '#000000'
        self._label_cached = Label()
        self.use_text_color = True

        super(CodeInput, self).__init__(**kwargs)

        self._line_options = kw = self._get_line_options()
        self._label_cached = Label(**kw)
        # use text_color as foreground color
        text_color = kwargs.get('foreground_color')
        if text_color:
            self.text_color = get_hex_from_color(text_color)
        # set foreground to white to allow text colors to show
        # use text_color as the default color in bbcodes
        self.use_text_color = False
        self.foreground_color = [1, 1, 1, .999]
        if not kwargs.get('background_color'):
            self.background_color = [.9, .92, .92, 1] 
Example #11
Source File: codeinput.py    From Tickeys-linux with MIT License 6 votes vote down vote up
def __init__(self, **kwargs):
        stylename = kwargs.get('style_name', 'default')
        style = kwargs['style'] if 'style' in kwargs \
            else styles.get_style_by_name(stylename)
        self.formatter = BBCodeFormatter(style=style)
        self.lexer = lexers.PythonLexer()
        self.text_color = '#000000'
        self._label_cached = Label()
        self.use_text_color = True

        super(CodeInput, self).__init__(**kwargs)

        self._line_options = kw = self._get_line_options()
        self._label_cached = Label(**kw)
        # use text_color as foreground color
        text_color = kwargs.get('foreground_color')
        if text_color:
            self.text_color = get_hex_from_color(text_color)
        # set foreground to white to allow text colors to show
        # use text_color as the default color in bbcodes
        self.use_text_color = False
        self.foreground_color = [1, 1, 1, .999]
        if not kwargs.get('background_color'):
            self.background_color = [.9, .92, .92, 1] 
Example #12
Source File: codeinput.py    From kivystudio with MIT License 6 votes vote down vote up
def __init__(self, **kwargs):
        stylename = kwargs.get('style_name', 'default')
        style = kwargs['style'] if 'style' in kwargs \
            else styles.get_style_by_name(stylename)
        self.formatter = BBCodeFormatter(style=style)
        self.lexer = lexers.PythonLexer()
        self.text_color = '#000000'
        self._label_cached = Label()
        self.use_text_color = True

        super(CodeInput, self).__init__(**kwargs)

        self._line_options = kw = self._get_line_options()
        self._label_cached = Label(**kw)
        # use text_color as foreground color
        text_color = kwargs.get('foreground_color')
        if text_color:
            self.text_color = get_hex_from_color(text_color)
        # set foreground to white to allow text colors to show
        # use text_color as the default color in bbcodes
        self.use_text_color = False
        self.foreground_color = [1, 1, 1, .999]
        if not kwargs.get('background_color'):
            self.background_color = [.9, .92, .92, 1] 
Example #13
Source File: styles.py    From pySINDy with MIT License 6 votes vote down vote up
def get_colors(stylename):
    """Construct the keys to be used building the base stylesheet
    from a templatee."""
    style = get_style_by_name(stylename)
    fgcolor = style.style_for_token(Token.Text)['color'] or ''
    if len(fgcolor) in (3,6):
        # could be 'abcdef' or 'ace' hex, which needs '#' prefix
        try:
            int(fgcolor, 16)
        except TypeError:
            pass
        else:
            fgcolor = "#"+fgcolor

    return dict(
        bgcolor = style.background_color,
        select = style.highlight_color,
        fgcolor = fgcolor
    ) 
Example #14
Source File: styles.py    From qtconsole with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def dark_style(stylename):
    """Guess whether the background of the style with name 'stylename'
    counts as 'dark'."""
    return dark_color(get_style_by_name(stylename).background_color) 
Example #15
Source File: formatter.py    From syntax-highlighting with GNU Affero General Public License v3.0 5 votes vote down vote up
def _lookup_style(style):
    if isinstance(style, string_types):
        return get_style_by_name(style)
    return style 
Example #16
Source File: style.py    From ptpython with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_all_code_styles() -> Dict[str, BaseStyle]:
    """
    Return a mapping from style names to their classes.
    """
    result: Dict[str, BaseStyle] = {
        name: style_from_pygments_cls(get_style_by_name(name))
        for name in get_all_styles()
    }
    result["win32"] = Style.from_dict(win32_code_style)
    return result 
Example #17
Source File: pygments_highlighter.py    From qtconsole with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def set_style(self, style):
        """ Sets the style to the specified Pygments style.
        """
        if isinstance(style, string_types):
            style = get_style_by_name(style)
        self._style = style
        self._clear_caches() 
Example #18
Source File: syntax.py    From rich with MIT License 5 votes vote down vote up
def __init__(
        self,
        code: str,
        lexer_name: str,
        *,
        theme: Union[str, Type[PygmentsStyle]] = DEFAULT_THEME,
        dedent: bool = False,
        line_numbers: bool = False,
        start_line: int = 1,
        line_range: Tuple[int, int] = None,
        highlight_lines: Set[int] = None,
        code_width: Optional[int] = None,
        tab_size: int = 4,
        word_wrap: bool = False
    ) -> None:
        self.code = code
        self.lexer_name = lexer_name
        self.dedent = dedent
        self.line_numbers = line_numbers
        self.start_line = start_line
        self.line_range = line_range
        self.highlight_lines = highlight_lines or set()
        self.code_width = code_width
        self.tab_size = tab_size
        self.word_wrap = word_wrap

        self._style_cache: Dict[Any, Style] = {}

        if not isinstance(theme, str) and issubclass(theme, PygmentsStyle):
            self._pygments_style_class = theme
        else:
            try:
                self._pygments_style_class = get_style_by_name(theme)
            except ClassNotFound:
                self._pygments_style_class = get_style_by_name("default")
        self._background_color = self._pygments_style_class.background_color 
Example #19
Source File: x86.py    From deen with Apache License 2.0 5 votes vote down vote up
def _syntax_highlighting(self, data):
        try:
            from pygments import highlight
            from pygments.lexers import NasmLexer, GasLexer
            from pygments.formatters import TerminalFormatter, Terminal256Formatter
            from pygments.styles import get_style_by_name
            style = get_style_by_name('colorful')
            import curses
            curses.setupterm()
            if curses.tigetnum('colors') >= 256:
                FORMATTER = Terminal256Formatter(style=style)
            else:
                FORMATTER = TerminalFormatter()
            if self.ks.syntax == keystone.KS_OPT_SYNTAX_INTEL:
                lexer = NasmLexer()
            else:
                lexer = GasLexer()
            # When pygments is available, we
            # can print the disassembled
            # instructions with syntax
            # highlighting.
            data = highlight(data, lexer, FORMATTER)
        except ImportError:
            pass
        finally:
            data = data.encode()
        return data 
Example #20
Source File: style.py    From aws-shell with Apache License 2.0 5 votes vote down vote up
def style_factory(self, style_name):
        """Retrieve the specified pygments style.

        If the specified style is not found, the vim style is returned.

        :type style_name: str
        :param style_name: The pygments style name.

        :rtype: :class:`pygments.style.StyleMeta`
        :return: Pygments style info.
        """
        try:
            style = get_style_by_name(style_name)
        except ClassNotFound:
            style = get_style_by_name('vim')

        # Create a style dictionary.
        styles = {}
        styles.update(style.styles)
        styles.update(default_style_extensions)
        t = Token
        styles.update({
            t.Menu.Completions.Completion.Current: 'bg:#00aaaa #000000',
            t.Menu.Completions.Completion: 'bg:#008888 #ffffff',
            t.Menu.Completions.Meta.Current: 'bg:#00aaaa #000000',
            t.Menu.Completions.Meta: 'bg:#00aaaa #ffffff',
            t.Scrollbar.Button: 'bg:#003333',
            t.Scrollbar: 'bg:#00aaaa',
            t.Toolbar: 'bg:#222222 #cccccc',
            t.Toolbar.Off: 'bg:#222222 #696969',
            t.Toolbar.On: 'bg:#222222 #ffffff',
            t.Toolbar.Search: 'noinherit bold',
            t.Toolbar.Search.Text: 'nobold',
            t.Toolbar.System: 'noinherit bold',
            t.Toolbar.Arg: 'noinherit bold',
            t.Toolbar.Arg.Text: 'nobold'
        })

        return style_from_dict(styles) 
Example #21
Source File: style.py    From pyvim with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_editor_style_by_name(name):
    """
    Get Style class.
    This raises `pygments.util.ClassNotFound` when there is no style with this
    name.
    """
    if name == 'vim':
        vim_style = Style.from_dict(default_vim_style)
    else:
        vim_style = style_from_pygments_cls(get_style_by_name(name))

    return merge_styles([
        vim_style,
        Style.from_dict(style_extensions),
    ]) 
Example #22
Source File: pygments_highlighter.py    From Computable with MIT License 5 votes vote down vote up
def set_style(self, style):
        """ Sets the style to the specified Pygments style.
        """
        if isinstance(style, basestring):
            style = get_style_by_name(style)
        self._style = style
        self._clear_caches() 
Example #23
Source File: formatter.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _lookup_style(style):
    if isinstance(style, string_types):
        return get_style_by_name(style)
    return style 
Example #24
Source File: styles.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def dark_style(stylename):
    """Guess whether the background of the style with name 'stylename'
    counts as 'dark'."""
    return dark_color(get_style_by_name(stylename).background_color) 
Example #25
Source File: styles.py    From Computable with MIT License 5 votes vote down vote up
def dark_style(stylename):
    """Guess whether the background of the style with name 'stylename'
    counts as 'dark'."""
    return dark_color(get_style_by_name(stylename).background_color) 
Example #26
Source File: pygments_highlighter.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def set_style(self, style):
        """ Sets the style to the specified Pygments style.
        """
        if isinstance(style, string_types):
            style = get_style_by_name(style)
        self._style = style
        self._clear_caches() 
Example #27
Source File: formatter.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def _lookup_style(style):
    if isinstance(style, str):
        return get_style_by_name(style)
    return style 
Example #28
Source File: formatter.py    From diaphora with GNU Affero General Public License v3.0 5 votes vote down vote up
def _lookup_style(style):
    if isinstance(style, string_types):
        return get_style_by_name(style)
    return style 
Example #29
Source File: formatter.py    From android_universal with MIT License 5 votes vote down vote up
def _lookup_style(style):
    if isinstance(style, string_types):
        return get_style_by_name(style)
    return style 
Example #30
Source File: from_pygments.py    From android_universal with MIT License 5 votes vote down vote up
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)