Pass segment contents to renderer hl() methods

This allows the hl() methods to wrap highlighting code *around* the
segment contents, this is required for e.g. Pango markup in Awesome
statuslines (segments must be wrapped in <span ...>...</span> tags).
This commit is contained in:
Kim Silkebækken 2013-01-28 11:51:26 +01:00
parent 7a1d7ecaf4
commit 1e4c3612ee
5 changed files with 47 additions and 27 deletions

View File

@ -105,39 +105,59 @@ class Renderer(object):
segment['rendered_raw'] = u'' segment['rendered_raw'] = u''
outer_padding = ' ' if (index == 0 and segment['side'] == 'left') or (index == segments_len - 1 and segment['side'] == 'right') else '' outer_padding = ' ' if (index == 0 and segment['side'] == 'left') or (index == segments_len - 1 and segment['side'] == 'right') else ''
divider_type = 'soft' if compare_segment['highlight'][mode]['bg'] == segment['highlight'][mode]['bg'] else 'hard' divider_type = 'soft' if compare_segment['highlight'][mode]['bg'] == segment['highlight'][mode]['bg'] else 'hard'
divider = theme.get_divider(segment['side'], divider_type)
divider_hl = ''
segment_hl = ''
divider_raw = theme.get_divider(segment['side'], divider_type)
divider_highlighted = ''
contents_raw = segment['contents']
contents_highlighted = ''
# Pad segments first
if segment['type'] == 'filler':
pass
elif segment['draw_divider'] or divider_type == 'hard':
if segment['side'] == 'left':
contents_raw = outer_padding + contents_raw + ' '
divider_raw = divider_raw + ' '
else:
contents_raw = ' ' + contents_raw + outer_padding
divider_raw = ' ' + divider_raw
elif contents_raw:
if segment['side'] == 'left':
contents_raw = outer_padding + contents_raw
else:
contents_raw = contents_raw + outer_padding
else:
raise ValueError('Unknown segment type')
# Apply highlighting to padded dividers and contents
if render_highlighted: if render_highlighted:
if divider_type == 'soft' and segment['divider_highlight_group'] is not None: if divider_type == 'soft' and segment['divider_highlight_group'] is not None:
divider_hl = self.hl(segment['divider_highlight'][mode]['fg'], segment['divider_highlight'][mode]['bg'], False) divider_highlighted = self.hl(divider_raw, segment['divider_highlight'][mode]['fg'], segment['divider_highlight'][mode]['bg'], False)
elif divider_type == 'hard': elif divider_type == 'hard':
divider_hl = self.hl(segment['highlight'][mode]['bg'], compare_segment['highlight'][mode]['bg'], False) divider_highlighted = self.hl(divider_raw, segment['highlight'][mode]['bg'], compare_segment['highlight'][mode]['bg'], False)
segment_hl = self.hl(**segment['highlight'][mode]) contents_highlighted = self.hl(self.escape(contents_raw), **segment['highlight'][mode])
# Append padded raw and highlighted segments to the rendered segment variables
if segment['type'] == 'filler': if segment['type'] == 'filler':
rendered_highlighted += self.escape(segment['contents'] or '') rendered_highlighted += contents_highlighted if contents_raw else ''
elif segment['draw_divider'] or divider_type == 'hard': elif segment['draw_divider'] or divider_type == 'hard':
# Draw divider if specified, or if it's a hard divider # Draw divider if specified, or if it's a hard divider
# Note: Hard dividers are always drawn, regardless of # Note: Hard dividers are always drawn, regardless of
# the draw_divider option # the draw_divider option
if segment['side'] == 'left': if segment['side'] == 'left':
segment['rendered_raw'] += outer_padding + segment['contents'] + ' ' + divider + ' ' segment['rendered_raw'] += contents_raw + divider_raw
rendered_highlighted += segment_hl + self.escape(outer_padding + segment['contents'] + ' ') + divider_hl + self.escape(divider + ' ') rendered_highlighted += contents_highlighted + divider_highlighted
else: else:
segment['rendered_raw'] += ' ' + divider + ' ' + segment['contents'] + outer_padding segment['rendered_raw'] += divider_raw + contents_raw
rendered_highlighted += self.escape(' ') + divider_hl + self.escape(divider) + segment_hl + self.escape(' ' + segment['contents'] + outer_padding) rendered_highlighted += divider_highlighted + contents_highlighted
elif segment['contents']: elif contents_raw:
# Segments without divider # Segments without divider
if segment['side'] == 'left': if segment['side'] == 'left':
segment['rendered_raw'] += outer_padding + segment['contents'] segment['rendered_raw'] += contents_raw
rendered_highlighted += segment_hl + self.escape(outer_padding + segment['contents']) rendered_highlighted += contents_highlighted
else: else:
segment['rendered_raw'] += segment['contents'] + outer_padding segment['rendered_raw'] += contents_raw
rendered_highlighted += segment_hl + self.escape(segment['contents'] + outer_padding) rendered_highlighted += contents_highlighted
else:
raise ValueError('Unknown segment type')
rendered_highlighted += self.hl() rendered_highlighted += self.hl()
return rendered_highlighted return rendered_highlighted
@ -161,5 +181,5 @@ class Renderer(object):
b = int & 0xff b = int & 0xff
return r, g, b return r, g, b
def hl(self, fg=None, bg=None, attr=None): def hl(self, contents=None, fg=None, bg=None, attr=None):
raise NotImplementedError raise NotImplementedError

View File

@ -5,7 +5,7 @@ from powerline.renderer import Renderer
class ShellRenderer(Renderer): class ShellRenderer(Renderer):
'''Powerline shell segment renderer.''' '''Powerline shell segment renderer.'''
def hl(self, fg=None, bg=None, attr=None): def hl(self, contents=None, fg=None, bg=None, attr=None):
'''Highlight a segment. '''Highlight a segment.
If an argument is None, the argument is ignored. If an argument is If an argument is None, the argument is ignored. If an argument is
@ -35,7 +35,7 @@ class ShellRenderer(Renderer):
else: else:
if attr & Renderer.ATTR_BOLD: if attr & Renderer.ATTR_BOLD:
ansi += [1] ansi += [1]
return '[{0}m'.format(';'.join(str(attr) for attr in ansi)) return '[{0}m'.format(';'.join(str(attr) for attr in ansi)) + (contents or u'')
@staticmethod @staticmethod
def escape(string): def escape(string):

View File

@ -5,7 +5,7 @@ from powerline.renderer import Renderer
class TmuxRenderer(Renderer): class TmuxRenderer(Renderer):
'''Powerline tmux segment renderer.''' '''Powerline tmux segment renderer.'''
def hl(self, fg=None, bg=None, attr=None): def hl(self, contents=None, fg=None, bg=None, attr=None):
'''Highlight a segment.''' '''Highlight a segment.'''
# We don't need to explicitly reset attributes, so skip those calls # We don't need to explicitly reset attributes, so skip those calls
if not attr and not bg and not fg: if not attr and not bg and not fg:
@ -37,4 +37,4 @@ class TmuxRenderer(Renderer):
tmux_attr += ['underscore'] tmux_attr += ['underscore']
else: else:
tmux_attr += ['nounderscore'] tmux_attr += ['nounderscore']
return '#[' + ','.join(tmux_attr) + ']' return '#[' + ','.join(tmux_attr) + ']' + (contents or u'')

View File

@ -45,7 +45,7 @@ class VimRenderer(Renderer):
def escape(string): def escape(string):
return string.replace('%', '%%') return string.replace('%', '%%')
def hl(self, fg=None, bg=None, attr=None): def hl(self, contents=None, fg=None, bg=None, attr=None):
'''Highlight a segment. '''Highlight a segment.
If an argument is None, the argument is ignored. If an argument is If an argument is None, the argument is ignored. If an argument is
@ -94,4 +94,4 @@ class VimRenderer(Renderer):
guibg='#{0:06x}'.format(hl_group['guibg']) if hl_group['guibg'] != 'NONE' else 'NONE', guibg='#{0:06x}'.format(hl_group['guibg']) if hl_group['guibg'] != 'NONE' else 'NONE',
attr=','.join(hl_group['attr']), attr=','.join(hl_group['attr']),
)) ))
return '%#' + self.hl_groups[(fg, bg, attr)]['name'] + '#' return '%#' + self.hl_groups[(fg, bg, attr)]['name'] + '#' + (contents or u'')

View File

@ -5,13 +5,13 @@ from powerline.renderers.shell import ShellRenderer
class ZshPromptRenderer(ShellRenderer): class ZshPromptRenderer(ShellRenderer):
'''Powerline zsh prompt segment renderer.''' '''Powerline zsh prompt segment renderer.'''
def hl(self, *args, **kwargs): def hl(self, contents=None, fg=None, bg=None, attr=None):
'''Highlight a segment. '''Highlight a segment.
Returns the default ShellRenderer escape sequence with %{...%} wrapped Returns the default ShellRenderer escape sequence with %{...%} wrapped
around it (required in zsh prompts). around it (required in zsh prompts).
''' '''
return '%{' + super(ZshPromptRenderer, self).hl(*args, **kwargs) + '%}' return '%{' + super(ZshPromptRenderer, self).hl(None, fg, bg, attr) + '%}' + (contents or u'')
@staticmethod @staticmethod
def escape(string): def escape(string):