Do not output \x01/\x02 symbols for non-prompts in ipython

Should fix #664
This commit is contained in:
ZyX 2014-08-06 13:38:45 +04:00
parent faceb59876
commit 85ea44b1ee
5 changed files with 56 additions and 26 deletions

View File

@ -15,28 +15,36 @@ class IpythonInfo(object):
class PowerlinePromptManager(PromptManager):
powerline = None
def __init__(self, powerline, shell):
self.powerline = powerline
def __init__(self, prompt_powerline, non_prompt_powerline, shell):
self.prompt_powerline = prompt_powerline
self.non_prompt_powerline = non_prompt_powerline
self.powerline_segment_info = IpythonInfo(shell)
self.shell = shell
def render(self, name, color=True, *args, **kwargs):
width = None if name == 'in' else self.width
res, res_nocolor = self.powerline.render(output_raw=True, width=width, matcher_info=name, segment_info=self.powerline_segment_info)
if name == 'out' or name == 'rewrite':
powerline = self.non_prompt_powerline
else:
powerline = self.prompt_powerline
res, res_nocolor = powerline.render(
output_raw=True,
width=width,
matcher_info=name,
segment_info=self.powerline_segment_info,
)
self.txtwidth = len(res_nocolor)
self.width = self.txtwidth
return res if color else res_nocolor
class ConfigurableIpythonPowerline(IpythonPowerline):
def __init__(self, ip):
def __init__(self, ip, is_prompt):
config = ip.config.Powerline
self.config_overrides = config.get('config_overrides')
self.theme_overrides = config.get('theme_overrides', {})
self.path = config.get('path')
super(ConfigurableIpythonPowerline, self).__init__()
super(ConfigurableIpythonPowerline, self).__init__(is_prompt)
old_prompt_manager = None
@ -46,12 +54,18 @@ def load_ipython_extension(ip):
global old_prompt_manager
old_prompt_manager = ip.prompt_manager
powerline = ConfigurableIpythonPowerline(ip)
prompt_powerline = ConfigurableIpythonPowerline(ip, True)
non_prompt_powerline = ConfigurableIpythonPowerline(ip, False)
ip.prompt_manager = PowerlinePromptManager(powerline=powerline, shell=ip.prompt_manager.shell)
ip.prompt_manager = PowerlinePromptManager(
prompt_powerline=prompt_powerline,
non_prompt_powerline=non_prompt_powerline,
shell=ip.prompt_manager.shell
)
def shutdown_hook():
powerline.shutdown()
prompt_powerline.shutdown()
non_prompt_powerline.shutdown()
raise TryNext()
ip.hooks.shutdown_hook.add(shutdown_hook)

View File

@ -41,8 +41,9 @@ class IpythonInfo(object):
class PowerlinePrompt(BasePrompt):
def __init__(self, powerline, powerline_last_in, old_prompt):
def __init__(self, powerline, other_powerline, powerline_last_in, old_prompt):
self.powerline = powerline
self.other_powerline = other_powerline
self.powerline_last_in = powerline_last_in
self.powerline_segment_info = IpythonInfo(old_prompt.cache)
self.cache = old_prompt.cache
@ -85,7 +86,7 @@ class PowerlinePrompt1(PowerlinePrompt):
self.powerline_last_in['prompt_text_len'] = self.prompt_text_len
def auto_rewrite(self):
return RewriteResult(self.powerline.render(matcher_info='rewrite', width=self.prompt_text_len, segment_info=self.powerline_segment_info)
return RewriteResult(self.other_powerline.render(matcher_info='rewrite', width=self.prompt_text_len, segment_info=self.powerline_segment_info)
+ (' ' * self.nrspaces))
@ -104,31 +105,33 @@ class PowerlinePrompt2(PowerlinePromptOut):
class ConfigurableIpythonPowerline(IpythonPowerline):
def __init__(self, config_overrides=None, theme_overrides={}, path=None):
def __init__(self, is_prompt, config_overrides=None, theme_overrides={}, path=None):
self.config_overrides = config_overrides
self.theme_overrides = theme_overrides
self.path = path
super(ConfigurableIpythonPowerline, self).__init__()
super(ConfigurableIpythonPowerline, self).__init__(is_prompt)
def setup(**kwargs):
ip = get_ipython()
powerline = ConfigurableIpythonPowerline(**kwargs)
prompt_powerline = ConfigurableIpythonPowerline(True, **kwargs)
non_prompt_powerline = ConfigurableIpythonPowerline(False, **kwargs)
def late_startup_hook():
last_in = {'nrspaces': 0, 'prompt_text_len': None}
for attr, prompt_class in (
('prompt1', PowerlinePrompt1),
('prompt2', PowerlinePrompt2),
('prompt_out', PowerlinePromptOut)
for attr, prompt_class, powerline, other_powerline in (
('prompt1', PowerlinePrompt1, prompt_powerline, non_prompt_powerline),
('prompt2', PowerlinePrompt2, prompt_powerline, None),
('prompt_out', PowerlinePromptOut, non_prompt_powerline, None)
):
old_prompt = getattr(ip.IP.outputcache, attr)
setattr(ip.IP.outputcache, attr, prompt_class(powerline, last_in, old_prompt))
setattr(ip.IP.outputcache, attr, prompt_class(powerline, other_powerline, last_in, old_prompt))
raise TryNext()
def shutdown_hook():
powerline.shutdown()
prompt_powerline.shutdown()
non_prompt_powerline.shutdown()
raise TryNext()
ip.IP.hooks.late_startup_hook.add(late_startup_hook)

View File

@ -5,8 +5,12 @@ from powerline.lib import mergedicts
class IpythonPowerline(Powerline):
def __init__(self):
super(IpythonPowerline, self).__init__('ipython', use_daemon_threads=True)
def __init__(self, is_prompt):
super(IpythonPowerline, self).__init__(
'ipython',
renderer_module=('ipython_prompt' if is_prompt else 'ipython'),
use_daemon_threads=True
)
def get_config_paths(self):
if self.path:

View File

@ -6,9 +6,6 @@ from powerline.theme import Theme
class IpythonRenderer(ShellRenderer):
'''Powerline ipython segment renderer.'''
escape_hl_start = '\x01'
escape_hl_end = '\x02'
def get_segment_info(self, segment_info, mode):
r = self.segment_info.copy()
r['ipython'] = segment_info

View File

@ -0,0 +1,12 @@
# vim:fileencoding=utf-8:noet
from powerline.renderers.ipython import IpythonRenderer
class IpythonPromptRenderer(IpythonRenderer):
'''Powerline ipython prompt renderer'''
escape_hl_start = '\x01'
escape_hl_end = '\x02'
renderer = IpythonPromptRenderer