summaryrefslogtreecommitdiffstats
path: root/powerline/bindings/ipython/since_5.py
blob: 5a899ae84158bbd8bcb1713392f19b1ca08faa0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)

from weakref import ref

from IPython.terminal.prompts import Prompts
from pygments.token import Token  # NOQA

from powerline.ipython import IPythonPowerline
from powerline.renderers.ipython.since_5 import PowerlinePromptStyle
from powerline.bindings.ipython.post_0_11 import PowerlineMagics, ShutdownHook


class ConfigurableIPythonPowerline(IPythonPowerline):
	def init(self, ip):
		config = ip.config.Powerline
		self.config_overrides = config.get('config_overrides')
		self.theme_overrides = config.get('theme_overrides', {})
		self.config_paths = config.get('config_paths')
		super(ConfigurableIPythonPowerline, self).init(
			renderer_module='.since_5')

	def do_setup(self, ip, prompts, shutdown_hook):
		prompts.powerline = self

		msfn_missing = ()
		saved_msfn = getattr(ip, '_make_style_from_name', msfn_missing)

		if hasattr(saved_msfn, 'powerline_original'):
			saved_msfn = saved_msfn.powerline_original

		def _make_style_from_name(ip, name):
			prev_style = saved_msfn(name)
			new_style = PowerlinePromptStyle(lambda: prev_style)
			return new_style

		_make_style_from_name.powerline_original = saved_msfn

		if not isinstance(ip._style, PowerlinePromptStyle):
			prev_style = ip._style
			ip._style = PowerlinePromptStyle(lambda: prev_style)

		if not isinstance(saved_msfn, type(self.init)):
			_saved_msfn = saved_msfn
			saved_msfn = lambda: _saved_msfn(ip)

		if saved_msfn is not msfn_missing:
			ip._make_style_from_name = _make_style_from_name

		magics = PowerlineMagics(ip, self)
		ip.register_magics(magics)

		if shutdown_hook:
			shutdown_hook.powerline = ref(self)


class PowerlinePrompts(Prompts):
	'''Class that returns powerline prompts
	'''
	def __init__(self, shell):
		shutdown_hook = ShutdownHook(shell)
		powerline = ConfigurableIPythonPowerline(shell)
		self.shell = shell
		powerline.do_setup(shell, self, shutdown_hook)
		self.last_output_count = None
		self.last_output = {}

	for prompt in ('in', 'continuation', 'rewrite', 'out'):
		exec((
			'def {0}_prompt_tokens(self, *args, **kwargs):\n'
			'	if self.last_output_count != self.shell.execution_count:\n'
			'		self.last_output.clear()\n'
			'		self.last_output_count = self.shell.execution_count\n'
			'	if "{0}" not in self.last_output:\n'
			'		self.last_output["{0}"] = self.powerline.render('
			'			side="left",'
			'			matcher_info="{1}",'
			'			segment_info=self.shell,'
			'		) + [(Token.Generic.Prompt, " ")]\n'
			'	return self.last_output["{0}"]'
		).format(prompt, 'in2' if prompt == 'continuation' else prompt))