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