Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fastcore.style: fast styling for friendly CLIs #460

Merged
merged 1 commit into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,4 @@ checklink/cookies.txt
# .gitconfig is now autogenerated
.gitconfig

_docs
7 changes: 6 additions & 1 deletion fastcore/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
'title': 'fastcore',
'tst_flags': '',
'user': 'fastai',
'version': '1.5.16'},
'version': '1.5.17'},
'syms': { 'fastcore.all': {},
'fastcore.basics': { 'fastcore.basics.AttrDict': 'https://fastcore.fast.ai/basics.html#attrdict',
'fastcore.basics.AttrDict.copy': 'https://fastcore.fast.ai/basics.html#attrdict.copy',
Expand Down Expand Up @@ -361,6 +361,11 @@
'fastcore.shutil.disk_usage': 'https://fastcore.fast.ai/shutil.html#disk_usage',
'fastcore.shutil.move': 'https://fastcore.fast.ai/shutil.html#move',
'fastcore.shutil.rmtree': 'https://fastcore.fast.ai/shutil.html#rmtree'},
'fastcore.style': { 'fastcore.style.S': 'https://fastcore.fast.ai/style.html#s',
'fastcore.style.Style': 'https://fastcore.fast.ai/style.html#style',
'fastcore.style.StyleCode': 'https://fastcore.fast.ai/style.html#stylecode',
'fastcore.style.demo': 'https://fastcore.fast.ai/style.html#demo',
'fastcore.style.style_codes': 'https://fastcore.fast.ai/style.html#style_codes'},
'fastcore.test': { 'fastcore.test.ExceptionExpected': 'https://fastcore.fast.ai/test.html#exceptionexpected',
'fastcore.test.TEST_IMAGE': 'https://fastcore.fast.ai/test.html#test_image',
'fastcore.test.TEST_IMAGE_BW': 'https://fastcore.fast.ai/test.html#test_image_bw',
Expand Down
69 changes: 69 additions & 0 deletions fastcore/style.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
Loading