From 59a760c3c617ce65c2734d1aa12f9f702075b62b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20Silkeb=C3=A6kken?= Date: Thu, 8 Nov 2012 13:16:22 +0100 Subject: [PATCH] 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. --- lib/core.py | 22 +++++--------- lib/renderers/__init__.py | 5 +--- lib/renderers/terminal.py | 56 ++++++++++++++--------------------- powerline-terminal-example.py | 2 +- powerline-vim-example.py | 2 +- 5 files changed, 34 insertions(+), 53 deletions(-) diff --git a/lib/core.py b/lib/core.py index c3020539..fe659b42 100644 --- a/lib/core.py +++ b/lib/core.py @@ -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 diff --git a/lib/renderers/__init__.py b/lib/renderers/__init__.py index ee7568ee..bda7cf34 100644 --- a/lib/renderers/__init__.py +++ b/lib/renderers/__init__.py @@ -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 diff --git a/lib/renderers/terminal.py b/lib/renderers/terminal.py index 10eeb3d9..3d9df3f7 100644 --- a/lib/renderers/terminal.py +++ b/lib/renderers/terminal.py @@ -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)) diff --git a/powerline-terminal-example.py b/powerline-terminal-example.py index 2230e516..0677d768 100755 --- a/powerline-terminal-example.py +++ b/powerline-terminal-example.py @@ -15,4 +15,4 @@ powerline = Segment([ ], 248, 239), ]) -print(powerline.render(TerminalSegmentRenderer)) +print(powerline.render(TerminalSegmentRenderer())) diff --git a/powerline-vim-example.py b/powerline-vim-example.py index 03e45f81..e7879230 100755 --- a/powerline-vim-example.py +++ b/powerline-vim-example.py @@ -26,4 +26,4 @@ powerline = Segment([ ], 245, side='r'), ], bg=236) -print(powerline.render(TerminalSegmentRenderer)) +print(powerline.render(TerminalSegmentRenderer()))