Properly escape color codes in bash

Closes #177.
Closes #179.
This commit is contained in:
ZyX 2013-02-06 08:34:35 +04:00 committed by Kim Silkebækken
parent 4fa17f6ff5
commit 2e5b9383a5
2 changed files with 30 additions and 4 deletions

View File

@ -12,8 +12,16 @@ _powerline_tmux_set_columns() {
_powerline_tmux_setenv COLUMNS "$COLUMNS"
}
trap "_powerline_tmux_set_columns" SIGWINCH
kill -SIGWINCH "$$"
_powerline_prompt() {
[[ -z "$POWERLINE_OLD_PROMPT_COMMAND" ]] ||
eval $POWERLINE_OLD_PROMPT_COMMAND
PS1="$(powerline shell left -r bash_prompt --last_exit_code=$?)"
_powerline_tmux_set_pwd
}
export PROMPT_COMMAND="_powerline_tmux_set_pwd"
export PS1='$(powerline shell left --last_exit_code=$?)'
trap "_powerline_tmux_set_columns" SIGWINCH
_powerline_tmux_set_columns
[[ "$PROMPT_COMMAND" == "_powerline_prompt" ]] ||
POWERLINE_OLD_PROMPT_COMMAND="$PROMPT_COMMAND"
export PROMPT_COMMAND="_powerline_prompt"

View File

@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
from powerline.renderers.shell import ShellRenderer
class BashPromptRenderer(ShellRenderer):
'''Powerline bash prompt segment renderer.'''
def hl(self, contents=None, fg=None, bg=None, attr=None):
'''Highlight a segment.
Returns the default ShellRenderer escape sequence with \[...\] wrapped
around it (required in bash prompts).
'''
return '\[' + super(BashPromptRenderer, self).hl(None, fg, bg, attr) + '\]' + (contents or u'')
@staticmethod
def escape(string):
return string.replace('\\', '\\\\').replace('$', '\\$').replace('`', '\\`')