Update terminal renderer output depending on shell

Bash and zsh have different ways of escaping colors in the prompt, this
update checks the $SHELL environment variable and uses the correct
escape sequence for the current shell.

A known issue with this method is that $SHELL doesn't get updated when
switching shells (i.e. if you're running /bin/bash when you're in a zsh
shell) so in rare cases rendering errors may occur. A workaround which
is much slower is to parse the output of `ps -p $$` which always returns
the current shell.
This commit is contained in:
Kim Silkebækken 2013-01-16 09:39:20 +01:00
parent b91009404f
commit ecf9e7eea7
1 changed files with 14 additions and 1 deletions

View File

@ -1,10 +1,23 @@
# -*- coding: utf-8 -*-
import os
from powerline.renderer import Renderer
class TerminalRenderer(Renderer):
'''Powerline terminal segment renderer.'''
_color_templates = {
'default': '[{code}m',
'bash': '\[[{code}m\]',
'zsh': '%{{[{code}m%}}',
}
def __init__(self, *args, **kwargs):
super(TerminalRenderer, self).__init__(*args, **kwargs)
shell = os.path.basename(os.environ.get('SHELL', 'default'))
self.color_template = self._color_templates[shell]
def hl(self, fg=None, bg=None, attr=None):
'''Highlight a segment.
@ -29,4 +42,4 @@ class TerminalRenderer(Renderer):
else:
if attr & Renderer.ATTR_BOLD:
ansi += [1]
return '[{0}m'.format(';'.join(str(attr) for attr in ansi))
return self.color_template.format(code=';'.join(str(attr) for attr in ansi))