##// 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 from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER
19 from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER
20 from prompt_toolkit.filters import HasFocus, HasSelection, Condition
20 from prompt_toolkit.filters import HasFocus, HasSelection, Condition
21 from prompt_toolkit.history import InMemoryHistory
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 from prompt_toolkit.interface import CommandLineInterface
23 from prompt_toolkit.interface import CommandLineInterface
24 from prompt_toolkit.key_binding.manager import KeyBindingManager
24 from prompt_toolkit.key_binding.manager import KeyBindingManager
25 from prompt_toolkit.key_binding.vi_state import InputMode
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 from prompt_toolkit.keys import Keys
27 from prompt_toolkit.keys import Keys
28 from prompt_toolkit.layout.lexers import Lexer
28 from prompt_toolkit.layout.lexers import Lexer
29 from prompt_toolkit.layout.lexers import PygmentsLexer
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 from pygments.lexers import Python3Lexer, BashLexer, PythonLexer
33 from pygments.lexers import Python3Lexer, BashLexer, PythonLexer
34 from pygments.token import Token
34 from pygments.token import Token
35
35
@@ -92,6 +92,9 b' class TerminalInteractiveShell(InteractiveShell):'
92 space_for_menu = Integer(6, config=True, help='space at the bottom of the screen to reserve for'
92 space_for_menu = Integer(6, config=True, help='space at the bottom of the screen to reserve for'
93 'the completion menu')
93 'the completion menu')
94
94
95 def _space_for_menu_changed(self, old, new):
96 self.relayout()
97
95 pt_cli = None
98 pt_cli = None
96
99
97 autoedit_syntax = CBool(False, config=True,
100 autoedit_syntax = CBool(False, config=True,
@@ -111,10 +114,13 b' class TerminalInteractiveShell(InteractiveShell):'
111 help="Enable mouse support in the prompt"
114 help="Enable mouse support in the prompt"
112 )
115 )
113
116
114 highlighting_style = Unicode('', config=True,
117 highlighting_style = Unicode('default', config=True,
115 help="The name of a Pygments style to use for syntax highlighting"
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 highlighting_style_overrides = Dict(config=True,
124 highlighting_style_overrides = Dict(config=True,
119 help="Override highlighting format for specific tokens"
125 help="Override highlighting format for specific tokens"
120 )
126 )
@@ -223,13 +229,33 b' class TerminalInteractiveShell(InteractiveShell):'
223 if cell and (cell != last_cell):
229 if cell and (cell != last_cell):
224 history.append(cell)
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 style_overrides = {
254 style_overrides = {
227 Token.Prompt: '#009900',
255 Token.Prompt: '#009900',
228 Token.PromptNum: '#00ff00 bold',
256 Token.PromptNum: '#00ff00 bold',
229 }
257 }
230 if self.highlighting_style:
258 if name is 'default':
231 style_cls = get_style_by_name(self.highlighting_style)
232 else:
233 style_cls = get_style_by_name('default')
259 style_cls = get_style_by_name('default')
234 # The default theme needs to be visible on both a dark background
260 # The default theme needs to be visible on both a dark background
235 # and a light background, because we can't tell what the terminal
261 # and a light background, because we can't tell what the terminal
@@ -246,21 +272,27 b' class TerminalInteractiveShell(InteractiveShell):'
246 style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls,
272 style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls,
247 style_dict=style_overrides)
273 style_dict=style_overrides)
248
274
249 app = create_prompt_application(multiline=True,
275 return style
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 )
261
276
262 self.pt_cli = CommandLineInterface(app,
277 def _layout_options(self):
263 eventloop=create_eventloop(self.inputhook))
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 def prompt_for_code(self):
297 def prompt_for_code(self):
266 document = self.pt_cli.run(pre_run=self.pre_prompt)
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