##// END OF EJS Templates
Add configurable providers for autosuggestions
Alexander Steppke -
Show More
@@ -1,710 +1,728 b''
1 """IPython terminal interface using prompt_toolkit"""
1 """IPython terminal interface using prompt_toolkit"""
2
2
3 import asyncio
3 import asyncio
4 import os
4 import os
5 import sys
5 import sys
6 import warnings
6 import warnings
7 from warnings import warn
7 from warnings import warn
8
8
9 from IPython.core.async_helpers import get_asyncio_loop
9 from IPython.core.async_helpers import get_asyncio_loop
10 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
10 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
11 from IPython.utils import io
11 from IPython.utils import io
12 from IPython.utils.py3compat import input
12 from IPython.utils.py3compat import input
13 from IPython.utils.terminal import toggle_set_term_title, set_term_title, restore_term_title
13 from IPython.utils.terminal import toggle_set_term_title, set_term_title, restore_term_title
14 from IPython.utils.process import abbrev_cwd
14 from IPython.utils.process import abbrev_cwd
15 from traitlets import (
15 from traitlets import (
16 Bool,
16 Bool,
17 Unicode,
17 Unicode,
18 Dict,
18 Dict,
19 Integer,
19 Integer,
20 observe,
20 observe,
21 Instance,
21 Instance,
22 Type,
22 Type,
23 default,
23 default,
24 Enum,
24 Enum,
25 Union,
25 Union,
26 Any,
26 Any,
27 validate,
27 validate,
28 Float,
28 Float,
29 )
29 )
30
30
31 from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
31 from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
32 from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode
32 from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode
33 from prompt_toolkit.filters import (HasFocus, Condition, IsDone)
33 from prompt_toolkit.filters import (HasFocus, Condition, IsDone)
34 from prompt_toolkit.formatted_text import PygmentsTokens
34 from prompt_toolkit.formatted_text import PygmentsTokens
35 from prompt_toolkit.history import InMemoryHistory
35 from prompt_toolkit.history import InMemoryHistory
36 from prompt_toolkit.layout.processors import ConditionalProcessor, HighlightMatchingBracketProcessor
36 from prompt_toolkit.layout.processors import ConditionalProcessor, HighlightMatchingBracketProcessor
37 from prompt_toolkit.output import ColorDepth
37 from prompt_toolkit.output import ColorDepth
38 from prompt_toolkit.patch_stdout import patch_stdout
38 from prompt_toolkit.patch_stdout import patch_stdout
39 from prompt_toolkit.shortcuts import PromptSession, CompleteStyle, print_formatted_text
39 from prompt_toolkit.shortcuts import PromptSession, CompleteStyle, print_formatted_text
40 from prompt_toolkit.styles import DynamicStyle, merge_styles
40 from prompt_toolkit.styles import DynamicStyle, merge_styles
41 from prompt_toolkit.styles.pygments import style_from_pygments_cls, style_from_pygments_dict
41 from prompt_toolkit.styles.pygments import style_from_pygments_cls, style_from_pygments_dict
42 from prompt_toolkit import __version__ as ptk_version
42 from prompt_toolkit import __version__ as ptk_version
43
43
44 from pygments.styles import get_style_by_name
44 from pygments.styles import get_style_by_name
45 from pygments.style import Style
45 from pygments.style import Style
46 from pygments.token import Token
46 from pygments.token import Token
47
47
48 from .debugger import TerminalPdb, Pdb
48 from .debugger import TerminalPdb, Pdb
49 from .magics import TerminalMagics
49 from .magics import TerminalMagics
50 from .pt_inputhooks import get_inputhook_name_and_func
50 from .pt_inputhooks import get_inputhook_name_and_func
51 from .prompts import Prompts, ClassicPrompts, RichPromptDisplayHook
51 from .prompts import Prompts, ClassicPrompts, RichPromptDisplayHook
52 from .ptutils import IPythonPTCompleter, IPythonPTLexer
52 from .ptutils import IPythonPTCompleter, IPythonPTLexer
53 from .shortcuts import create_ipython_shortcuts
53 from .shortcuts import create_ipython_shortcuts
54
54
55 PTK3 = ptk_version.startswith('3.')
55 PTK3 = ptk_version.startswith('3.')
56
56
57
57
58 class _NoStyle(Style): pass
58 class _NoStyle(Style): pass
59
59
60
60
61
61
62 _style_overrides_light_bg = {
62 _style_overrides_light_bg = {
63 Token.Prompt: '#ansibrightblue',
63 Token.Prompt: '#ansibrightblue',
64 Token.PromptNum: '#ansiblue bold',
64 Token.PromptNum: '#ansiblue bold',
65 Token.OutPrompt: '#ansibrightred',
65 Token.OutPrompt: '#ansibrightred',
66 Token.OutPromptNum: '#ansired bold',
66 Token.OutPromptNum: '#ansired bold',
67 }
67 }
68
68
69 _style_overrides_linux = {
69 _style_overrides_linux = {
70 Token.Prompt: '#ansibrightgreen',
70 Token.Prompt: '#ansibrightgreen',
71 Token.PromptNum: '#ansigreen bold',
71 Token.PromptNum: '#ansigreen bold',
72 Token.OutPrompt: '#ansibrightred',
72 Token.OutPrompt: '#ansibrightred',
73 Token.OutPromptNum: '#ansired bold',
73 Token.OutPromptNum: '#ansired bold',
74 }
74 }
75
75
76 def get_default_editor():
76 def get_default_editor():
77 try:
77 try:
78 return os.environ['EDITOR']
78 return os.environ['EDITOR']
79 except KeyError:
79 except KeyError:
80 pass
80 pass
81 except UnicodeError:
81 except UnicodeError:
82 warn("$EDITOR environment variable is not pure ASCII. Using platform "
82 warn("$EDITOR environment variable is not pure ASCII. Using platform "
83 "default editor.")
83 "default editor.")
84
84
85 if os.name == 'posix':
85 if os.name == 'posix':
86 return 'vi' # the only one guaranteed to be there!
86 return 'vi' # the only one guaranteed to be there!
87 else:
87 else:
88 return 'notepad' # same in Windows!
88 return 'notepad' # same in Windows!
89
89
90 # conservatively check for tty
90 # conservatively check for tty
91 # overridden streams can result in things like:
91 # overridden streams can result in things like:
92 # - sys.stdin = None
92 # - sys.stdin = None
93 # - no isatty method
93 # - no isatty method
94 for _name in ('stdin', 'stdout', 'stderr'):
94 for _name in ('stdin', 'stdout', 'stderr'):
95 _stream = getattr(sys, _name)
95 _stream = getattr(sys, _name)
96 if not _stream or not hasattr(_stream, 'isatty') or not _stream.isatty():
96 if not _stream or not hasattr(_stream, 'isatty') or not _stream.isatty():
97 _is_tty = False
97 _is_tty = False
98 break
98 break
99 else:
99 else:
100 _is_tty = True
100 _is_tty = True
101
101
102
102
103 _use_simple_prompt = ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or (not _is_tty)
103 _use_simple_prompt = ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or (not _is_tty)
104
104
105 def black_reformat_handler(text_before_cursor):
105 def black_reformat_handler(text_before_cursor):
106 """
106 """
107 We do not need to protect against error,
107 We do not need to protect against error,
108 this is taken care at a higher level where any reformat error is ignored.
108 this is taken care at a higher level where any reformat error is ignored.
109 Indeed we may call reformatting on incomplete code.
109 Indeed we may call reformatting on incomplete code.
110 """
110 """
111 import black
111 import black
112
112
113 formatted_text = black.format_str(text_before_cursor, mode=black.FileMode())
113 formatted_text = black.format_str(text_before_cursor, mode=black.FileMode())
114 if not text_before_cursor.endswith("\n") and formatted_text.endswith("\n"):
114 if not text_before_cursor.endswith("\n") and formatted_text.endswith("\n"):
115 formatted_text = formatted_text[:-1]
115 formatted_text = formatted_text[:-1]
116 return formatted_text
116 return formatted_text
117
117
118
118
119 class TerminalInteractiveShell(InteractiveShell):
119 class TerminalInteractiveShell(InteractiveShell):
120 mime_renderers = Dict().tag(config=True)
120 mime_renderers = Dict().tag(config=True)
121
121
122 space_for_menu = Integer(6, help='Number of line at the bottom of the screen '
122 space_for_menu = Integer(6, help='Number of line at the bottom of the screen '
123 'to reserve for the tab completion menu, '
123 'to reserve for the tab completion menu, '
124 'search history, ...etc, the height of '
124 'search history, ...etc, the height of '
125 'these menus will at most this value. '
125 'these menus will at most this value. '
126 'Increase it is you prefer long and skinny '
126 'Increase it is you prefer long and skinny '
127 'menus, decrease for short and wide.'
127 'menus, decrease for short and wide.'
128 ).tag(config=True)
128 ).tag(config=True)
129
129
130 pt_app = None
130 pt_app = None
131 debugger_history = None
131 debugger_history = None
132
132
133 debugger_history_file = Unicode(
133 debugger_history_file = Unicode(
134 "~/.pdbhistory", help="File in which to store and read history"
134 "~/.pdbhistory", help="File in which to store and read history"
135 ).tag(config=True)
135 ).tag(config=True)
136
136
137 simple_prompt = Bool(_use_simple_prompt,
137 simple_prompt = Bool(_use_simple_prompt,
138 help="""Use `raw_input` for the REPL, without completion and prompt colors.
138 help="""Use `raw_input` for the REPL, without completion and prompt colors.
139
139
140 Useful when controlling IPython as a subprocess, and piping STDIN/OUT/ERR. Known usage are:
140 Useful when controlling IPython as a subprocess, and piping STDIN/OUT/ERR. Known usage are:
141 IPython own testing machinery, and emacs inferior-shell integration through elpy.
141 IPython own testing machinery, and emacs inferior-shell integration through elpy.
142
142
143 This mode default to `True` if the `IPY_TEST_SIMPLE_PROMPT`
143 This mode default to `True` if the `IPY_TEST_SIMPLE_PROMPT`
144 environment variable is set, or the current terminal is not a tty."""
144 environment variable is set, or the current terminal is not a tty."""
145 ).tag(config=True)
145 ).tag(config=True)
146
146
147 @property
147 @property
148 def debugger_cls(self):
148 def debugger_cls(self):
149 return Pdb if self.simple_prompt else TerminalPdb
149 return Pdb if self.simple_prompt else TerminalPdb
150
150
151 confirm_exit = Bool(True,
151 confirm_exit = Bool(True,
152 help="""
152 help="""
153 Set to confirm when you try to exit IPython with an EOF (Control-D
153 Set to confirm when you try to exit IPython with an EOF (Control-D
154 in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
154 in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
155 you can force a direct exit without any confirmation.""",
155 you can force a direct exit without any confirmation.""",
156 ).tag(config=True)
156 ).tag(config=True)
157
157
158 editing_mode = Unicode('emacs',
158 editing_mode = Unicode('emacs',
159 help="Shortcut style to use at the prompt. 'vi' or 'emacs'.",
159 help="Shortcut style to use at the prompt. 'vi' or 'emacs'.",
160 ).tag(config=True)
160 ).tag(config=True)
161
161
162 emacs_bindings_in_vi_insert_mode = Bool(
162 emacs_bindings_in_vi_insert_mode = Bool(
163 True,
163 True,
164 help="Add shortcuts from 'emacs' insert mode to 'vi' insert mode.",
164 help="Add shortcuts from 'emacs' insert mode to 'vi' insert mode.",
165 ).tag(config=True)
165 ).tag(config=True)
166
166
167 modal_cursor = Bool(
167 modal_cursor = Bool(
168 True,
168 True,
169 help="""
169 help="""
170 Cursor shape changes depending on vi mode: beam in vi insert mode,
170 Cursor shape changes depending on vi mode: beam in vi insert mode,
171 block in nav mode, underscore in replace mode.""",
171 block in nav mode, underscore in replace mode.""",
172 ).tag(config=True)
172 ).tag(config=True)
173
173
174 ttimeoutlen = Float(
174 ttimeoutlen = Float(
175 0.01,
175 0.01,
176 help="""The time in milliseconds that is waited for a key code
176 help="""The time in milliseconds that is waited for a key code
177 to complete.""",
177 to complete.""",
178 ).tag(config=True)
178 ).tag(config=True)
179
179
180 timeoutlen = Float(
180 timeoutlen = Float(
181 0.5,
181 0.5,
182 help="""The time in milliseconds that is waited for a mapped key
182 help="""The time in milliseconds that is waited for a mapped key
183 sequence to complete.""",
183 sequence to complete.""",
184 ).tag(config=True)
184 ).tag(config=True)
185
185
186 autoformatter = Unicode(
186 autoformatter = Unicode(
187 "black",
187 "black",
188 help="Autoformatter to reformat Terminal code. Can be `'black'` or `None`",
188 help="Autoformatter to reformat Terminal code. Can be `'black'` or `None`",
189 allow_none=True
189 allow_none=True
190 ).tag(config=True)
190 ).tag(config=True)
191
191
192 auto_match = Bool(
192 auto_match = Bool(
193 False,
193 False,
194 help="""
194 help="""
195 Automatically add/delete closing bracket or quote when opening bracket or quote is entered/deleted.
195 Automatically add/delete closing bracket or quote when opening bracket or quote is entered/deleted.
196 Brackets: (), [], {}
196 Brackets: (), [], {}
197 Quotes: '', \"\"
197 Quotes: '', \"\"
198 """,
198 """,
199 ).tag(config=True)
199 ).tag(config=True)
200
200
201 mouse_support = Bool(False,
201 mouse_support = Bool(False,
202 help="Enable mouse support in the prompt\n(Note: prevents selecting text with the mouse)"
202 help="Enable mouse support in the prompt\n(Note: prevents selecting text with the mouse)"
203 ).tag(config=True)
203 ).tag(config=True)
204
204
205 # We don't load the list of styles for the help string, because loading
205 # We don't load the list of styles for the help string, because loading
206 # Pygments plugins takes time and can cause unexpected errors.
206 # Pygments plugins takes time and can cause unexpected errors.
207 highlighting_style = Union([Unicode('legacy'), Type(klass=Style)],
207 highlighting_style = Union([Unicode('legacy'), Type(klass=Style)],
208 help="""The name or class of a Pygments style to use for syntax
208 help="""The name or class of a Pygments style to use for syntax
209 highlighting. To see available styles, run `pygmentize -L styles`."""
209 highlighting. To see available styles, run `pygmentize -L styles`."""
210 ).tag(config=True)
210 ).tag(config=True)
211
211
212 @validate('editing_mode')
212 @validate('editing_mode')
213 def _validate_editing_mode(self, proposal):
213 def _validate_editing_mode(self, proposal):
214 if proposal['value'].lower() == 'vim':
214 if proposal['value'].lower() == 'vim':
215 proposal['value']= 'vi'
215 proposal['value']= 'vi'
216 elif proposal['value'].lower() == 'default':
216 elif proposal['value'].lower() == 'default':
217 proposal['value']= 'emacs'
217 proposal['value']= 'emacs'
218
218
219 if hasattr(EditingMode, proposal['value'].upper()):
219 if hasattr(EditingMode, proposal['value'].upper()):
220 return proposal['value'].lower()
220 return proposal['value'].lower()
221
221
222 return self.editing_mode
222 return self.editing_mode
223
223
224
224
225 @observe('editing_mode')
225 @observe('editing_mode')
226 def _editing_mode(self, change):
226 def _editing_mode(self, change):
227 if self.pt_app:
227 if self.pt_app:
228 self.pt_app.editing_mode = getattr(EditingMode, change.new.upper())
228 self.pt_app.editing_mode = getattr(EditingMode, change.new.upper())
229
229
230 def _set_formatter(self, formatter):
230 def _set_formatter(self, formatter):
231 if formatter is None:
231 if formatter is None:
232 self.reformat_handler = lambda x:x
232 self.reformat_handler = lambda x:x
233 elif formatter == 'black':
233 elif formatter == 'black':
234 self.reformat_handler = black_reformat_handler
234 self.reformat_handler = black_reformat_handler
235 else:
235 else:
236 raise ValueError
236 raise ValueError
237
237
238 @observe("autoformatter")
238 @observe("autoformatter")
239 def _autoformatter_changed(self, change):
239 def _autoformatter_changed(self, change):
240 formatter = change.new
240 formatter = change.new
241 self._set_formatter(formatter)
241 self._set_formatter(formatter)
242
242
243 @observe('highlighting_style')
243 @observe('highlighting_style')
244 @observe('colors')
244 @observe('colors')
245 def _highlighting_style_changed(self, change):
245 def _highlighting_style_changed(self, change):
246 self.refresh_style()
246 self.refresh_style()
247
247
248 def refresh_style(self):
248 def refresh_style(self):
249 self._style = self._make_style_from_name_or_cls(self.highlighting_style)
249 self._style = self._make_style_from_name_or_cls(self.highlighting_style)
250
250
251
251
252 highlighting_style_overrides = Dict(
252 highlighting_style_overrides = Dict(
253 help="Override highlighting format for specific tokens"
253 help="Override highlighting format for specific tokens"
254 ).tag(config=True)
254 ).tag(config=True)
255
255
256 true_color = Bool(False,
256 true_color = Bool(False,
257 help="""Use 24bit colors instead of 256 colors in prompt highlighting.
257 help="""Use 24bit colors instead of 256 colors in prompt highlighting.
258 If your terminal supports true color, the following command should
258 If your terminal supports true color, the following command should
259 print ``TRUECOLOR`` in orange::
259 print ``TRUECOLOR`` in orange::
260
260
261 printf \"\\x1b[38;2;255;100;0mTRUECOLOR\\x1b[0m\\n\"
261 printf \"\\x1b[38;2;255;100;0mTRUECOLOR\\x1b[0m\\n\"
262 """,
262 """,
263 ).tag(config=True)
263 ).tag(config=True)
264
264
265 editor = Unicode(get_default_editor(),
265 editor = Unicode(get_default_editor(),
266 help="Set the editor used by IPython (default to $EDITOR/vi/notepad)."
266 help="Set the editor used by IPython (default to $EDITOR/vi/notepad)."
267 ).tag(config=True)
267 ).tag(config=True)
268
268
269 prompts_class = Type(Prompts, help='Class used to generate Prompt token for prompt_toolkit').tag(config=True)
269 prompts_class = Type(Prompts, help='Class used to generate Prompt token for prompt_toolkit').tag(config=True)
270
270
271 prompts = Instance(Prompts)
271 prompts = Instance(Prompts)
272
272
273 @default('prompts')
273 @default('prompts')
274 def _prompts_default(self):
274 def _prompts_default(self):
275 return self.prompts_class(self)
275 return self.prompts_class(self)
276
276
277 # @observe('prompts')
277 # @observe('prompts')
278 # def _(self, change):
278 # def _(self, change):
279 # self._update_layout()
279 # self._update_layout()
280
280
281 @default('displayhook_class')
281 @default('displayhook_class')
282 def _displayhook_class_default(self):
282 def _displayhook_class_default(self):
283 return RichPromptDisplayHook
283 return RichPromptDisplayHook
284
284
285 term_title = Bool(True,
285 term_title = Bool(True,
286 help="Automatically set the terminal title"
286 help="Automatically set the terminal title"
287 ).tag(config=True)
287 ).tag(config=True)
288
288
289 term_title_format = Unicode("IPython: {cwd}",
289 term_title_format = Unicode("IPython: {cwd}",
290 help="Customize the terminal title format. This is a python format string. " +
290 help="Customize the terminal title format. This is a python format string. " +
291 "Available substitutions are: {cwd}."
291 "Available substitutions are: {cwd}."
292 ).tag(config=True)
292 ).tag(config=True)
293
293
294 display_completions = Enum(('column', 'multicolumn','readlinelike'),
294 display_completions = Enum(('column', 'multicolumn','readlinelike'),
295 help= ( "Options for displaying tab completions, 'column', 'multicolumn', and "
295 help= ( "Options for displaying tab completions, 'column', 'multicolumn', and "
296 "'readlinelike'. These options are for `prompt_toolkit`, see "
296 "'readlinelike'. These options are for `prompt_toolkit`, see "
297 "`prompt_toolkit` documentation for more information."
297 "`prompt_toolkit` documentation for more information."
298 ),
298 ),
299 default_value='multicolumn').tag(config=True)
299 default_value='multicolumn').tag(config=True)
300
300
301 highlight_matching_brackets = Bool(True,
301 highlight_matching_brackets = Bool(True,
302 help="Highlight matching brackets.",
302 help="Highlight matching brackets.",
303 ).tag(config=True)
303 ).tag(config=True)
304
304
305 extra_open_editor_shortcuts = Bool(False,
305 extra_open_editor_shortcuts = Bool(False,
306 help="Enable vi (v) or Emacs (C-X C-E) shortcuts to open an external editor. "
306 help="Enable vi (v) or Emacs (C-X C-E) shortcuts to open an external editor. "
307 "This is in addition to the F2 binding, which is always enabled."
307 "This is in addition to the F2 binding, which is always enabled."
308 ).tag(config=True)
308 ).tag(config=True)
309
309
310 handle_return = Any(None,
310 handle_return = Any(None,
311 help="Provide an alternative handler to be called when the user presses "
311 help="Provide an alternative handler to be called when the user presses "
312 "Return. This is an advanced option intended for debugging, which "
312 "Return. This is an advanced option intended for debugging, which "
313 "may be changed or removed in later releases."
313 "may be changed or removed in later releases."
314 ).tag(config=True)
314 ).tag(config=True)
315
315
316 enable_history_search = Bool(True,
316 enable_history_search = Bool(True,
317 help="Allows to enable/disable the prompt toolkit history search"
317 help="Allows to enable/disable the prompt toolkit history search"
318 ).tag(config=True)
318 ).tag(config=True)
319
319
320 enable_autosuggestions = Bool(True,
320 autosuggestions_provider = Unicode(
321 help="Allows to enable/disable the prompt autosuggestions based on "
321 "AutoSuggestFromHistory",
322 "the prompt toolkit history.",
322 help="Specifies from which source automatic suggestions are provided. "
323 "Can be set to `'AutoSuggestFromHistory`' or `None` to disable"
324 "automatic suggestions. Default is `'AutoSuggestFromHistory`'.",
325 allow_none=True,
323 ).tag(config=True)
326 ).tag(config=True)
324
327
328 prompt_includes_vi_mode = Bool(
329 True, help="Display the current vi mode (when using vi editing mode)."
330 ).tag(config=True)
331
332 def _set_autosuggestions(self, provider):
333 if provider is None:
334 self.auto_suggest = None
335 elif provider == "AutoSuggestFromHistory":
336 self.auto_suggest = AutoSuggestFromHistory()
337 else:
338 raise ValueError("No valid provider.")
339 if self.pt_app:
340 self.pt_app.auto_suggest = self.auto_suggest
341
342 @observe("autosuggestions_provider")
343 def _autosuggestions_provider_changed(self, change):
344 provider = change.new
345 self._set_autosuggestions(provider)
346
325 prompt_includes_vi_mode = Bool(True,
347 prompt_includes_vi_mode = Bool(True,
326 help="Display the current vi mode (when using vi editing mode)."
348 help="Display the current vi mode (when using vi editing mode)."
327 ).tag(config=True)
349 ).tag(config=True)
328
350
329 @observe('term_title')
351 @observe('term_title')
330 def init_term_title(self, change=None):
352 def init_term_title(self, change=None):
331 # Enable or disable the terminal title.
353 # Enable or disable the terminal title.
332 if self.term_title:
354 if self.term_title:
333 toggle_set_term_title(True)
355 toggle_set_term_title(True)
334 set_term_title(self.term_title_format.format(cwd=abbrev_cwd()))
356 set_term_title(self.term_title_format.format(cwd=abbrev_cwd()))
335 else:
357 else:
336 toggle_set_term_title(False)
358 toggle_set_term_title(False)
337
359
338 def restore_term_title(self):
360 def restore_term_title(self):
339 if self.term_title:
361 if self.term_title:
340 restore_term_title()
362 restore_term_title()
341
363
342 def init_display_formatter(self):
364 def init_display_formatter(self):
343 super(TerminalInteractiveShell, self).init_display_formatter()
365 super(TerminalInteractiveShell, self).init_display_formatter()
344 # terminal only supports plain text
366 # terminal only supports plain text
345 self.display_formatter.active_types = ["text/plain"]
367 self.display_formatter.active_types = ["text/plain"]
346
368
347 def init_prompt_toolkit_cli(self):
369 def init_prompt_toolkit_cli(self):
348 if self.simple_prompt:
370 if self.simple_prompt:
349 # Fall back to plain non-interactive output for tests.
371 # Fall back to plain non-interactive output for tests.
350 # This is very limited.
372 # This is very limited.
351 def prompt():
373 def prompt():
352 prompt_text = "".join(x[1] for x in self.prompts.in_prompt_tokens())
374 prompt_text = "".join(x[1] for x in self.prompts.in_prompt_tokens())
353 lines = [input(prompt_text)]
375 lines = [input(prompt_text)]
354 prompt_continuation = "".join(x[1] for x in self.prompts.continuation_prompt_tokens())
376 prompt_continuation = "".join(x[1] for x in self.prompts.continuation_prompt_tokens())
355 while self.check_complete('\n'.join(lines))[0] == 'incomplete':
377 while self.check_complete('\n'.join(lines))[0] == 'incomplete':
356 lines.append( input(prompt_continuation) )
378 lines.append( input(prompt_continuation) )
357 return '\n'.join(lines)
379 return '\n'.join(lines)
358 self.prompt_for_code = prompt
380 self.prompt_for_code = prompt
359 return
381 return
360
382
361 # Set up keyboard shortcuts
383 # Set up keyboard shortcuts
362 key_bindings = create_ipython_shortcuts(self)
384 key_bindings = create_ipython_shortcuts(self)
363
385
364 # Pre-populate history from IPython's history database
386 # Pre-populate history from IPython's history database
365 history = InMemoryHistory()
387 history = InMemoryHistory()
366 last_cell = u""
388 last_cell = u""
367 for __, ___, cell in self.history_manager.get_tail(self.history_load_length,
389 for __, ___, cell in self.history_manager.get_tail(self.history_load_length,
368 include_latest=True):
390 include_latest=True):
369 # Ignore blank lines and consecutive duplicates
391 # Ignore blank lines and consecutive duplicates
370 cell = cell.rstrip()
392 cell = cell.rstrip()
371 if cell and (cell != last_cell):
393 if cell and (cell != last_cell):
372 history.append_string(cell)
394 history.append_string(cell)
373 last_cell = cell
395 last_cell = cell
374
396
375 self._style = self._make_style_from_name_or_cls(self.highlighting_style)
397 self._style = self._make_style_from_name_or_cls(self.highlighting_style)
376 self.style = DynamicStyle(lambda: self._style)
398 self.style = DynamicStyle(lambda: self._style)
377
399
378 if self.enable_autosuggestions:
379 auto_suggest = AutoSuggestFromHistory()
380 else:
381 auto_suggest = None
382
383 editing_mode = getattr(EditingMode, self.editing_mode.upper())
400 editing_mode = getattr(EditingMode, self.editing_mode.upper())
384
401
385 self.pt_loop = asyncio.new_event_loop()
402 self.pt_loop = asyncio.new_event_loop()
386 self.pt_app = PromptSession(
403 self.pt_app = PromptSession(
387 auto_suggest=auto_suggest,
404 auto_suggest=self.auto_suggest,
388 editing_mode=editing_mode,
405 editing_mode=editing_mode,
389 key_bindings=key_bindings,
406 key_bindings=key_bindings,
390 history=history,
407 history=history,
391 completer=IPythonPTCompleter(shell=self),
408 completer=IPythonPTCompleter(shell=self),
392 enable_history_search=self.enable_history_search,
409 enable_history_search=self.enable_history_search,
393 style=self.style,
410 style=self.style,
394 include_default_pygments_style=False,
411 include_default_pygments_style=False,
395 mouse_support=self.mouse_support,
412 mouse_support=self.mouse_support,
396 enable_open_in_editor=self.extra_open_editor_shortcuts,
413 enable_open_in_editor=self.extra_open_editor_shortcuts,
397 color_depth=self.color_depth,
414 color_depth=self.color_depth,
398 tempfile_suffix=".py",
415 tempfile_suffix=".py",
399 **self._extra_prompt_options()
416 **self._extra_prompt_options()
400 )
417 )
401
418
402 def _make_style_from_name_or_cls(self, name_or_cls):
419 def _make_style_from_name_or_cls(self, name_or_cls):
403 """
420 """
404 Small wrapper that make an IPython compatible style from a style name
421 Small wrapper that make an IPython compatible style from a style name
405
422
406 We need that to add style for prompt ... etc.
423 We need that to add style for prompt ... etc.
407 """
424 """
408 style_overrides = {}
425 style_overrides = {}
409 if name_or_cls == 'legacy':
426 if name_or_cls == 'legacy':
410 legacy = self.colors.lower()
427 legacy = self.colors.lower()
411 if legacy == 'linux':
428 if legacy == 'linux':
412 style_cls = get_style_by_name('monokai')
429 style_cls = get_style_by_name('monokai')
413 style_overrides = _style_overrides_linux
430 style_overrides = _style_overrides_linux
414 elif legacy == 'lightbg':
431 elif legacy == 'lightbg':
415 style_overrides = _style_overrides_light_bg
432 style_overrides = _style_overrides_light_bg
416 style_cls = get_style_by_name('pastie')
433 style_cls = get_style_by_name('pastie')
417 elif legacy == 'neutral':
434 elif legacy == 'neutral':
418 # The default theme needs to be visible on both a dark background
435 # The default theme needs to be visible on both a dark background
419 # and a light background, because we can't tell what the terminal
436 # and a light background, because we can't tell what the terminal
420 # looks like. These tweaks to the default theme help with that.
437 # looks like. These tweaks to the default theme help with that.
421 style_cls = get_style_by_name('default')
438 style_cls = get_style_by_name('default')
422 style_overrides.update({
439 style_overrides.update({
423 Token.Number: '#ansigreen',
440 Token.Number: '#ansigreen',
424 Token.Operator: 'noinherit',
441 Token.Operator: 'noinherit',
425 Token.String: '#ansiyellow',
442 Token.String: '#ansiyellow',
426 Token.Name.Function: '#ansiblue',
443 Token.Name.Function: '#ansiblue',
427 Token.Name.Class: 'bold #ansiblue',
444 Token.Name.Class: 'bold #ansiblue',
428 Token.Name.Namespace: 'bold #ansiblue',
445 Token.Name.Namespace: 'bold #ansiblue',
429 Token.Name.Variable.Magic: '#ansiblue',
446 Token.Name.Variable.Magic: '#ansiblue',
430 Token.Prompt: '#ansigreen',
447 Token.Prompt: '#ansigreen',
431 Token.PromptNum: '#ansibrightgreen bold',
448 Token.PromptNum: '#ansibrightgreen bold',
432 Token.OutPrompt: '#ansired',
449 Token.OutPrompt: '#ansired',
433 Token.OutPromptNum: '#ansibrightred bold',
450 Token.OutPromptNum: '#ansibrightred bold',
434 })
451 })
435
452
436 # Hack: Due to limited color support on the Windows console
453 # Hack: Due to limited color support on the Windows console
437 # the prompt colors will be wrong without this
454 # the prompt colors will be wrong without this
438 if os.name == 'nt':
455 if os.name == 'nt':
439 style_overrides.update({
456 style_overrides.update({
440 Token.Prompt: '#ansidarkgreen',
457 Token.Prompt: '#ansidarkgreen',
441 Token.PromptNum: '#ansigreen bold',
458 Token.PromptNum: '#ansigreen bold',
442 Token.OutPrompt: '#ansidarkred',
459 Token.OutPrompt: '#ansidarkred',
443 Token.OutPromptNum: '#ansired bold',
460 Token.OutPromptNum: '#ansired bold',
444 })
461 })
445 elif legacy =='nocolor':
462 elif legacy =='nocolor':
446 style_cls=_NoStyle
463 style_cls=_NoStyle
447 style_overrides = {}
464 style_overrides = {}
448 else :
465 else :
449 raise ValueError('Got unknown colors: ', legacy)
466 raise ValueError('Got unknown colors: ', legacy)
450 else :
467 else :
451 if isinstance(name_or_cls, str):
468 if isinstance(name_or_cls, str):
452 style_cls = get_style_by_name(name_or_cls)
469 style_cls = get_style_by_name(name_or_cls)
453 else:
470 else:
454 style_cls = name_or_cls
471 style_cls = name_or_cls
455 style_overrides = {
472 style_overrides = {
456 Token.Prompt: '#ansigreen',
473 Token.Prompt: '#ansigreen',
457 Token.PromptNum: '#ansibrightgreen bold',
474 Token.PromptNum: '#ansibrightgreen bold',
458 Token.OutPrompt: '#ansired',
475 Token.OutPrompt: '#ansired',
459 Token.OutPromptNum: '#ansibrightred bold',
476 Token.OutPromptNum: '#ansibrightred bold',
460 }
477 }
461 style_overrides.update(self.highlighting_style_overrides)
478 style_overrides.update(self.highlighting_style_overrides)
462 style = merge_styles([
479 style = merge_styles([
463 style_from_pygments_cls(style_cls),
480 style_from_pygments_cls(style_cls),
464 style_from_pygments_dict(style_overrides),
481 style_from_pygments_dict(style_overrides),
465 ])
482 ])
466
483
467 return style
484 return style
468
485
469 @property
486 @property
470 def pt_complete_style(self):
487 def pt_complete_style(self):
471 return {
488 return {
472 'multicolumn': CompleteStyle.MULTI_COLUMN,
489 'multicolumn': CompleteStyle.MULTI_COLUMN,
473 'column': CompleteStyle.COLUMN,
490 'column': CompleteStyle.COLUMN,
474 'readlinelike': CompleteStyle.READLINE_LIKE,
491 'readlinelike': CompleteStyle.READLINE_LIKE,
475 }[self.display_completions]
492 }[self.display_completions]
476
493
477 @property
494 @property
478 def color_depth(self):
495 def color_depth(self):
479 return (ColorDepth.TRUE_COLOR if self.true_color else None)
496 return (ColorDepth.TRUE_COLOR if self.true_color else None)
480
497
481 def _extra_prompt_options(self):
498 def _extra_prompt_options(self):
482 """
499 """
483 Return the current layout option for the current Terminal InteractiveShell
500 Return the current layout option for the current Terminal InteractiveShell
484 """
501 """
485 def get_message():
502 def get_message():
486 return PygmentsTokens(self.prompts.in_prompt_tokens())
503 return PygmentsTokens(self.prompts.in_prompt_tokens())
487
504
488 if self.editing_mode == 'emacs':
505 if self.editing_mode == 'emacs':
489 # with emacs mode the prompt is (usually) static, so we call only
506 # with emacs mode the prompt is (usually) static, so we call only
490 # the function once. With VI mode it can toggle between [ins] and
507 # the function once. With VI mode it can toggle between [ins] and
491 # [nor] so we can't precompute.
508 # [nor] so we can't precompute.
492 # here I'm going to favor the default keybinding which almost
509 # here I'm going to favor the default keybinding which almost
493 # everybody uses to decrease CPU usage.
510 # everybody uses to decrease CPU usage.
494 # if we have issues with users with custom Prompts we can see how to
511 # if we have issues with users with custom Prompts we can see how to
495 # work around this.
512 # work around this.
496 get_message = get_message()
513 get_message = get_message()
497
514
498 options = {
515 options = {
499 'complete_in_thread': False,
516 'complete_in_thread': False,
500 'lexer':IPythonPTLexer(),
517 'lexer':IPythonPTLexer(),
501 'reserve_space_for_menu':self.space_for_menu,
518 'reserve_space_for_menu':self.space_for_menu,
502 'message': get_message,
519 'message': get_message,
503 'prompt_continuation': (
520 'prompt_continuation': (
504 lambda width, lineno, is_soft_wrap:
521 lambda width, lineno, is_soft_wrap:
505 PygmentsTokens(self.prompts.continuation_prompt_tokens(width))),
522 PygmentsTokens(self.prompts.continuation_prompt_tokens(width))),
506 'multiline': True,
523 'multiline': True,
507 'complete_style': self.pt_complete_style,
524 'complete_style': self.pt_complete_style,
508
525
509 # Highlight matching brackets, but only when this setting is
526 # Highlight matching brackets, but only when this setting is
510 # enabled, and only when the DEFAULT_BUFFER has the focus.
527 # enabled, and only when the DEFAULT_BUFFER has the focus.
511 'input_processors': [ConditionalProcessor(
528 'input_processors': [ConditionalProcessor(
512 processor=HighlightMatchingBracketProcessor(chars='[](){}'),
529 processor=HighlightMatchingBracketProcessor(chars='[](){}'),
513 filter=HasFocus(DEFAULT_BUFFER) & ~IsDone() &
530 filter=HasFocus(DEFAULT_BUFFER) & ~IsDone() &
514 Condition(lambda: self.highlight_matching_brackets))],
531 Condition(lambda: self.highlight_matching_brackets))],
515 }
532 }
516 if not PTK3:
533 if not PTK3:
517 options['inputhook'] = self.inputhook
534 options['inputhook'] = self.inputhook
518
535
519 return options
536 return options
520
537
521 def prompt_for_code(self):
538 def prompt_for_code(self):
522 if self.rl_next_input:
539 if self.rl_next_input:
523 default = self.rl_next_input
540 default = self.rl_next_input
524 self.rl_next_input = None
541 self.rl_next_input = None
525 else:
542 else:
526 default = ''
543 default = ''
527
544
528 # In order to make sure that asyncio code written in the
545 # In order to make sure that asyncio code written in the
529 # interactive shell doesn't interfere with the prompt, we run the
546 # interactive shell doesn't interfere with the prompt, we run the
530 # prompt in a different event loop.
547 # prompt in a different event loop.
531 # If we don't do this, people could spawn coroutine with a
548 # If we don't do this, people could spawn coroutine with a
532 # while/true inside which will freeze the prompt.
549 # while/true inside which will freeze the prompt.
533
550
534 policy = asyncio.get_event_loop_policy()
551 policy = asyncio.get_event_loop_policy()
535 old_loop = get_asyncio_loop()
552 old_loop = get_asyncio_loop()
536
553
537 # FIXME: prompt_toolkit is using the deprecated `asyncio.get_event_loop`
554 # FIXME: prompt_toolkit is using the deprecated `asyncio.get_event_loop`
538 # to get the current event loop.
555 # to get the current event loop.
539 # This will probably be replaced by an attribute or input argument,
556 # This will probably be replaced by an attribute or input argument,
540 # at which point we can stop calling the soon-to-be-deprecated `set_event_loop` here.
557 # at which point we can stop calling the soon-to-be-deprecated `set_event_loop` here.
541 if old_loop is not self.pt_loop:
558 if old_loop is not self.pt_loop:
542 policy.set_event_loop(self.pt_loop)
559 policy.set_event_loop(self.pt_loop)
543 try:
560 try:
544 with patch_stdout(raw=True):
561 with patch_stdout(raw=True):
545 text = self.pt_app.prompt(
562 text = self.pt_app.prompt(
546 default=default,
563 default=default,
547 **self._extra_prompt_options())
564 **self._extra_prompt_options())
548 finally:
565 finally:
549 # Restore the original event loop.
566 # Restore the original event loop.
550 if old_loop is not None and old_loop is not self.pt_loop:
567 if old_loop is not None and old_loop is not self.pt_loop:
551 policy.set_event_loop(old_loop)
568 policy.set_event_loop(old_loop)
552
569
553 return text
570 return text
554
571
555 def enable_win_unicode_console(self):
572 def enable_win_unicode_console(self):
556 # Since IPython 7.10 doesn't support python < 3.6 and PEP 528, Python uses the unicode APIs for the Windows
573 # Since IPython 7.10 doesn't support python < 3.6 and PEP 528, Python uses the unicode APIs for the Windows
557 # console by default, so WUC shouldn't be needed.
574 # console by default, so WUC shouldn't be needed.
558 from warnings import warn
575 from warnings import warn
559 warn("`enable_win_unicode_console` is deprecated since IPython 7.10, does not do anything and will be removed in the future",
576 warn("`enable_win_unicode_console` is deprecated since IPython 7.10, does not do anything and will be removed in the future",
560 DeprecationWarning,
577 DeprecationWarning,
561 stacklevel=2)
578 stacklevel=2)
562
579
563 def init_io(self):
580 def init_io(self):
564 if sys.platform not in {'win32', 'cli'}:
581 if sys.platform not in {'win32', 'cli'}:
565 return
582 return
566
583
567 import colorama
584 import colorama
568 colorama.init()
585 colorama.init()
569
586
570 def init_magics(self):
587 def init_magics(self):
571 super(TerminalInteractiveShell, self).init_magics()
588 super(TerminalInteractiveShell, self).init_magics()
572 self.register_magics(TerminalMagics)
589 self.register_magics(TerminalMagics)
573
590
574 def init_alias(self):
591 def init_alias(self):
575 # The parent class defines aliases that can be safely used with any
592 # The parent class defines aliases that can be safely used with any
576 # frontend.
593 # frontend.
577 super(TerminalInteractiveShell, self).init_alias()
594 super(TerminalInteractiveShell, self).init_alias()
578
595
579 # Now define aliases that only make sense on the terminal, because they
596 # Now define aliases that only make sense on the terminal, because they
580 # need direct access to the console in a way that we can't emulate in
597 # need direct access to the console in a way that we can't emulate in
581 # GUI or web frontend
598 # GUI or web frontend
582 if os.name == 'posix':
599 if os.name == 'posix':
583 for cmd in ('clear', 'more', 'less', 'man'):
600 for cmd in ('clear', 'more', 'less', 'man'):
584 self.alias_manager.soft_define_alias(cmd, cmd)
601 self.alias_manager.soft_define_alias(cmd, cmd)
585
602
586
603
587 def __init__(self, *args, **kwargs):
604 def __init__(self, *args, **kwargs):
588 super(TerminalInteractiveShell, self).__init__(*args, **kwargs)
605 super(TerminalInteractiveShell, self).__init__(*args, **kwargs)
606 self._set_autosuggestions(self.autosuggestions_provider)
589 self.init_prompt_toolkit_cli()
607 self.init_prompt_toolkit_cli()
590 self.init_term_title()
608 self.init_term_title()
591 self.keep_running = True
609 self.keep_running = True
592 self._set_formatter(self.autoformatter)
610 self._set_formatter(self.autoformatter)
593
611
594
612
595 def ask_exit(self):
613 def ask_exit(self):
596 self.keep_running = False
614 self.keep_running = False
597
615
598 rl_next_input = None
616 rl_next_input = None
599
617
600 def interact(self):
618 def interact(self):
601 self.keep_running = True
619 self.keep_running = True
602 while self.keep_running:
620 while self.keep_running:
603 print(self.separate_in, end='')
621 print(self.separate_in, end='')
604
622
605 try:
623 try:
606 code = self.prompt_for_code()
624 code = self.prompt_for_code()
607 except EOFError:
625 except EOFError:
608 if (not self.confirm_exit) \
626 if (not self.confirm_exit) \
609 or self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'):
627 or self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'):
610 self.ask_exit()
628 self.ask_exit()
611
629
612 else:
630 else:
613 if code:
631 if code:
614 self.run_cell(code, store_history=True)
632 self.run_cell(code, store_history=True)
615
633
616 def mainloop(self):
634 def mainloop(self):
617 # An extra layer of protection in case someone mashing Ctrl-C breaks
635 # An extra layer of protection in case someone mashing Ctrl-C breaks
618 # out of our internal code.
636 # out of our internal code.
619 while True:
637 while True:
620 try:
638 try:
621 self.interact()
639 self.interact()
622 break
640 break
623 except KeyboardInterrupt as e:
641 except KeyboardInterrupt as e:
624 print("\n%s escaped interact()\n" % type(e).__name__)
642 print("\n%s escaped interact()\n" % type(e).__name__)
625 finally:
643 finally:
626 # An interrupt during the eventloop will mess up the
644 # An interrupt during the eventloop will mess up the
627 # internal state of the prompt_toolkit library.
645 # internal state of the prompt_toolkit library.
628 # Stopping the eventloop fixes this, see
646 # Stopping the eventloop fixes this, see
629 # https://github.com/ipython/ipython/pull/9867
647 # https://github.com/ipython/ipython/pull/9867
630 if hasattr(self, '_eventloop'):
648 if hasattr(self, '_eventloop'):
631 self._eventloop.stop()
649 self._eventloop.stop()
632
650
633 self.restore_term_title()
651 self.restore_term_title()
634
652
635 # try to call some at-exit operation optimistically as some things can't
653 # try to call some at-exit operation optimistically as some things can't
636 # be done during interpreter shutdown. this is technically inaccurate as
654 # be done during interpreter shutdown. this is technically inaccurate as
637 # this make mainlool not re-callable, but that should be a rare if not
655 # this make mainlool not re-callable, but that should be a rare if not
638 # in existent use case.
656 # in existent use case.
639
657
640 self._atexit_once()
658 self._atexit_once()
641
659
642
660
643 _inputhook = None
661 _inputhook = None
644 def inputhook(self, context):
662 def inputhook(self, context):
645 if self._inputhook is not None:
663 if self._inputhook is not None:
646 self._inputhook(context)
664 self._inputhook(context)
647
665
648 active_eventloop = None
666 active_eventloop = None
649 def enable_gui(self, gui=None):
667 def enable_gui(self, gui=None):
650 if gui and (gui != 'inline') :
668 if gui and (gui != 'inline') :
651 self.active_eventloop, self._inputhook =\
669 self.active_eventloop, self._inputhook =\
652 get_inputhook_name_and_func(gui)
670 get_inputhook_name_and_func(gui)
653 else:
671 else:
654 self.active_eventloop = self._inputhook = None
672 self.active_eventloop = self._inputhook = None
655
673
656 # For prompt_toolkit 3.0. We have to create an asyncio event loop with
674 # For prompt_toolkit 3.0. We have to create an asyncio event loop with
657 # this inputhook.
675 # this inputhook.
658 if PTK3:
676 if PTK3:
659 import asyncio
677 import asyncio
660 from prompt_toolkit.eventloop import new_eventloop_with_inputhook
678 from prompt_toolkit.eventloop import new_eventloop_with_inputhook
661
679
662 if gui == 'asyncio':
680 if gui == 'asyncio':
663 # When we integrate the asyncio event loop, run the UI in the
681 # When we integrate the asyncio event loop, run the UI in the
664 # same event loop as the rest of the code. don't use an actual
682 # same event loop as the rest of the code. don't use an actual
665 # input hook. (Asyncio is not made for nesting event loops.)
683 # input hook. (Asyncio is not made for nesting event loops.)
666 self.pt_loop = get_asyncio_loop()
684 self.pt_loop = get_asyncio_loop()
667
685
668 elif self._inputhook:
686 elif self._inputhook:
669 # If an inputhook was set, create a new asyncio event loop with
687 # If an inputhook was set, create a new asyncio event loop with
670 # this inputhook for the prompt.
688 # this inputhook for the prompt.
671 self.pt_loop = new_eventloop_with_inputhook(self._inputhook)
689 self.pt_loop = new_eventloop_with_inputhook(self._inputhook)
672 else:
690 else:
673 # When there's no inputhook, run the prompt in a separate
691 # When there's no inputhook, run the prompt in a separate
674 # asyncio event loop.
692 # asyncio event loop.
675 self.pt_loop = asyncio.new_event_loop()
693 self.pt_loop = asyncio.new_event_loop()
676
694
677 # Run !system commands directly, not through pipes, so terminal programs
695 # Run !system commands directly, not through pipes, so terminal programs
678 # work correctly.
696 # work correctly.
679 system = InteractiveShell.system_raw
697 system = InteractiveShell.system_raw
680
698
681 def auto_rewrite_input(self, cmd):
699 def auto_rewrite_input(self, cmd):
682 """Overridden from the parent class to use fancy rewriting prompt"""
700 """Overridden from the parent class to use fancy rewriting prompt"""
683 if not self.show_rewritten_input:
701 if not self.show_rewritten_input:
684 return
702 return
685
703
686 tokens = self.prompts.rewrite_prompt_tokens()
704 tokens = self.prompts.rewrite_prompt_tokens()
687 if self.pt_app:
705 if self.pt_app:
688 print_formatted_text(PygmentsTokens(tokens), end='',
706 print_formatted_text(PygmentsTokens(tokens), end='',
689 style=self.pt_app.app.style)
707 style=self.pt_app.app.style)
690 print(cmd)
708 print(cmd)
691 else:
709 else:
692 prompt = ''.join(s for t, s in tokens)
710 prompt = ''.join(s for t, s in tokens)
693 print(prompt, cmd, sep='')
711 print(prompt, cmd, sep='')
694
712
695 _prompts_before = None
713 _prompts_before = None
696 def switch_doctest_mode(self, mode):
714 def switch_doctest_mode(self, mode):
697 """Switch prompts to classic for %doctest_mode"""
715 """Switch prompts to classic for %doctest_mode"""
698 if mode:
716 if mode:
699 self._prompts_before = self.prompts
717 self._prompts_before = self.prompts
700 self.prompts = ClassicPrompts(self)
718 self.prompts = ClassicPrompts(self)
701 elif self._prompts_before:
719 elif self._prompts_before:
702 self.prompts = self._prompts_before
720 self.prompts = self._prompts_before
703 self._prompts_before = None
721 self._prompts_before = None
704 # self._update_layout()
722 # self._update_layout()
705
723
706
724
707 InteractiveShellABC.register(TerminalInteractiveShell)
725 InteractiveShellABC.register(TerminalInteractiveShell)
708
726
709 if __name__ == '__main__':
727 if __name__ == '__main__':
710 TerminalInteractiveShell.instance().interact()
728 TerminalInteractiveShell.instance().interact()
General Comments 0
You need to be logged in to leave comments. Login now