Join the fg/bg/attr methods of the renderers

This change joins the fg/bg/attr methods into a single hl method in the
renderers. This provides the same functionality, and it simplifies the
terminal rendering by being able to join all the attributes into one
escape sequence.

It's also a necessary change for the vim renderer, as this renderer
needs to log all the highlighting and create separate highlighting
classes for every single color and attribute combination. The only way
to do this is to have a single highlighting method.
This commit is contained in:
Kim Silkebækken 2012-11-08 13:16:22 +01:00
parent ffbdcc0f75
commit 59a760c3c6
5 changed files with 34 additions and 53 deletions

View File

@ -79,7 +79,7 @@ class Segment:
''' '''
if self.parent and self._fg[0] is None: if self.parent and self._fg[0] is None:
return self.parent.fg return self.parent.fg
return self._fg if self._fg[0] is not None else [None, None] return self._fg if self._fg[0] is not None else False
@property @property
def bg(self): def bg(self):
@ -91,7 +91,7 @@ class Segment:
''' '''
if self.parent and self._bg[0] is None: if self.parent and self._bg[0] is None:
return self.parent.bg return self.parent.bg
return self._bg if self._bg[0] is not None else [None, None] return self._bg if self._bg[0] is not None else False
@property @property
def attr(self): def attr(self):
@ -194,12 +194,10 @@ class Segment:
segment.next = segments[idx + 1] if idx < len(segments) - 1 else Segment() segment.next = segments[idx + 1] if idx < len(segments) - 1 else Segment()
if segment.side == 'l': if segment.side == 'l':
output += renderer.fg(segment.fg[0]) output += renderer.hl(segment.fg, segment.bg, segment.attr)
output += renderer.bg(segment.bg[0])
output += segment.padding output += segment.padding
output += renderer.attr(segment.attr)
output += segment.content output += segment.content
output += renderer.attr(None) output += renderer.hl(attr=False)
if segment.content: if segment.content:
if segment.next.bg == segment.bg: if segment.next.bg == segment.bg:
if segment.next.content and segment.separate: if segment.next.content and segment.separate:
@ -212,8 +210,7 @@ class Segment:
# the opposite side, it screws up the coloring # the opposite side, it screws up the coloring
elif segment.next.side == segment.side: elif segment.next.side == segment.side:
output += segment.padding output += segment.padding
output += renderer.fg(segment.bg[0]) output += renderer.hl(segment.bg, segment.next.bg)
output += renderer.bg(segment.next.bg[0])
output += segment.separator['hard'] output += segment.separator['hard']
else: else:
pad_pre = False pad_pre = False
@ -227,16 +224,13 @@ class Segment:
output += segment.separator['soft'] output += segment.separator['soft']
else: else:
pad_pre = True pad_pre = True
output += renderer.fg(segment.bg[0]) output += renderer.hl(segment.bg, segment.prev.bg)
output += renderer.bg(segment.prev.bg[0])
output += segment.separator['hard'] output += segment.separator['hard']
output += renderer.fg(segment.fg[0]) output += renderer.hl(segment.fg, segment.bg, segment.attr)
output += renderer.bg(segment.bg[0])
if pad_pre: if pad_pre:
output += segment.padding output += segment.padding
output += renderer.attr(segment.attr)
output += segment.content output += segment.content
output += renderer.attr(None) output += renderer.hl(attr=False)
output += segment.padding output += segment.padding
return output return output

View File

@ -1,8 +1,5 @@
class SegmentRenderer: class SegmentRenderer:
def fg(col): def hl(self, fg=None, bg=None, attr=None):
raise NotImplementedError
def bg(col):
raise NotImplementedError raise NotImplementedError
from lib.renderers.terminal import TerminalSegmentRenderer from lib.renderers.terminal import TerminalSegmentRenderer

View File

@ -7,42 +7,32 @@ from lib.renderers import SegmentRenderer
class TerminalSegmentRenderer(SegmentRenderer): class TerminalSegmentRenderer(SegmentRenderer):
'''Powerline terminal segment renderer. '''Powerline terminal segment renderer.
''' '''
def fg(col): def hl(self, fg=None, bg=None, attr=None):
'''Return ANSI escape code for foreground colors. '''Highlight a segment.
If no color is provided, the color is reset to the terminal default. If an argument is None, the argument is ignored. If an argument is
False, the argument is reset to the terminal defaults. If an argument
is a valid color or attribute, it's added to the ANSI escape code.
''' '''
if col: ansi = []
return '[38;5;{0}m'.format(col)
if fg is not None:
if fg is False:
ansi += [39]
else: else:
return '' ansi += [38, 5, fg[0]]
def bg(col): if bg is not None:
'''Return ANSI escape code for background colors. if bg is False:
ansi += [49]
If no color is provided, the color is reset to the terminal default.
'''
if col:
return '[48;5;{0}m'.format(col)
else: else:
return '' ansi += [48, 5, bg[0]]
def attr(attrs): if attr is not None:
'''Return ANSI escape code for attributes. if attr is False:
ansi += [22]
else:
if attr & Segment.ATTR_BOLD:
ansi += [1]
Accepts a flag with attributes defined in Segment. return '[{0}m'.format(';'.join(str(attr) for attr in ansi))
If no attributes are provided, the attributes are reset to the terminal
defaults.
'''
if not attrs:
return ''
ansi_attrs = []
if attrs & Segment.ATTR_BOLD:
ansi_attrs.append('1')
if ansi_attrs:
return '[{0}m'.format(';'.join(ansi_attrs))
return ''

View File

@ -15,4 +15,4 @@ powerline = Segment([
], 248, 239), ], 248, 239),
]) ])
print(powerline.render(TerminalSegmentRenderer)) print(powerline.render(TerminalSegmentRenderer()))

View File

@ -26,4 +26,4 @@ powerline = Segment([
], 245, side='r'), ], 245, side='r'),
], bg=236) ], bg=236)
print(powerline.render(TerminalSegmentRenderer)) print(powerline.render(TerminalSegmentRenderer()))