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