Add foreign local themes support

Refs #3.
This commit is contained in:
ZyX 2013-01-16 18:37:00 +04:00 committed by Kim Silkebækken
parent ecf9e7eea7
commit 128c012553
2 changed files with 25 additions and 2 deletions

View File

@ -31,7 +31,7 @@ class Powerline(object):
theme_config = self._load_theme_config(ext, self.config_ext.get('theme', 'default'))
path = [os.path.expanduser(path) for path in self.config.get('paths', [])]
get_segment = Segments(ext, path, colorscheme).get
get_matcher = Matchers(ext, path).get
self.get_matcher = Matchers(ext, path).get
theme_kwargs = {
'ext': ext,
'colorscheme': colorscheme,
@ -40,7 +40,7 @@ class Powerline(object):
}
local_themes = {}
for key, local_theme_name in self.config_ext.get('local_themes', {}).iteritems():
key = get_matcher(key)
key = self.get_matcher(key)
local_themes[key] = {'config': self._load_theme_config(ext, local_theme_name)}
# Load and initialize extension renderer
@ -49,6 +49,24 @@ class Powerline(object):
Renderer = getattr(importlib.import_module(renderer_module_name), renderer_class_name)
self.renderer = Renderer(theme_config, local_themes, theme_kwargs)
def add_local_theme(self, key, config):
'''Add local themes at runtime (e.g. during vim session).
Accepts key as first argument (same as keys in config.json:
ext/*/local_themes) and configuration dictionary as the second (has
format identical to themes/*/*.json)
Returns True if theme was added successfully and False if theme with
the same matcher already exists
'''
key = self.get_matcher(key)
try:
self.renderer.add_local_theme(key, {'config': config})
except KeyError:
return False
else:
return True
def _load_theme_config(self, ext, name):
return self._load_json_config(os.path.join('themes', ext, name))

View File

@ -14,6 +14,11 @@ class Renderer(object):
self.local_themes = local_themes
self.theme_kwargs = theme_kwargs
def add_local_theme(self, matcher, theme):
if matcher in self.local_themes:
raise KeyError('There is already a local theme with given matcher')
self.local_themes[matcher] = theme
def get_theme(self):
for matcher in self.local_themes.iterkeys():
if matcher():