generated from fastai/nbdev_template
-
Notifications
You must be signed in to change notification settings - Fork 262
/
style.py
69 lines (59 loc) · 2.72 KB
/
style.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/10_style.ipynb.
# %% auto 0
__all__ = ['style_codes', 'S', 'StyleCode', 'Style', 'demo']
# %% ../nbs/10_style.ipynb 3
# Source: https://misc.flogisoft.com/bash/tip_colors_and_formatting
_base = 'red green yellow blue magenta cyan'
_regular = f'black {_base} light_gray'
_intense = 'dark_gray ' + ' '.join('light_'+o for o in _base.split()) + ' white'
_fmt = dict(bold=1,dim=2,underline=4,reverse=7,hidden=8)
# %% ../nbs/10_style.ipynb 4
class StyleCode:
"An escape sequence for styling terminal text."
def __init__(self, name, code, typ): self.name,self.code,self.typ = name,code,typ
def __str__(self): return f'\033[{self.code}m'
# %% ../nbs/10_style.ipynb 7
def _mk_codes(s, start, typ, fmt=None, **kwargs):
d = {k:i for i,k in enumerate(s.split())} if isinstance(s, str) else s
res = {k if fmt is None else fmt.format(k):start+v for k,v in d.items()}
res.update(kwargs)
return {k:StyleCode(k,v,typ) for k,v in res.items()}
# %% ../nbs/10_style.ipynb 8
# Hardcode `reset_bold=22` since 21 is not always supported
# See: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
style_codes = {**_mk_codes(_regular, 30, 'fg', default=39),
**_mk_codes(_intense, 90, 'fg'),
**_mk_codes(_regular, 40, 'bg', '{}_bg', default_bg=49),
**_mk_codes(_intense, 100, 'bg', '{}_bg'),
**_mk_codes(_fmt, 0, 'fmt'),
**_mk_codes(_fmt, 21, 'reset', 'reset_{}', reset=0, reset_bold=22)}
# %% ../nbs/10_style.ipynb 9
def _reset_code(s):
if s.typ == 'fg': return style_codes['default']
if s.typ == 'bg': return style_codes['default_bg']
if s.typ == 'fmt': return style_codes['reset_'+s.name]
# %% ../nbs/10_style.ipynb 10
class Style:
"A minimal terminal text styler."
def __init__(self, codes=None): self.codes = [] if codes is None else codes
def __dir__(self): return style_codes.keys()
def __getattr__(self, k): return Style(self.codes+[style_codes[k]])
def __call__(self, obj):
set_ = ''.join(str(o) for o in self.codes)
reset = ''.join('' if o is None else str(o) for o in set(_reset_code(o) for o in self.codes))
return set_ + str(obj) + reset
def __repr__(self):
nm = type(self).__name__
res = f'<{nm}: '
res += ' '.join(o.name for o in self.codes) if self.codes else 'none'
return res+'>'
# %% ../nbs/10_style.ipynb 12
S = Style()
# %% ../nbs/10_style.ipynb 25
def _demo(name, code):
s = getattr(S,name)
print(s(f'{code.code:>3} {name:16}'))
# %% ../nbs/10_style.ipynb 26
def demo():
"Demonstrate all available styles and their codes."
for k,v in style_codes.items(): _demo(k,v)