##// END OF EJS Templates
Do sensible things on Ctrl-C and Ctrl-D
Thomas Kluyver -
Show More
@@ -1,76 +1,86 b''
1 1 from IPython.core.interactiveshell import InteractiveShell
2 2
3 3 from prompt_toolkit.history import InMemoryHistory
4 4 from prompt_toolkit.shortcuts import create_prompt_application
5 5 from prompt_toolkit.interface import CommandLineInterface
6 6 from prompt_toolkit.key_binding.manager import KeyBindingManager
7 7 from prompt_toolkit.keys import Keys
8 8 from prompt_toolkit.layout.lexers import PygmentsLexer
9 9
10 10 from pygments.lexers import Python3Lexer
11 11 from pygments.token import Token
12 12
13 13
14 14 class PTInteractiveShell(InteractiveShell):
15 15 pt_cli = None
16 16
17 17 def get_prompt_tokens(self, cli):
18 18 return [
19 19 (Token.Prompt, 'In ['),
20 20 (Token.Prompt, str(self.execution_count)),
21 21 (Token.Prompt, ']: '),
22 22 ]
23 23
24 24
25 25 def init_prompt_toolkit_cli(self):
26 26 kbmanager = KeyBindingManager.for_prompt()
27 27 @kbmanager.registry.add_binding(Keys.ControlJ) # Ctrl+J == Enter, seemingly
28 28 def _(event):
29 29 b = event.current_buffer
30 30 if not b.document.on_last_line:
31 31 b.newline()
32 32 return
33 33
34 34 status, indent = self.input_splitter.check_complete(b.document.text)
35 35
36 36 if (status != 'incomplete') and b.accept_action.is_returnable:
37 37 b.accept_action.validate_and_handle(event.cli, b)
38 38 else:
39 39 b.insert_text('\n' + (' ' * (indent or 0)))
40 40
41 @kbmanager.registry.add_binding(Keys.ControlC)
42 def _(event):
43 event.current_buffer.reset()
44
41 45 # Pre-populate history from IPython's history database
42 46 history = InMemoryHistory()
43 47 last_cell = u""
44 48 for _, _, cell in self.history_manager.get_tail(self.history_load_length,
45 49 include_latest=True):
46 50 # Ignore blank lines and consecutive duplicates
47 51 cell = cell.rstrip()
48 52 if cell and (cell != last_cell):
49 53 history.append(cell)
50 54
51 55 app = create_prompt_application(multiline=True,
52 56 lexer=PygmentsLexer(Python3Lexer),
53 57 get_prompt_tokens=self.get_prompt_tokens,
54 58 key_bindings_registry=kbmanager.registry,
55 59 history=history,
56 60 )
57 61
58 62 self.pt_cli = CommandLineInterface(app)
59 63
60 64 def __init__(self, *args, **kwargs):
61 65 super(PTInteractiveShell, self).__init__(*args, **kwargs)
62 66 self.init_prompt_toolkit_cli()
63 67 self.keep_running = True
64 68
65 69 def ask_exit(self):
66 70 self.keep_running = False
67 71
68 72 def interact(self):
69 73 while self.keep_running:
70 document = self.pt_cli.run()
71 if document:
72 self.run_cell(document.text, store_history=True)
74 try:
75 document = self.pt_cli.run()
76 except EOFError:
77 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'):
78 self.ask_exit()
79
80 else:
81 if document:
82 self.run_cell(document.text, store_history=True)
73 83
74 84
75 85 if __name__ == '__main__':
76 86 PTInteractiveShell().interact()
General Comments 0
You need to be logged in to leave comments. Login now