Show More
@@ -13,13 +13,13 b' from IPython.core.interactiveshell import InteractiveShell' | |||
|
13 | 13 | from IPython.utils.py3compat import PY3, cast_unicode_py2, input |
|
14 | 14 | from IPython.utils.terminal import toggle_set_term_title, set_term_title |
|
15 | 15 | from IPython.utils.process import abbrev_cwd |
|
16 | from traitlets import Bool, CBool, Unicode, Dict | |
|
16 | from traitlets import Bool, CBool, Unicode, Dict, Integer | |
|
17 | 17 | |
|
18 | 18 | from prompt_toolkit.completion import Completer, Completion |
|
19 | 19 | from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER |
|
20 | 20 | from prompt_toolkit.filters import HasFocus, HasSelection, Condition |
|
21 | 21 | from prompt_toolkit.history import InMemoryHistory |
|
22 | from prompt_toolkit.shortcuts import create_prompt_application, create_eventloop | |
|
22 | from prompt_toolkit.shortcuts import create_prompt_application, create_eventloop, create_prompt_layout | |
|
23 | 23 | from prompt_toolkit.interface import CommandLineInterface |
|
24 | 24 | from prompt_toolkit.key_binding.manager import KeyBindingManager |
|
25 | 25 | from prompt_toolkit.key_binding.vi_state import InputMode |
@@ -27,9 +27,9 b' from prompt_toolkit.key_binding.bindings.vi import ViStateFilter' | |||
|
27 | 27 | from prompt_toolkit.keys import Keys |
|
28 | 28 | from prompt_toolkit.layout.lexers import Lexer |
|
29 | 29 | from prompt_toolkit.layout.lexers import PygmentsLexer |
|
30 | from prompt_toolkit.styles import PygmentsStyle | |
|
30 | from prompt_toolkit.styles import PygmentsStyle, DynamicStyle | |
|
31 | 31 | |
|
32 | from pygments.styles import get_style_by_name | |
|
32 | from pygments.styles import get_style_by_name, get_all_styles | |
|
33 | 33 | from pygments.lexers import Python3Lexer, BashLexer, PythonLexer |
|
34 | 34 | from pygments.token import Token |
|
35 | 35 | |
@@ -89,6 +89,12 b' class IPythonPTLexer(Lexer):' | |||
|
89 | 89 | class TerminalInteractiveShell(InteractiveShell): |
|
90 | 90 | colors_force = True |
|
91 | 91 | |
|
92 | space_for_menu = Integer(6, config=True, help='Number of line at the bottom of the screen ' | |
|
93 | 'to reserve for the completion menu') | |
|
94 | ||
|
95 | def _space_for_menu_changed(self, old, new): | |
|
96 | self._update_layout() | |
|
97 | ||
|
92 | 98 | pt_cli = None |
|
93 | 99 | |
|
94 | 100 | autoedit_syntax = CBool(False, config=True, |
@@ -108,10 +114,13 b' class TerminalInteractiveShell(InteractiveShell):' | |||
|
108 | 114 | help="Enable mouse support in the prompt" |
|
109 | 115 | ) |
|
110 | 116 | |
|
111 | highlighting_style = Unicode('', config=True, | |
|
112 | help="The name of a Pygments style to use for syntax highlighting" | |
|
117 | highlighting_style = Unicode('default', config=True, | |
|
118 | help="The name of a Pygments style to use for syntax highlighting: \n %s" % ', '.join(get_all_styles()) | |
|
113 | 119 | ) |
|
114 | 120 | |
|
121 | def _highlighting_style_changed(self, old, new): | |
|
122 | self._style = self._make_style_from_name(self.highlighting_style) | |
|
123 | ||
|
115 | 124 | highlighting_style_overrides = Dict(config=True, |
|
116 | 125 | help="Override highlighting format for specific tokens" |
|
117 | 126 | ) |
@@ -220,13 +229,33 b' class TerminalInteractiveShell(InteractiveShell):' | |||
|
220 | 229 | if cell and (cell != last_cell): |
|
221 | 230 | history.append(cell) |
|
222 | 231 | |
|
232 | self._style = self._make_style_from_name(self.highlighting_style) | |
|
233 | style = DynamicStyle(lambda: self._style) | |
|
234 | ||
|
235 | self._app = create_prompt_application( | |
|
236 | key_bindings_registry=kbmanager.registry, | |
|
237 | history=history, | |
|
238 | completer=IPythonPTCompleter(self.Completer), | |
|
239 | enable_history_search=True, | |
|
240 | style=style, | |
|
241 | mouse_support=self.mouse_support, | |
|
242 | **self._layout_options() | |
|
243 | ) | |
|
244 | self.pt_cli = CommandLineInterface(self._app, | |
|
245 | eventloop=create_eventloop(self.inputhook)) | |
|
246 | ||
|
247 | def _make_style_from_name(self, name): | |
|
248 | """ | |
|
249 | Small wrapper that make an IPython compatible style from a style name | |
|
250 | ||
|
251 | We need that to add style for prompt ... etc. | |
|
252 | """ | |
|
253 | style_cls = get_style_by_name(name) | |
|
223 | 254 | style_overrides = { |
|
224 | Token.Prompt: '#009900', | |
|
225 |
Token.PromptNum: '#00ff00 bold' |
|
|
255 | Token.Prompt: style_cls.styles.get( Token.Keyword, '#009900'), | |
|
256 | Token.PromptNum: style_cls.styles.get( Token.Literal.Number, '#00ff00 bold') | |
|
226 | 257 | } |
|
227 | if self.highlighting_style: | |
|
228 | style_cls = get_style_by_name(self.highlighting_style) | |
|
229 | else: | |
|
258 | if name is 'default': | |
|
230 | 259 | style_cls = get_style_by_name('default') |
|
231 | 260 | # The default theme needs to be visible on both a dark background |
|
232 | 261 | # and a light background, because we can't tell what the terminal |
@@ -243,21 +272,27 b' class TerminalInteractiveShell(InteractiveShell):' | |||
|
243 | 272 | style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls, |
|
244 | 273 | style_dict=style_overrides) |
|
245 | 274 | |
|
246 | app = create_prompt_application(multiline=True, | |
|
247 | lexer=IPythonPTLexer(), | |
|
248 | get_prompt_tokens=self.get_prompt_tokens, | |
|
249 | get_continuation_tokens=self.get_continuation_tokens, | |
|
250 | key_bindings_registry=kbmanager.registry, | |
|
251 | history=history, | |
|
252 | completer=IPythonPTCompleter(self.Completer), | |
|
253 | enable_history_search=True, | |
|
254 | style=style, | |
|
255 | mouse_support=self.mouse_support, | |
|
256 | reserve_space_for_menu=6, | |
|
257 | ) | |
|
275 | return style | |
|
258 | 276 | |
|
259 | self.pt_cli = CommandLineInterface(app, | |
|
260 | eventloop=create_eventloop(self.inputhook)) | |
|
277 | def _layout_options(self): | |
|
278 | """ | |
|
279 | Return the current layout option for the current Terminal InteractiveShell | |
|
280 | """ | |
|
281 | return { | |
|
282 | 'lexer':IPythonPTLexer(), | |
|
283 | 'reserve_space_for_menu':self.space_for_menu, | |
|
284 | 'get_prompt_tokens':self.get_prompt_tokens, | |
|
285 | 'get_continuation_tokens':self.get_continuation_tokens, | |
|
286 | 'multiline':False, | |
|
287 | } | |
|
288 | ||
|
289 | ||
|
290 | def _update_layout(self): | |
|
291 | """ | |
|
292 | Ask for a re computation of the application layout, if for example , | |
|
293 | some configuration options have changed. | |
|
294 | """ | |
|
295 | self._app.layout = create_prompt_layout(**self._layout_options()) | |
|
261 | 296 | |
|
262 | 297 | def prompt_for_code(self): |
|
263 | 298 | document = self.pt_cli.run(pre_run=self.pre_prompt) |
General Comments 0
You need to be logged in to leave comments.
Login now