mirror of
https://github.com/powerline/powerline.git
synced 2025-07-27 07:44:36 +02:00
Merge pull request #1065 from ZyX-I/fix-1064
Fix some possible unicode errors
This commit is contained in:
commit
5f37ab7e3e
@ -1,6 +1,9 @@
|
|||||||
# vim:fileencoding=utf-8:noet
|
# vim:fileencoding=utf-8:noet
|
||||||
from __future__ import (unicode_literals, division, absolute_import, print_function)
|
from __future__ import (unicode_literals, division, absolute_import, print_function)
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import codecs
|
||||||
|
|
||||||
from locale import getpreferredencoding
|
from locale import getpreferredencoding
|
||||||
|
|
||||||
|
|
||||||
@ -25,6 +28,45 @@ def u(s):
|
|||||||
return unicode(s, 'utf-8')
|
return unicode(s, 'utf-8')
|
||||||
|
|
||||||
|
|
||||||
|
if sys.version_info < (3,):
|
||||||
|
def tointiter(s):
|
||||||
|
'''Convert a byte string to the sequence of integers
|
||||||
|
'''
|
||||||
|
return (ord(c) for c in s)
|
||||||
|
else:
|
||||||
|
def tointiter(s):
|
||||||
|
'''Convert a byte string to the sequence of integers
|
||||||
|
'''
|
||||||
|
return iter(s)
|
||||||
|
|
||||||
|
|
||||||
|
def powerline_decode_error(e):
|
||||||
|
if not isinstance(e, UnicodeDecodeError):
|
||||||
|
raise NotImplementedError
|
||||||
|
return (''.join((
|
||||||
|
'<{0:02X}>'.format(c)
|
||||||
|
for c in tointiter(e.object[e.start:e.end])
|
||||||
|
)), e.end)
|
||||||
|
|
||||||
|
|
||||||
|
codecs.register_error('powerline_decode_error', powerline_decode_error)
|
||||||
|
|
||||||
|
|
||||||
|
def out_u(s):
|
||||||
|
'''Return unicode string suitable for displaying
|
||||||
|
|
||||||
|
Unlike other functions assumes getpreferredencoding() first. Unlike u() does
|
||||||
|
not throw exceptions for invalid unicode strings. Unlike safe_unicode() does
|
||||||
|
throw an exception if object is not a string.
|
||||||
|
'''
|
||||||
|
if isinstance(s, unicode):
|
||||||
|
return s
|
||||||
|
elif isinstance(s, bytes):
|
||||||
|
return unicode(s, getpreferredencoding(), 'powerline_decode_error')
|
||||||
|
else:
|
||||||
|
raise TypeError('Expected unicode or bytes instance, got {0}'.format(repr(type(s))))
|
||||||
|
|
||||||
|
|
||||||
def safe_unicode(s):
|
def safe_unicode(s):
|
||||||
'''Return unicode instance without raising an exception.
|
'''Return unicode instance without raising an exception.
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ from powerline.lib.vcs import guess, tree_status
|
|||||||
from powerline.lib.threaded import ThreadedSegment, KwThreadedSegment
|
from powerline.lib.threaded import ThreadedSegment, KwThreadedSegment
|
||||||
from powerline.lib.monotonic import monotonic
|
from powerline.lib.monotonic import monotonic
|
||||||
from powerline.lib.humanize_bytes import humanize_bytes
|
from powerline.lib.humanize_bytes import humanize_bytes
|
||||||
from powerline.lib.unicode import u
|
from powerline.lib.unicode import out_u
|
||||||
from powerline.theme import requires_segment_info, requires_filesystem_watcher
|
from powerline.theme import requires_segment_info, requires_filesystem_watcher
|
||||||
from powerline.segments import Segment, with_docstring
|
from powerline.segments import Segment, with_docstring
|
||||||
|
|
||||||
@ -94,7 +94,7 @@ class CwdSegment(Segment):
|
|||||||
|
|
||||||
def get_shortened_path(self, pl, segment_info, shorten_home=True, **kwargs):
|
def get_shortened_path(self, pl, segment_info, shorten_home=True, **kwargs):
|
||||||
try:
|
try:
|
||||||
path = u(segment_info['getcwd']())
|
path = out_u(segment_info['getcwd']())
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
if e.errno == 2:
|
if e.errno == 2:
|
||||||
# user most probably deleted the directory
|
# user most probably deleted the directory
|
||||||
@ -106,7 +106,7 @@ class CwdSegment(Segment):
|
|||||||
if shorten_home:
|
if shorten_home:
|
||||||
home = segment_info['home']
|
home = segment_info['home']
|
||||||
if home:
|
if home:
|
||||||
home = u(home)
|
home = out_u(home)
|
||||||
if path.startswith(home):
|
if path.startswith(home):
|
||||||
path = '~' + path[len(home):]
|
path = '~' + path[len(home):]
|
||||||
return path
|
return path
|
||||||
@ -1123,12 +1123,12 @@ class NowPlayingSegment(Segment):
|
|||||||
return
|
return
|
||||||
if not info:
|
if not info:
|
||||||
return
|
return
|
||||||
album = u(info.get('xesam:album'))
|
album = out_u(info.get('xesam:album'))
|
||||||
title = u(info.get('xesam:title'))
|
title = out_u(info.get('xesam:title'))
|
||||||
artist = info.get('xesam:artist')
|
artist = info.get('xesam:artist')
|
||||||
state = self._convert_state(status)
|
state = self._convert_state(status)
|
||||||
if artist:
|
if artist:
|
||||||
artist = u(artist[0])
|
artist = out_u(artist[0])
|
||||||
return {
|
return {
|
||||||
'state': state,
|
'state': state,
|
||||||
'album': album,
|
'album': album,
|
||||||
|
@ -4,6 +4,7 @@ from __future__ import (unicode_literals, division, absolute_import, print_funct
|
|||||||
from powerline.theme import requires_segment_info
|
from powerline.theme import requires_segment_info
|
||||||
from powerline.segments import with_docstring
|
from powerline.segments import with_docstring
|
||||||
from powerline.segments.common import CwdSegment
|
from powerline.segments.common import CwdSegment
|
||||||
|
from powerline.lib.unicode import out_u
|
||||||
|
|
||||||
|
|
||||||
@requires_segment_info
|
@requires_segment_info
|
||||||
@ -136,7 +137,7 @@ class ShellCwdSegment(CwdSegment):
|
|||||||
def get_shortened_path(self, pl, segment_info, use_shortened_path=True, **kwargs):
|
def get_shortened_path(self, pl, segment_info, use_shortened_path=True, **kwargs):
|
||||||
if use_shortened_path:
|
if use_shortened_path:
|
||||||
try:
|
try:
|
||||||
return segment_info['shortened_path']
|
return out_u(segment_info['shortened_path'])
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
return super(ShellCwdSegment, self).get_shortened_path(pl, segment_info, **kwargs)
|
return super(ShellCwdSegment, self).get_shortened_path(pl, segment_info, **kwargs)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user