Allow overriding segment contents when rendering

This commit is contained in:
Kim Silkebækken 2012-12-13 13:10:10 +01:00
parent c2dfabdb8d
commit 00f749c959
2 changed files with 15 additions and 11 deletions

View File

@ -12,7 +12,7 @@ class Renderer(object):
self.segments = [] self.segments = []
self.theme = theme self.theme = theme
def render(self, mode, width=None): def render(self, mode, width=None, contents_override=None):
'''Render all segments. '''Render all segments.
When a width is provided, low-priority segments are dropped one at When a width is provided, low-priority segments are dropped one at
@ -21,7 +21,7 @@ class Renderer(object):
provided they will fill the remaining space until the desired width is provided they will fill the remaining space until the desired width is
reached. reached.
''' '''
self.segments = self.theme.get_segments(mode) self.segments = self.theme.get_segments(mode, contents_override)
rendered_highlighted = self._render_segments(mode) rendered_highlighted = self._render_segments(mode)
if not width: if not width:

View File

@ -31,6 +31,7 @@ class Theme(object):
highlighting_group = segment.get('highlight', segment.get('name')) highlighting_group = segment.get('highlight', segment.get('name'))
self.segments.append({ self.segments.append({
'key': None if segment_type != 'function' else '{0}.{1}'.format(segment_module, function_name),
'type': segment_type, 'type': segment_type,
'highlight': self.colorscheme.get_group_highlighting(highlighting_group), 'highlight': self.colorscheme.get_group_highlighting(highlighting_group),
'before': segment.get('before', ''), 'before': segment.get('before', ''),
@ -52,36 +53,39 @@ class Theme(object):
''' '''
return self.dividers[side][type] return self.dividers[side][type]
def get_segments(self, mode): def get_segments(self, mode, contents_override=None):
'''Return all segments. '''Return all segments.
Function segments are called, and all segments get their before/after Function segments are called, and all segments get their before/after
and ljust/rjust properties applied. and ljust/rjust properties applied.
''' '''
contents_override = contents_override or {}
return_segments = [] return_segments = []
for segment in self.segments: for segment in self.segments:
if mode in segment['exclude_modes'] or (segment['include_modes'] and segment not in segment['include_modes']): if mode in segment['exclude_modes'] or (segment['include_modes'] and segment not in segment['include_modes']):
continue continue
if segment['type'] == 'function': if segment['type'] == 'function':
contents_func_ret = segment['contents_func'](**segment['args']) contents = contents_override.get(segment['key'], segment['contents_func'](**segment['args']))
if contents_func_ret is None: if contents is None:
continue continue
try: try:
segment['highlight'] = self.colorscheme.get_group_highlighting(contents_func_ret['highlight']) segment['highlight'] = self.colorscheme.get_group_highlighting(contents['highlight'])
segment['contents'] = contents_func_ret['contents'] segment['contents'] = contents['contents']
except TypeError: except TypeError:
segment['contents'] = contents_func_ret segment['contents'] = contents
elif segment['type'] == 'filler' or (segment['type'] == 'string' and segment['contents'] is not None): elif segment['type'] == 'filler' or (segment['type'] == 'string' and segment['contents'] is not None):
pass pass
else: else:
continue continue
segment['contents'] = unicode(segment['before'] + unicode(segment['contents']) + segment['after'])\ if not segment['key'] in contents_override:
.ljust(segment['ljust'])\ # Only apply before/after/just to non-overridden segments
.rjust(segment['rjust']) segment['contents'] = unicode(segment['before'] + unicode(segment['contents']) + segment['after'])\
.ljust(segment['ljust'])\
.rjust(segment['rjust'])
return_segments.append(segment) return_segments.append(segment)