mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 16:34:47 +01:00
refactor, per suggestions
This commit is contained in:
parent
27e8409760
commit
0a3f29f016
3 changed files with 112 additions and 96 deletions
|
@ -5,6 +5,7 @@ from warnings import warn
|
|||
|
||||
RE_BACKGROUND = re.compile('(bg|bg#|bghex|background)')
|
||||
|
||||
|
||||
def partial_color_format(template, style='default', cmap=None, hide=False):
|
||||
"""Formats a template string but only with respect to the colors.
|
||||
Another template string is returned, with the color values filled in.
|
||||
|
@ -27,6 +28,13 @@ def partial_color_format(template, style='default', cmap=None, hide=False):
|
|||
-------
|
||||
A template string with the color values filled in.
|
||||
"""
|
||||
try:
|
||||
return _partial_color_format_main(template, style=style, cmap=cmap, hide=hide)
|
||||
except:
|
||||
return template
|
||||
|
||||
|
||||
def _partial_color_format_main(template, style='default', cmap=None, hide=False):
|
||||
if cmap is not None:
|
||||
pass
|
||||
elif style in STYLES:
|
||||
|
@ -43,39 +51,36 @@ def partial_color_format(template, style='default', cmap=None, hide=False):
|
|||
colon = ':'
|
||||
expl = '!'
|
||||
toks = []
|
||||
try:
|
||||
for literal, field, spec, conv in formatter.parse(template):
|
||||
toks.append(literal)
|
||||
if field is None:
|
||||
pass
|
||||
elif field in cmap:
|
||||
toks.extend([esc, cmap[field], m])
|
||||
elif '#' in field:
|
||||
field = field.lower()
|
||||
pre, _, post = field.partition('#')
|
||||
f_or_b = '38' if RE_BACKGROUND.search(pre) is None else '48'
|
||||
rgb, _, post = post.partition('_')
|
||||
c256, _ = rgb_to_256(rgb)
|
||||
color = f_or_b + ';5;' + c256
|
||||
mods = pre + '_' + post
|
||||
if 'underline' in mods:
|
||||
color = '4;' + color
|
||||
if 'bold' in mods:
|
||||
color = '1;' + color
|
||||
toks.extend([esc, color, m])
|
||||
elif field is not None:
|
||||
toks.append(bopen)
|
||||
toks.append(field)
|
||||
if conv is not None and len(conv) > 0:
|
||||
toks.append(expl)
|
||||
toks.append(conv)
|
||||
if spec is not None and len(spec) > 0:
|
||||
toks.append(colon)
|
||||
toks.append(spec)
|
||||
toks.append(bclose)
|
||||
return ''.join(toks)
|
||||
except:
|
||||
return template
|
||||
for literal, field, spec, conv in formatter.parse(template):
|
||||
toks.append(literal)
|
||||
if field is None:
|
||||
pass
|
||||
elif field in cmap:
|
||||
toks.extend([esc, cmap[field], m])
|
||||
elif '#' in field:
|
||||
field = field.lower()
|
||||
pre, _, post = field.partition('#')
|
||||
f_or_b = '38' if RE_BACKGROUND.search(pre) is None else '48'
|
||||
rgb, _, post = post.partition('_')
|
||||
c256, _ = rgb_to_256(rgb)
|
||||
color = f_or_b + ';5;' + c256
|
||||
mods = pre + '_' + post
|
||||
if 'underline' in mods:
|
||||
color = '4;' + color
|
||||
if 'bold' in mods:
|
||||
color = '1;' + color
|
||||
toks.extend([esc, color, m])
|
||||
elif field is not None:
|
||||
toks.append(bopen)
|
||||
toks.append(field)
|
||||
if conv is not None and len(conv) > 0:
|
||||
toks.append(expl)
|
||||
toks.append(conv)
|
||||
if spec is not None and len(spec) > 0:
|
||||
toks.append(colon)
|
||||
toks.append(spec)
|
||||
toks.append(bclose)
|
||||
return ''.join(toks)
|
||||
|
||||
|
||||
RGB_256 = {
|
||||
|
|
|
@ -1148,6 +1148,14 @@ def format_prompt(template=DEFAULT_PROMPT, formatter_dict=None):
|
|||
|
||||
def partial_format_prompt(template=DEFAULT_PROMPT, formatter_dict=None):
|
||||
"""Formats a xonsh prompt template string."""
|
||||
try:
|
||||
return _partial_format_prompt_main(template=template,
|
||||
formatter_dict=formatter_dict)
|
||||
except:
|
||||
return template
|
||||
|
||||
|
||||
def _partial_format_prompt_main(template=DEFAULT_PROMPT, formatter_dict=None):
|
||||
template = template() if callable(template) else template
|
||||
fmtter = _get_fmtter(formatter_dict)
|
||||
bopen = '{'
|
||||
|
@ -1155,35 +1163,32 @@ def partial_format_prompt(template=DEFAULT_PROMPT, formatter_dict=None):
|
|||
colon = ':'
|
||||
expl = '!'
|
||||
toks = []
|
||||
try:
|
||||
for literal, field, spec, conv in _FORMATTER.parse(template):
|
||||
toks.append(literal)
|
||||
if field is None:
|
||||
continue
|
||||
elif field.startswith('$'):
|
||||
v = builtins.__xonsh_env__[name[1:]] # FIXME `name` is an unresolved ref
|
||||
v = _FORMATTER.convert_field(v, conv)
|
||||
v = _FORMATTER.format_field(v, spec)
|
||||
toks.append(v)
|
||||
continue
|
||||
elif field in fmtter:
|
||||
v = fmtter[field]
|
||||
val = v() if callable(v) else v
|
||||
val = '' if val is None else val
|
||||
toks.append(val)
|
||||
else:
|
||||
toks.append(bopen)
|
||||
toks.append(field)
|
||||
if conv is not None and len(conv) > 0:
|
||||
toks.append(expl)
|
||||
toks.append(conv)
|
||||
if spec is not None and len(spec) > 0:
|
||||
toks.append(colon)
|
||||
toks.append(spec)
|
||||
toks.append(bclose)
|
||||
return ''.join(toks)
|
||||
except:
|
||||
return template
|
||||
for literal, field, spec, conv in _FORMATTER.parse(template):
|
||||
toks.append(literal)
|
||||
if field is None:
|
||||
continue
|
||||
elif field.startswith('$'):
|
||||
v = builtins.__xonsh_env__[field[1:]]
|
||||
v = _FORMATTER.convert_field(v, conv)
|
||||
v = _FORMATTER.format_field(v, spec)
|
||||
toks.append(v)
|
||||
continue
|
||||
elif field in fmtter:
|
||||
v = fmtter[field]
|
||||
val = v() if callable(v) else v
|
||||
val = '' if val is None else val
|
||||
toks.append(val)
|
||||
else:
|
||||
toks.append(bopen)
|
||||
toks.append(field)
|
||||
if conv is not None and len(conv) > 0:
|
||||
toks.append(expl)
|
||||
toks.append(conv)
|
||||
if spec is not None and len(spec) > 0:
|
||||
toks.append(colon)
|
||||
toks.append(spec)
|
||||
toks.append(bclose)
|
||||
return ''.join(toks)
|
||||
|
||||
|
||||
RE_HIDDEN = re.compile('\001.*?\002')
|
||||
|
|
|
@ -186,15 +186,26 @@ def code_by_name(name, styles):
|
|||
|
||||
|
||||
def partial_color_tokenize(template):
|
||||
"""Toeknizes a template string containing colors. Will return a list
|
||||
"""Tokenizes a template string containing colors. Will return a list
|
||||
of tuples mapping the token to the string which has that color.
|
||||
These sub-strings maybe templates themselves.
|
||||
"""
|
||||
formatter = string.Formatter()
|
||||
if hasattr(builtins, '__xonsh_shell__'):
|
||||
styles = __xonsh_shell__.shell.styler.styles
|
||||
else:
|
||||
styles = None
|
||||
color = Color.NO_COLOR
|
||||
try:
|
||||
toks, color = _partial_color_tokenize_main(template)
|
||||
except:
|
||||
toks = [(Color.NO_COLOR, template)]
|
||||
if styles is not None:
|
||||
styles[color] # ensure color is available
|
||||
return toks
|
||||
|
||||
|
||||
def _partial_color_tokenize_main(template):
|
||||
formatter = string.Formatter()
|
||||
bopen = '{'
|
||||
bclose = '}'
|
||||
colon = ':'
|
||||
|
@ -203,38 +214,33 @@ def partial_color_tokenize(template):
|
|||
fg = bg = None
|
||||
value = ''
|
||||
toks = []
|
||||
try:
|
||||
for literal, field, spec, conv in formatter.parse(template):
|
||||
if field is None:
|
||||
value += literal
|
||||
elif field in KNOWN_COLORS or '#' in field:
|
||||
value += literal
|
||||
next_color, fg, bg = color_by_name(field, fg, bg)
|
||||
if next_color is not color:
|
||||
if len(value) > 0:
|
||||
toks.append((color, value))
|
||||
if styles is not None:
|
||||
styles[color] # ensure color is available
|
||||
color = next_color
|
||||
value = ''
|
||||
elif field is not None:
|
||||
parts = [literal, bopen, field]
|
||||
if conv is not None and len(conv) > 0:
|
||||
parts.append(expl)
|
||||
parts.append(conv)
|
||||
if spec is not None and len(spec) > 0:
|
||||
parts.append(colon)
|
||||
parts.append(spec)
|
||||
parts.append(bclose)
|
||||
value += ''.join(parts)
|
||||
else:
|
||||
value += literal
|
||||
toks.append((color, value))
|
||||
except:
|
||||
toks = [(Color.NO_COLOR, template)]
|
||||
if styles is not None:
|
||||
styles[color] # ensure color is available
|
||||
return toks
|
||||
for literal, field, spec, conv in formatter.parse(template):
|
||||
if field is None:
|
||||
value += literal
|
||||
elif field in KNOWN_COLORS or '#' in field:
|
||||
value += literal
|
||||
next_color, fg, bg = color_by_name(field, fg, bg)
|
||||
if next_color is not color:
|
||||
if len(value) > 0:
|
||||
toks.append((color, value))
|
||||
if styles is not None:
|
||||
styles[color] # ensure color is available
|
||||
color = next_color
|
||||
value = ''
|
||||
elif field is not None:
|
||||
parts = [literal, bopen, field]
|
||||
if conv is not None and len(conv) > 0:
|
||||
parts.append(expl)
|
||||
parts.append(conv)
|
||||
if spec is not None and len(spec) > 0:
|
||||
parts.append(colon)
|
||||
parts.append(spec)
|
||||
parts.append(bclose)
|
||||
value += ''.join(parts)
|
||||
else:
|
||||
value += literal
|
||||
toks.append((color, value))
|
||||
return toks, color
|
||||
|
||||
|
||||
class CompoundColorMap(MutableMapping):
|
||||
|
|
Loading…
Add table
Reference in a new issue