mirror of
https://github.com/powerline/powerline.git
synced 2025-07-24 22:36:01 +02:00
* ipython fix * ipython fix * ipython fix * fix ipython startup * better fix for nbsp * ipython7: fix attributes * Change ipythen tests to use new binding * ipython test: use proper config
This commit is contained in:
parent
bc33b1ce31
commit
f9ce8201c4
81
powerline/bindings/ipython/since_7.py
Normal file
81
powerline/bindings/ipython/since_7.py
Normal file
@ -0,0 +1,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_7 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_7')
|
||||||
|
|
||||||
|
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))
|
91
powerline/renderers/ipython/since_7.py
Normal file
91
powerline/renderers/ipython/since_7.py
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
# vim:fileencoding=utf-8:noet
|
||||||
|
import operator
|
||||||
|
|
||||||
|
try:
|
||||||
|
from __builtin__ import reduce
|
||||||
|
except ImportError:
|
||||||
|
from functools import reduce
|
||||||
|
|
||||||
|
from pygments.token import Token
|
||||||
|
from prompt_toolkit.styles import DynamicStyle
|
||||||
|
|
||||||
|
from powerline.renderers.ipython import IPythonRenderer
|
||||||
|
from powerline.ipython import IPythonInfo
|
||||||
|
from powerline.colorscheme import ATTR_BOLD, ATTR_ITALIC, ATTR_UNDERLINE
|
||||||
|
|
||||||
|
used_styles = []
|
||||||
|
seen = set()
|
||||||
|
|
||||||
|
class PowerlinePromptStyle(DynamicStyle):
|
||||||
|
@property
|
||||||
|
def style_rules(self):
|
||||||
|
return (self.get_style() or self._dummy).style_rules + used_styles
|
||||||
|
|
||||||
|
def invalidation_hash(self):
|
||||||
|
return (h + 1 for h in tuple(super(PowerlinePromptStyle, self).invalidation_hash()))
|
||||||
|
|
||||||
|
|
||||||
|
class IPythonPygmentsRenderer(IPythonRenderer):
|
||||||
|
reduce_initial = []
|
||||||
|
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
super(IPythonPygmentsRenderer, self).__init__(**kwargs)
|
||||||
|
self.character_translations[ord(' ')] = ' '
|
||||||
|
|
||||||
|
def get_segment_info(self, segment_info, mode):
|
||||||
|
return super(IPythonPygmentsRenderer, self).get_segment_info(
|
||||||
|
IPythonInfo(segment_info), mode)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def hl_join(segments):
|
||||||
|
return reduce(operator.iadd, segments, [])
|
||||||
|
|
||||||
|
def hl(self, escaped_contents, fg=None, bg=None, attrs=None, *args, **kwargs):
|
||||||
|
'''Output highlighted chunk.
|
||||||
|
|
||||||
|
This implementation outputs a list containing a single pair
|
||||||
|
(:py:class:`string`,
|
||||||
|
:py:class:`powerline.lib.unicode.unicode`).
|
||||||
|
'''
|
||||||
|
guifg = None
|
||||||
|
guibg = None
|
||||||
|
att = []
|
||||||
|
if fg is not None and fg is not False:
|
||||||
|
guifg = fg[1]
|
||||||
|
if bg is not None and bg is not False:
|
||||||
|
guibg = bg[1]
|
||||||
|
if attrs:
|
||||||
|
att = []
|
||||||
|
if attrs & ATTR_BOLD:
|
||||||
|
att.append('bold')
|
||||||
|
if attrs & ATTR_ITALIC:
|
||||||
|
att.append('italic')
|
||||||
|
if attrs & ATTR_UNDERLINE:
|
||||||
|
att.append('underline')
|
||||||
|
|
||||||
|
fg = (('%06x' % guifg) if guifg is not None else '')
|
||||||
|
bg = (('%06x' % guibg) if guibg is not None else '')
|
||||||
|
name = (
|
||||||
|
'pl'
|
||||||
|
+ ''.join(('_a' + attr for attr in att))
|
||||||
|
+ '_f' + fg + '_b' + bg
|
||||||
|
)
|
||||||
|
|
||||||
|
global seen
|
||||||
|
if not (name in seen):
|
||||||
|
global used_styles
|
||||||
|
used_styles += [('pygments.' + name,
|
||||||
|
''.join((' ' + attr for attr in att))
|
||||||
|
+ (' fg:#' + fg if fg != '' else ' fg:')
|
||||||
|
+ (' bg:#' + bg if bg != '' else ' bg:'))]
|
||||||
|
seen.add(name)
|
||||||
|
return [((name,), escaped_contents)]
|
||||||
|
|
||||||
|
def hlstyle(self, *args, **kwargs):
|
||||||
|
return []
|
||||||
|
|
||||||
|
def get_client_id(self, segment_info):
|
||||||
|
return id(self)
|
||||||
|
|
||||||
|
|
||||||
|
renderer = IPythonPygmentsRenderer
|
@ -1,6 +1,9 @@
|
|||||||
|
from powerline.bindings.ipython.since_7 import PowerlinePrompts
|
||||||
import os
|
import os
|
||||||
c = get_config()
|
c = get_config()
|
||||||
c.InteractiveShellApp.extensions = ['powerline.bindings.ipython.post_0_11']
|
c.TerminalInteractiveShell.simple_prompt = False
|
||||||
|
c.TerminalIPythonApp.display_banner = False
|
||||||
|
c.TerminalInteractiveShell.prompts_class = PowerlinePrompts
|
||||||
c.TerminalInteractiveShell.autocall = 1
|
c.TerminalInteractiveShell.autocall = 1
|
||||||
c.Powerline.config_paths = [os.path.abspath('powerline/config_files')]
|
c.Powerline.config_paths = [os.path.abspath('powerline/config_files')]
|
||||||
c.Powerline.theme_overrides = {
|
c.Powerline.theme_overrides = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user