##// END OF EJS Templates
Make style and layout dynamic....
Matthias Bussonnier -
Show More
@@ -19,7 +19,7 b' 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
@@ -92,6 +92,9 b' class TerminalInteractiveShell(InteractiveShell):'
92 92 space_for_menu = Integer(6, config=True, help='space at the bottom of the screen to reserve for'
93 93 'the completion menu')
94 94
95 def _space_for_menu_changed(self, old, new):
96 self.relayout()
97
95 98 pt_cli = None
96 99
97 100 autoedit_syntax = CBool(False, config=True,
@@ -111,10 +114,13 b' class TerminalInteractiveShell(InteractiveShell):'
111 114 help="Enable mouse support in the prompt"
112 115 )
113 116
114 highlighting_style = Unicode('', config=True,
115 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())
116 119 )
117 120
121 def _highlighting_style_changed(self, old, new):
122 self._style = self._make_style_from_name(self.highlighting_style)
123
118 124 highlighting_style_overrides = Dict(config=True,
119 125 help="Override highlighting format for specific tokens"
120 126 )
@@ -223,13 +229,33 b' class TerminalInteractiveShell(InteractiveShell):'
223 229 if cell and (cell != last_cell):
224 230 history.append(cell)
225 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)
226 254 style_overrides = {
227 255 Token.Prompt: '#009900',
228 256 Token.PromptNum: '#00ff00 bold',
229 257 }
230 if self.highlighting_style:
231 style_cls = get_style_by_name(self.highlighting_style)
232 else:
258 if name is 'default':
233 259 style_cls = get_style_by_name('default')
234 260 # The default theme needs to be visible on both a dark background
235 261 # and a light background, because we can't tell what the terminal
@@ -246,21 +272,27 b' class TerminalInteractiveShell(InteractiveShell):'
246 272 style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls,
247 273 style_dict=style_overrides)
248 274
249 app = create_prompt_application(multiline=True,
250 lexer=IPythonPTLexer(),
251 get_prompt_tokens=self.get_prompt_tokens,
252 get_continuation_tokens=self.get_continuation_tokens,
253 key_bindings_registry=kbmanager.registry,
254 history=history,
255 completer=IPythonPTCompleter(self.Completer),
256 enable_history_search=True,
257 style=style,
258 mouse_support=self.mouse_support,
259 reserve_space_for_menu=self.space_for_menu,
260 )
275 return style
261 276
262 self.pt_cli = CommandLineInterface(app,
263 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 _relayout(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())
264 296
265 297 def prompt_for_code(self):
266 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