Skip to content

Commit 30ec079

Browse files
committed
add options for various styles
1 parent 9a7fb6b commit 30ec079

File tree

1 file changed

+50
-8
lines changed
  • refinery/units/sinks

1 file changed

+50
-8
lines changed

refinery/units/sinks/hl.py

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,30 @@ def __init__(
2424
help='Optionally specify the input language to be highlighted.')] = None,
2525
style: Param[str | None, Arg.String('-s', group='STYLE',
2626
help='Optionally specify a color style supported by Pygments.')] = None,
27-
dark: Param[bool, Arg.Switch('-d', group='STYLE',
28-
help='Use the Pygments color scheme for a dark brackground.')] = False,
29-
light: Param[bool, Arg.Switch('-l', group='STYLE',
30-
help='Use the Pygments color scheme for a light background.')] = False,
27+
github: Param[bool, Arg.Switch('-G', group='STYLE',
28+
help='Use github-flavored styling.')] = False,
29+
solarized: Param[bool, Arg.Switch('-S', group='STYLE',
30+
help='Use solarized-flavored styling.')] = False,
31+
gruvbox: Param[bool, Arg.Switch('-B', group='STYLE',
32+
help='Use gruvbox-flavored styling.')] = False,
33+
dark: Param[bool, Arg.Switch('-d',
34+
help='Assume a dark brackground.')] = False,
35+
light: Param[bool, Arg.Switch('-l',
36+
help='Assume a light background.')] = False,
3137
):
3238
if dark and light:
3339
raise ValueError('The "dark" and "light" options cannot simultaneously be set.')
34-
return super().__init__(lexer=lexer, style=style, dark=dark, light=light)
40+
if sum(1 for opt in (github, solarized, gruvbox, style) if opt) > 1:
41+
raise ValueError('More than one styling option was set.')
42+
return super().__init__(
43+
lexer=lexer,
44+
style=style,
45+
dark=dark,
46+
light=light,
47+
github=github,
48+
solarized=solarized,
49+
gruvbox=gruvbox,
50+
)
3551

3652
@Unit.Requires('Pygments', ['display', 'extended'])
3753
def _pygments():
@@ -42,6 +58,9 @@ def _pygments():
4258
import pygments.token
4359
return pygments
4460

61+
def _style_variant(self):
62+
return 'light' if self.args.light else 'dark'
63+
4564
def process(self, data):
4665
lib = self._pygments
4766
token = lib.token
@@ -67,14 +86,21 @@ def process(self, data):
6786
lexer = lib.lexers.guess_lexer(data)
6887

6988
self.log_info(F'using lexer for {lexer.name.lower()}')
89+
t256 = lib.formatters.Terminal256Formatter
7090

71-
if self.args.light:
91+
if self.args.github:
92+
tf = t256(style=F'github-{self._style_variant()}')
93+
elif self.args.solarized:
94+
tf = t256(style=F'solarized-{self._style_variant()}')
95+
elif self.args.gruvbox:
96+
tf = t256(style=F'gruvbox-{self._style_variant()}')
97+
elif self.args.light:
7298
tf = lib.formatters.TerminalFormatter(bg='light')
7399
elif self.args.dark:
74100
tf = lib.formatters.TerminalFormatter(bg='dark')
75101
else:
76102
if not (style := self.args.style):
77-
class style(lib.style.Style):
103+
class _style(lib.style.Style):
78104
background_color = 'default'
79105
R = 'ansibrightred'
80106
C = 'ansibrightcyan'
@@ -92,8 +118,24 @@ class style(lib.style.Style):
92118
token.Number : C,
93119
token.Literal : C,
94120
}
95-
tf = lib.formatters.Terminal256Formatter(style=style)
121+
style = _style
122+
tf = t256(style=style)
96123

97124
out = lib.highlight(data, lexer, tf)
98125
out = F'{out}{colorama.Style.RESET_ALL}'
99126
return out.encode(self.codec)
127+
128+
129+
class hlg(hl, docs='{0}{s}This variant uses GitHub styling.'):
130+
def __init__(self, lexer: str | None = None, dark: bool = False, light: bool = False):
131+
super().__init__(lexer, dark=dark, light=light, github=True)
132+
133+
134+
class hls(hl, docs='{0}{s}This variant uses solarized styling.'):
135+
def __init__(self, lexer: str | None = None, dark: bool = False, light: bool = False):
136+
super().__init__(lexer, dark=dark, light=light, solarized=True)
137+
138+
139+
class hlb(hl, docs='{0}{s}This variant uses gruvbox styling.'):
140+
def __init__(self, lexer: str | None = None, dark: bool = False, light: bool = False):
141+
super().__init__(lexer, dark=dark, light=light, gruvbox=True)

0 commit comments

Comments
 (0)