From ea6e28a037f69f04596530f0ce60f368461ce069 Mon Sep 17 00:00:00 2001 From: ZyX Date: Wed, 6 Aug 2014 16:56:20 +0400 Subject: [PATCH] Use the same hack for post_0_11 rewrite prompt IPython again tries to leave us without unicode in rewrite prompt. But in Python, one needs to be more serious than that to be actually able to do this. --- powerline/bindings/ipython/post_0_11.py | 8 ++++++-- powerline/bindings/ipython/pre_0_11.py | 27 ++----------------------- powerline/ipython.py | 18 +++++++++++++++++ powerline/lib/unicode.py | 7 +++++++ 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/powerline/bindings/ipython/post_0_11.py b/powerline/bindings/ipython/post_0_11.py index 1a364e5c..dc6c8112 100644 --- a/powerline/bindings/ipython/post_0_11.py +++ b/powerline/bindings/ipython/post_0_11.py @@ -1,5 +1,5 @@ # vim:fileencoding=utf-8:noet -from powerline.ipython import IpythonPowerline +from powerline.ipython import IpythonPowerline, RewriteResult from IPython.core.prompts import PromptManager from IPython.core.hooks import TryNext @@ -35,7 +35,11 @@ class PowerlinePromptManager(PromptManager): ) self.txtwidth = len(res_nocolor) self.width = self.txtwidth - return res if color else res_nocolor + ret = res if color else res_nocolor + if name == 'rewrite': + return RewriteResult(ret) + else: + return ret class ConfigurableIpythonPowerline(IpythonPowerline): diff --git a/powerline/bindings/ipython/pre_0_11.py b/powerline/bindings/ipython/pre_0_11.py index 26356c5d..8d7f0503 100644 --- a/powerline/bindings/ipython/pre_0_11.py +++ b/powerline/bindings/ipython/pre_0_11.py @@ -1,5 +1,6 @@ # vim:fileencoding=utf-8:noet -from powerline.ipython import IpythonPowerline +from powerline.ipython import IpythonPowerline, RewriteResult +from powerline.lib.unicode import string from IPython.Prompts import BasePrompt from IPython.ipapi import get as get_ipython from IPython.ipapi import TryNext @@ -7,30 +8,6 @@ from IPython.ipapi import TryNext import re -def string(s): - if type(s) is not str: - return s.encode('utf-8') - else: - return s - - -# HACK: ipython tries to only leave us with plain ASCII -class RewriteResult(object): - def __init__(self, prompt): - self.prompt = string(prompt) - - def __str__(self): - return self.prompt - - def __add__(self, s): - if type(s) is not str: - try: - s = s.encode('utf-8') - except AttributeError: - raise NotImplementedError - return RewriteResult(self.prompt + s) - - class IpythonInfo(object): def __init__(self, cache): self._cache = cache diff --git a/powerline/ipython.py b/powerline/ipython.py index 51cc4881..6716e075 100644 --- a/powerline/ipython.py +++ b/powerline/ipython.py @@ -2,6 +2,24 @@ from powerline import Powerline from powerline.lib import mergedicts +from powerline.lib.unicode import string + + +# HACK: ipython tries to only leave us with plain ASCII +class RewriteResult(object): + def __init__(self, prompt): + self.prompt = string(prompt) + + def __str__(self): + return self.prompt + + def __add__(self, s): + if type(s) is not str: + try: + s = s.encode('utf-8') + except AttributeError: + raise NotImplementedError + return RewriteResult(self.prompt + s) class IpythonPowerline(Powerline): diff --git a/powerline/lib/unicode.py b/powerline/lib/unicode.py index a1a5d567..34505e9a 100644 --- a/powerline/lib/unicode.py +++ b/powerline/lib/unicode.py @@ -56,3 +56,10 @@ class FailedUnicode(unicode): FailedUnicode. ''' pass + + +def string(s): + if type(s) is not str: + return s.encode('utf-8') + else: + return s