powerline/lib/renderers/terminal.py
Kim Silkebækken 59a760c3c6 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.
2012-11-08 13:16:22 +01:00

39 lines
862 B
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
from lib.core import Segment
from lib.renderers import SegmentRenderer
class TerminalSegmentRenderer(SegmentRenderer):
'''Powerline terminal segment renderer.
'''
def hl(self, fg=None, bg=None, attr=None):
'''Highlight a segment.
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.
'''
ansi = []
if fg is not None:
if fg is False:
ansi += [39]
else:
ansi += [38, 5, fg[0]]
if bg is not None:
if bg is False:
ansi += [49]
else:
ansi += [48, 5, bg[0]]
if attr is not None:
if attr is False:
ansi += [22]
else:
if attr & Segment.ATTR_BOLD:
ansi += [1]
return '[{0}m'.format(';'.join(str(attr) for attr in ansi))