Make daemon work with aboveleft shell key

This commit is contained in:
ZyX 2014-08-02 20:35:23 +04:00
parent 8374a66ca7
commit 9290c2a23b
3 changed files with 54 additions and 39 deletions

View File

@ -1,5 +1,7 @@
# vim:fileencoding=utf-8:noet
import os
from powerline import Powerline
from powerline.lib import mergedicts, parsedotval
@ -73,3 +75,26 @@ def finish_args(args):
args.theme_option = {}
if args.renderer_arg:
args.renderer_arg = mergeargs((parsedotval(v) for v in args.renderer_arg))
def write_output(args, powerline, segment_info, write):
if args.renderer_arg:
segment_info.update(args.renderer_arg)
if args.side.startswith('above'):
for line in powerline.render_above_lines(
width=args.width,
segment_info=segment_info,
mode=os.environ.get('_POWERLINE_MODE'),
):
write(line)
write('\n')
args.side = args.side[len('above'):]
if args.side:
rendered = powerline.render(
width=args.width,
side=args.side,
segment_info=segment_info,
mode=os.environ.get('_POWERLINE_MODE'),
)
write(rendered)

View File

@ -12,8 +12,9 @@ from signal import signal, SIGTERM
from time import sleep
from functools import partial
from locale import getpreferredencoding
from io import StringIO
from powerline.shell import get_argparser, finish_args, ShellPowerline
from powerline.shell import get_argparser, finish_args, ShellPowerline, write_output
from powerline.lib.monotonic import monotonic
is_daemon = False
@ -77,35 +78,41 @@ def render(args):
'environ': environ,
'args': args,
}
key = (args.ext[0], args.renderer_module,
tuple(args.config) if args.config else None,
tuple(args.theme_option) if args.theme_option else None,)
if args.renderer_arg:
segment_info.update(args.renderer_arg)
key = (
args.ext[0],
args.renderer_module,
tuple(args.config) if args.config else None,
tuple(args.theme_option) if args.theme_option else None,
tuple(args.renderer_arg) if args.renderer_arg else None,
)
finish_args(args)
pl = None
powerline = None
try:
pl = powerlines[key]
powerline = powerlines[key]
except KeyError:
try:
pl = powerlines[key] = PowerlineDaemon(
powerline = powerlines[key] = PowerlineDaemon(
args,
logger=logger,
config_loader=config_loader
config_loader=config_loader,
run_once=False,
)
if logger is None:
logger = powerline.logger
if config_loader is None:
config_loader = powerline.config_loader
except SystemExit:
# Somebody thought raising system exit was a good idea,
return ''
except Exception as e:
if pl:
pl.pl.exception('Failed to render {0}: {1}', str(key), str(e))
if powerline:
powerline.pl.exception('Failed to render {0}: {1}', str(key), str(e))
else:
return 'Failed to render {0}: {1}'.format(str(key), str(e))
if logger is None:
logger = pl.logger
if config_loader is None:
config_loader = pl.config_loader
return pl.render(width=args.width, side=args.side, segment_info=segment_info)
s = StringIO()
write_output(args, powerline, segment_info, s.write)
s.seek(0)
return s.read()
def eintr_retry_call(func, *args, **kwargs):

View File

@ -5,10 +5,11 @@ import sys
import os
try:
from powerline.shell import ShellPowerline, get_argparser, finish_args
from powerline.shell import ShellPowerline, get_argparser, finish_args, write_output
except ImportError:
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(os.path.realpath(__file__)))))
from powerline.shell import ShellPowerline, get_argparser, finish_args # NOQA
from powerline.shell import ShellPowerline, get_argparser, finish_args, write_output # NOQA
def write(output):
try:
@ -16,28 +17,10 @@ def write(output):
except UnicodeEncodeError:
sys.stdout.write(output.encode('utf-8'))
if __name__ == '__main__':
args = get_argparser(description=__doc__).parse_args()
finish_args(args)
powerline = ShellPowerline(args, run_once=True)
segment_info = {'args': args, 'environ': os.environ}
if args.renderer_arg:
segment_info.update(args.renderer_arg)
if args.side.startswith('above'):
for line in powerline.render_above_lines(
width=args.width,
segment_info=segment_info,
mode=os.environ.get('_POWERLINE_MODE'),
):
write(line)
sys.stdout.write('\n')
args.side = args.side[len('above'):]
if args.side:
rendered = powerline.render(
width=args.width,
side=args.side,
segment_info=segment_info,
mode=os.environ.get('_POWERLINE_MODE'),
)
write(rendered)
write_output(args, powerline, segment_info, write)