Add a way to specify renderer options on command-line, added tmux_escape

Note: tmux escaping does not fix things, on my system it produces
![Gap between ... and directory name](http://img-fotki.yandex.ru/get/6439/9151298.2/0_93b4c_5d9a317f_orig.png)
Fixes #199
This commit is contained in:
ZyX 2013-02-11 08:37:58 +04:00 committed by Kim Silkebækken
parent 52635d05ae
commit d12299c18c
4 changed files with 25 additions and 10 deletions

View File

@ -11,7 +11,7 @@ from powerline.lib import underscore_to_camelcase
class Powerline(object):
def __init__(self, ext, renderer_module=None, segment_info=None):
def __init__(self, ext, renderer_module=None, segment_info=None, renderer_options={}):
config_home = os.environ.get('XDG_CONFIG_HOME', os.path.expanduser('~/.config'))
config_path = os.path.join(config_home, 'powerline')
plugin_path = os.path.join(os.path.realpath(os.path.dirname(__file__)), 'config_files')
@ -51,8 +51,9 @@ class Powerline(object):
except ImportError as e:
sys.stderr.write('Error while importing renderer module: {0}\n'.format(e))
sys.exit(1)
self.renderer = Renderer(theme_config, local_themes, theme_kwargs,
term_24bit_colors=self.config.get('term_24bit_colors', False))
options = {'term_truecolor': self.config.get('term_24bit_colors', False)}
options.update(renderer_options)
self.renderer = Renderer(theme_config, local_themes, theme_kwargs, **options)
def add_local_theme(self, key, config):
'''Add local themes at runtime (e.g. during vim session).

View File

@ -5,14 +5,11 @@ from powerline.theme import Theme
class Renderer(object):
term_truecolor = False
def __init__(self, theme_config, local_themes, theme_kwargs, term_24bit_colors=False):
def __init__(self, theme_config, local_themes, theme_kwargs, **options):
self.__dict__.update(options)
self.theme = Theme(theme_config=theme_config, **theme_kwargs)
self.local_themes = local_themes
self.theme_kwargs = theme_kwargs
self.term_truecolor = term_24bit_colors
def add_local_theme(self, matcher, theme):
if matcher in self.local_themes:

View File

@ -8,6 +8,8 @@ class ShellRenderer(Renderer):
'''Powerline shell segment renderer.'''
escape_hl_start = ''
escape_hl_end = ''
term_truecolor = False
tmux_escape = False
def hlstyle(self, fg=None, bg=None, attr=None):
'''Highlight a segment.
@ -45,7 +47,10 @@ class ShellRenderer(Renderer):
ansi += [3]
elif attr & ATTR_UNDERLINE:
ansi += [4]
return self.escape_hl_start + '[{0}m'.format(';'.join(str(attr) for attr in ansi)) + self.escape_hl_end
r = '\033[{0}m'.format(';'.join(str(attr) for attr in ansi))
if self.tmux_escape:
r = '\033Ptmux;' + r.replace('\033', '\033\033') + '\033\\'
return self.escape_hl_start + r + self.escape_hl_end
@staticmethod
def escape(string):

View File

@ -3,6 +3,7 @@
'''Powerline prompt and statusline script.'''
import argparse
import sys
import json
try:
from powerline.core import Powerline
@ -11,6 +12,16 @@ except ImportError:
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from powerline.core import Powerline # NOQA
def oval(s):
if '=' not in s:
raise TypeError('Option must look like option=json_value')
if s[0] == '_':
raise ValueError('Option names must not start with `_\'')
idx = s.index('=')
o = s[:idx]
val = json.loads(s[idx+1:])
return (o, val)
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('ext', nargs=1)
parser.add_argument('side', nargs='?', choices=('left', 'right'))
@ -18,10 +29,11 @@ parser.add_argument('-r', '--renderer_module', metavar='MODULE', type=str)
parser.add_argument('-w', '--width', type=int)
parser.add_argument('--last_exit_code', metavar='INT', type=int)
parser.add_argument('--last_pipe_status', metavar='LIST', default='', type=lambda s: [int(status) for status in s.split()])
parser.add_argument('-o', '--renderer_option', nargs='*', metavar='OPTION=VALUE', type=oval)
if __name__ == '__main__':
args = parser.parse_args()
powerline = Powerline(ext=args.ext[0], renderer_module=args.renderer_module, segment_info=args)
powerline = Powerline(ext=args.ext[0], renderer_module=args.renderer_module, segment_info=args, renderer_options=dict(args.renderer_option or {}))
rendered = powerline.renderer.render(width=args.width, side=args.side)
try:
sys.stdout.write(rendered)