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