summaryrefslogtreecommitdiffstats
path: root/pgcli/pgstyle.py
blob: 54ae1bbe9470a794272a250e2fbcda2e972e53b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from pygments.token import string_to_tokentype
from pygments.util import ClassNotFound
from prompt_toolkit.styles import PygmentsStyle
import pygments.styles

from pygments.style import Style

def style_factory(name, cli_style):
    try:
        style = pygments.styles.get_style_by_name(name)
    except ClassNotFound:
        style = pygments.styles.get_style_by_name('native')

    custom_styles = {}
    for token in cli_style:
        try:
            custom_styles[string_to_tokentype(
                token)] = style.styles[string_to_tokentype(cli_style[token])]
        except AttributeError as err:
            custom_styles[string_to_tokentype(token)] = cli_style[token]

    return PygmentsStyle.from_defaults(style_dict=custom_styles,
                                       pygments_style_cls=style)


def style_factory_output(name, cli_style):
    try:
        style = pygments.styles.get_style_by_name(name).styles
    except ClassNotFound:
        style = pygments.styles.get_style_by_name('native').styles

    for token in cli_style:
        try:
            style.update({string_to_tokentype(
                token): style[string_to_tokentype(cli_style[token])], })
        except AttributeError as err:
            style.update(
                {string_to_tokentype(token): cli_style[token], })

    class OutputStyle(pygments.style.Style):
        default_style = ""
        styles = style

    return OutputStyle