Skip to content
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 changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Features
* Set up customization of prompt/continuation colors in `~/.myclirc`.
* Allow customization of the toolbar with prompt format strings.
* Add warnings-count prompt format strings: `\w` and `\W`.
* Handle/document more attributes in the `[colors]` section of `~/.myclirc`.


Bug Fixes
Expand Down
37 changes: 32 additions & 5 deletions mycli/clistyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,28 @@ def parse_pygments_style(
return token_type, style_dict[token_name]


def is_valid_pygments(name: str) -> bool:
try:

class TestStyle(PygmentsStyle):
default_style = ''
styles = {Token.Default: name}

return True
except AssertionError:
# can't emit error because some styles are valid pygments and not valid ptoolkit
return False


def is_valid_ptoolkit(name: str) -> bool:
try:
_s = Style([("default", name)])
return True
except ValueError:
# can't emit error because some styles are valid pygments and not valid ptoolkit
return False


def style_factory_toolkit(name: str, cli_style: dict[str, str]) -> _MergedStyle:
try:
style: PygmentsStyle = pygments.styles.get_style_by_name(name)
Expand All @@ -119,14 +141,16 @@ def style_factory_toolkit(name: str, cli_style: dict[str, str]) -> _MergedStyle:
token_type, style_value = parse_pygments_style(token, style, cli_style)
if token_type in TOKEN_TO_PROMPT_STYLE:
prompt_style = TOKEN_TO_PROMPT_STYLE[token_type]
prompt_styles.append((prompt_style, style_value))
if is_valid_ptoolkit(style_value):
prompt_styles.append((prompt_style, style_value))
else:
# we don't want to support tokens anymore
logger.error("Unhandled style / class name: %s", token)
else:
# treat as prompt style name (2.0). See default style names here:
# https://github.com/jonathanslenders/python-prompt-toolkit/blob/master/prompt_toolkit/styles/defaults.py
prompt_styles.append((token, cli_style[token]))
if is_valid_ptoolkit(cli_style[token]):
prompt_styles.append((token, cli_style[token]))

override_style: Style = Style([("bottom-toolbar", "noreverse")])
return merge_styles([style_from_pygments_cls(style), override_style, Style(prompt_styles)])
Expand All @@ -145,13 +169,16 @@ def style_factory_helpers(
for token in cli_style:
if token.startswith("Token."):
token_type, style_value = parse_pygments_style(token, style, cli_style)
style.update({token_type: style_value})
if is_valid_pygments(style_value):
style.update({token_type: style_value})
elif token in PROMPT_STYLE_TO_TOKEN:
token_type = PROMPT_STYLE_TO_TOKEN[token]
style.update({token_type: cli_style[token]})
if is_valid_pygments(cli_style[token]):
style.update({token_type: cli_style[token]})
elif token in OVERRIDE_STYLE_TO_TOKEN:
token_type = OVERRIDE_STYLE_TO_TOKEN[token]
style.update({token_type: cli_style[token]})
if is_valid_pygments(cli_style[token]):
style.update({token_type: cli_style[token]})
else:
# TODO: cli helpers will have to switch to ptk.Style
logger.error("Unhandled style / class name: %s", token)
Expand Down
5 changes: 4 additions & 1 deletion mycli/myclirc
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ control_d = exit
# possible values: auto, fzf, reverse_isearch
control_r = auto

# Custom colors for the completion menu, toolbar, etc.
# Custom colors for the completion menu, toolbar, etc, with actual support
# depending on the terminal, and the property being set.
# Colors: #ffffff, bg:#ffffff, border:#ffffff.
# Attributes: (no)blink, bold, dim, hidden, inherit, italic, reverse, strike, underline.
[colors]
completion-menu.completion.current = 'bg:#ffffff #000000'
completion-menu.completion = 'bg:#008888 #ffffff'
Expand Down
5 changes: 4 additions & 1 deletion test/myclirc
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ control_d = exit
# possible values: auto, fzf, reverse_isearch
control_r = auto

# Custom colors for the completion menu, toolbar, etc.
# Custom colors for the completion menu, toolbar, etc, with actual support
# depending on the terminal, and the property being set.
# Colors: #ffffff, bg:#ffffff, border:#ffffff.
# Attributes: (no)blink, bold, dim, hidden, inherit, italic, reverse, strike, underline.
[colors]
completion-menu.completion.current = "bg:#ffffff #000000"
completion-menu.completion = "bg:#008888 #ffffff"
Expand Down