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

View File

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

View File

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

View File

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

View File

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