Make powerline-render respect user locale as well

Closes #1023
This commit is contained in:
ZyX 2014-08-27 21:44:29 +04:00
parent 484b9af6ed
commit 7e7803f680
3 changed files with 14 additions and 13 deletions

View File

@ -81,7 +81,7 @@ def finish_args(args):
args.renderer_arg = mergeargs((parsedotval(v) for v in args.renderer_arg)) args.renderer_arg = mergeargs((parsedotval(v) for v in args.renderer_arg))
def write_output(args, powerline, segment_info, write): def write_output(args, powerline, segment_info, write, encoding):
if args.renderer_arg: if args.renderer_arg:
segment_info.update(args.renderer_arg) segment_info.update(args.renderer_arg)
if args.side.startswith('above'): if args.side.startswith('above'):
@ -90,8 +90,8 @@ def write_output(args, powerline, segment_info, write):
segment_info=segment_info, segment_info=segment_info,
mode=segment_info['environ'].get('_POWERLINE_MODE'), mode=segment_info['environ'].get('_POWERLINE_MODE'),
): ):
write(line) write(line.encode(encoding, 'replace'))
write('\n') write(b'\n')
args.side = args.side[len('above'):] args.side = args.side[len('above'):]
if args.side: if args.side:
@ -101,4 +101,4 @@ def write_output(args, powerline, segment_info, write):
segment_info=segment_info, segment_info=segment_info,
mode=segment_info['environ'].get('_POWERLINE_MODE'), mode=segment_info['environ'].get('_POWERLINE_MODE'),
) )
write(rendered) write(rendered.encode(encoding, 'replace'))

View File

@ -12,7 +12,7 @@ from signal import signal, SIGTERM
from time import sleep from time import sleep
from functools import partial from functools import partial
from locale import getpreferredencoding from locale import getpreferredencoding
from io import StringIO from io import BytesIO
from powerline.shell import get_argparser, finish_args, ShellPowerline, write_output from powerline.shell import get_argparser, finish_args, ShellPowerline, write_output
from powerline.lib.monotonic import monotonic from powerline.lib.monotonic import monotonic
@ -98,8 +98,8 @@ def render(args, environ, cwd):
powerline.pl.exception('Failed to render {0}: {1}', str(key), str(e)) powerline.pl.exception('Failed to render {0}: {1}', str(key), str(e))
else: else:
return 'Failed to render {0}: {1}'.format(str(key), str(e)) return 'Failed to render {0}: {1}'.format(str(key), str(e))
s = StringIO() s = BytesIO()
write_output(args, powerline, segment_info, s.write) write_output(args, powerline, segment_info, s.write, encoding)
s.seek(0) s.seek(0)
return s.read() return s.read()

View File

@ -4,6 +4,8 @@
import sys import sys
import os import os
from locale import getpreferredencoding
try: try:
from powerline.shell import ShellPowerline, get_argparser, finish_args, write_output from powerline.shell import ShellPowerline, get_argparser, finish_args, write_output
except ImportError: except ImportError:
@ -11,11 +13,10 @@ except ImportError:
from powerline.shell import ShellPowerline, get_argparser, finish_args, write_output # NOQA from powerline.shell import ShellPowerline, get_argparser, finish_args, write_output # NOQA
def write(output): if sys.version_info < (3,):
try: write = sys.stdout.write
sys.stdout.write(output) else:
except UnicodeEncodeError: write = sys.stdout.buffer.write
sys.stdout.write(output.encode('utf-8'))
if __name__ == '__main__': if __name__ == '__main__':
@ -23,4 +24,4 @@ if __name__ == '__main__':
finish_args(args) finish_args(args)
powerline = ShellPowerline(args, run_once=True) powerline = ShellPowerline(args, run_once=True)
segment_info = {'args': args, 'environ': os.environ} segment_info = {'args': args, 'environ': os.environ}
write_output(args, powerline, segment_info, write) write_output(args, powerline, segment_info, write, getpreferredencoding())