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