Add support for zsh local themes

Still missing: parser state

Ref #771
This commit is contained in:
ZyX 2014-02-16 20:06:32 +04:00
parent 01ef26354f
commit aff028e4e9
9 changed files with 96 additions and 3 deletions

View File

@ -104,10 +104,12 @@ class Prompt(object):
self.args = powerline.args self.args = powerline.args
def __str__(self): def __str__(self):
segment_info = {'args': self.args, 'environ': environ},
r = self.powerline.render( r = self.powerline.render(
width=zsh.columns(), width=zsh.columns(),
side=self.side, side=self.side,
segment_info={'args': self.args, 'environ': environ} segment_info=segment_info,
matcher_info=segment_info,
) )
if type(r) is not str: if type(r) is not str:
if type(r) is bytes: if type(r) is bytes:

View File

@ -113,9 +113,13 @@ _powerline_setup_prompt() {
zpython 'del _powerline_setup' zpython 'del _powerline_setup'
else else
local add_args='--last_exit_code=$? --last_pipe_status="$pipestatus"' local add_args='--last_exit_code=$? --last_pipe_status="$pipestatus"'
add_args+=' --renderer_arg="client_id=$$"'
add_args+=' --jobnum=$_POWERLINE_JOBNUM' add_args+=' --jobnum=$_POWERLINE_JOBNUM'
PS1='$($POWERLINE_COMMAND shell left -r zsh_prompt '$add_args')' PS1='$($POWERLINE_COMMAND shell left -r zsh_prompt '$add_args')'
RPS1='$($POWERLINE_COMMAND shell right -r zsh_prompt '$add_args')' RPS1='$($POWERLINE_COMMAND shell right -r zsh_prompt '$add_args')'
PS2='$($POWERLINE_COMMAND shell left -r zsh_prompt -R local_theme=continuation '$add_args')'
RPS2='$($POWERLINE_COMMAND shell right -r zsh_prompt -R local_theme=continuation '$add_args')'
PS3='$($POWERLINE_COMMAND shell left -r zsh_prompt -R local_theme=select '$add_args')'
fi fi
} }

View File

@ -8,6 +8,7 @@
"branch": { "fg": "gray9", "bg": "gray2" }, "branch": { "fg": "gray9", "bg": "gray2" },
"branch_dirty": { "fg": "brightyellow", "bg": "gray2" }, "branch_dirty": { "fg": "brightyellow", "bg": "gray2" },
"branch_clean": { "fg": "gray9", "bg": "gray2" }, "branch_clean": { "fg": "gray9", "bg": "gray2" },
"prompt": { "fg": "gray9", "bg": "gray4" },
"cwd": { "fg": "gray9", "bg": "gray4" }, "cwd": { "fg": "gray9", "bg": "gray4" },
"cwd:current_folder": { "fg": "gray10", "bg": "gray4", "attr": ["bold"] }, "cwd:current_folder": { "fg": "gray10", "bg": "gray4", "attr": ["bold"] },
"cwd:divider": { "fg": "gray7", "bg": "gray4" }, "cwd:divider": { "fg": "gray7", "bg": "gray4" },

View File

@ -25,7 +25,11 @@
}, },
"shell": { "shell": {
"colorscheme": "default", "colorscheme": "default",
"theme": "default" "theme": "default",
"local_themes": {
"continuation": "continuation",
"select": "select"
}
}, },
"tmux": { "tmux": {
"colorscheme": "default", "colorscheme": "default",

View File

@ -0,0 +1,15 @@
{
"segments": {
"left": [
{
"type": "string",
"contents": "",
"width": "auto",
"align": "r",
"highlight_group": ["prompt"]
}
],
"right": [
]
}
}

View File

@ -0,0 +1,13 @@
{
"segments": {
"left": [
{
"type": "string",
"contents": "Select",
"width": "auto",
"align": "r",
"highlight_group": ["prompt"]
}
]
}
}

View File

@ -22,7 +22,11 @@ class IpythonRenderer(ShellRenderer):
try: try:
return match['theme'] return match['theme']
except KeyError: except KeyError:
match['theme'] = Theme(theme_config=match['config'], top_theme_config=self.theme_config, **self.theme_kwargs) match['theme'] = Theme(
theme_config=match['config'],
top_theme_config=self.theme_config,
**self.theme_kwargs
)
return match['theme'] return match['theme']
def shutdown(self): def shutdown(self):

View File

@ -3,6 +3,7 @@
from __future__ import absolute_import, unicode_literals from __future__ import absolute_import, unicode_literals
from powerline.renderers.shell import ShellRenderer from powerline.renderers.shell import ShellRenderer
from powerline.theme import Theme
class ZshPromptRenderer(ShellRenderer): class ZshPromptRenderer(ShellRenderer):
@ -13,5 +14,47 @@ class ZshPromptRenderer(ShellRenderer):
character_translations = ShellRenderer.character_translations.copy() character_translations = ShellRenderer.character_translations.copy()
character_translations[ord('%')] = '%%' character_translations[ord('%')] = '%%'
old_widths = {}
def render(self, segment_info, *args, **kwargs):
client_id = segment_info.get('client_id')
key = (client_id, kwargs.get('side'))
kwargs = kwargs.copy()
width = kwargs.pop('width', None)
local_theme = segment_info.get('local_theme')
if client_id and local_theme:
output_raw = False
try:
width = self.old_widths[key]
except KeyError:
pass
else:
output_raw = True
ret = super(ShellRenderer, self).render(
output_raw=output_raw,
width=width,
matcher_info=local_theme,
segment_info=segment_info,
*args, **kwargs
)
if output_raw:
self.old_widths[key] = len(ret[1])
ret = ret[0]
return ret
def get_theme(self, matcher_info):
if not matcher_info:
return self.theme
match = self.local_themes[matcher_info]
try:
return match['theme']
except KeyError:
match['theme'] = Theme(
theme_config=match['config'],
top_theme_config=self.theme_config,
**self.theme_kwargs
)
return match['theme']
renderer = ZshPromptRenderer renderer = ZshPromptRenderer

View File

@ -38,6 +38,13 @@ class ShellPowerline(Powerline):
else: else:
return super(ShellPowerline, self).get_config_paths() return super(ShellPowerline, self).get_config_paths()
def get_local_themes(self, local_themes):
if not local_themes:
return {}
return dict(((key, {'config': self.load_theme_config(val)})
for key, val in local_themes.items()))
def get_argparser(parser=None, *args, **kwargs): def get_argparser(parser=None, *args, **kwargs):
if not parser: if not parser: