From ecf9e7eea779e30ab55e4b43f0536bbe3bc05f7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20Silkeb=C3=A6kken?= Date: Wed, 16 Jan 2013 09:39:20 +0100 Subject: [PATCH] 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. --- powerline/ext/terminal/renderer.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/powerline/ext/terminal/renderer.py b/powerline/ext/terminal/renderer.py index f946d400..3c975de3 100644 --- a/powerline/ext/terminal/renderer.py +++ b/powerline/ext/terminal/renderer.py @@ -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))