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