##// END OF EJS Templates
Switch prompts for doctest_mode
Thomas Kluyver -
Show More
@@ -3219,6 +3219,11 b' class InteractiveShell(SingletonConfigurable):'
3219 3219 self.restore_sys_module_state()
3220 3220
3221 3221
3222 # Overridden in terminal subclass to change prompts
3223 def switch_doctest_mode(self, mode):
3224 pass
3225
3226
3222 3227 class InteractiveShellABC(with_metaclass(abc.ABCMeta, object)):
3223 3228 """An abstract base class for InteractiveShell."""
3224 3229
@@ -469,8 +469,12 b' Defaulting color scheme to \'NoColor\'"""'
469 469
470 470 shell.magic('xmode ' + dstore.xmode)
471 471
472 # mode here is the state before we switch; switch_doctest_mode takes
473 # the mode we're switching to.
474 shell.switch_doctest_mode(not mode)
475
472 476 # Store new mode and inform
473 dstore.mode = bool(1-int(mode))
477 dstore.mode = bool(not mode)
474 478 mode_label = ['OFF','ON'][dstore.mode]
475 479 print('Doctest mode is:', mode_label)
476 480
@@ -41,6 +41,23 b' class Prompts(object):'
41 41 (Token.OutPrompt, ']: '),
42 42 ]
43 43
44 class ClassicPrompts(Prompts):
45 def in_prompt_tokens(self, cli=None):
46 return [
47 (Token.Prompt, '>>> '),
48 ]
49
50 def continuation_prompt_tokens(self, cli=None, width=None):
51 return [
52 (Token.Prompt, '... ')
53 ]
54
55 def rewrite_prompt_tokens(self):
56 return []
57
58 def out_prompt_tokens(self):
59 return []
60
44 61 class RichPromptDisplayHook(DisplayHook):
45 62 """Subclass of base display hook using coloured prompt"""
46 63 def write_output_prompt(self):
@@ -29,7 +29,7 b' from pygments.token import Token'
29 29 from .debugger import TerminalPdb, Pdb
30 30 from .pt_inputhooks import get_inputhook_func
31 31 from .interactiveshell import get_default_editor, TerminalMagics
32 from .prompts import Prompts, RichPromptDisplayHook
32 from .prompts import Prompts, ClassicPrompts, RichPromptDisplayHook
33 33 from .ptutils import IPythonPTCompleter, IPythonPTLexer
34 34
35 35 _use_simple_prompt = 'IPY_TEST_SIMPLE_PROMPT' in os.environ or not sys.stdin.isatty()
@@ -105,6 +105,10 b' class TerminalInteractiveShell(InteractiveShell):'
105 105 def _prompts_default(self):
106 106 return Prompts(self)
107 107
108 @observe('prompts')
109 def _(self, change):
110 self._update_layout()
111
108 112 def _displayhook_class_default(self):
109 113 return RichPromptDisplayHook
110 114
@@ -451,6 +455,16 b' class TerminalInteractiveShell(InteractiveShell):'
451 455 prompt = ''.join(s for t, s in tokens)
452 456 print(prompt, cmd, sep='')
453 457
458 _prompts_before = None
459 def switch_doctest_mode(self, mode):
460 """Switch prompts to classic for %doctest_mode"""
461 if mode:
462 self._prompts_before = self.prompts
463 self.prompts = ClassicPrompts(self)
464 elif self._prompts_before:
465 self.prompts = self._prompts_before
466 self._prompts_before = None
467
454 468
455 469 if __name__ == '__main__':
456 470 TerminalInteractiveShell.instance().interact()
General Comments 0
You need to be logged in to leave comments. Login now