Show More
@@ -0,0 +1,64 b'' | |||||
|
1 | from IPython.core.interactiveshell import InteractiveShell | |||
|
2 | ||||
|
3 | from prompt_toolkit.buffer import Buffer | |||
|
4 | from prompt_toolkit.shortcuts import create_prompt_layout | |||
|
5 | from prompt_toolkit.filters import Condition | |||
|
6 | from prompt_toolkit.interface import AcceptAction, Application, CommandLineInterface | |||
|
7 | from prompt_toolkit.layout.lexers import PygmentsLexer | |||
|
8 | ||||
|
9 | from pygments.lexers import Python3Lexer | |||
|
10 | from pygments.token import Token | |||
|
11 | ||||
|
12 | ||||
|
13 | class PTInteractiveShell(InteractiveShell): | |||
|
14 | def _multiline(self, cli): | |||
|
15 | doc = cli.current_buffer.document | |||
|
16 | if not doc.on_last_line: | |||
|
17 | cli.run_in_terminal(lambda: print('Not on last line')) | |||
|
18 | return False | |||
|
19 | status, indent = self.input_splitter.check_complete(doc.text) | |||
|
20 | return status == 'incomplete' | |||
|
21 | ||||
|
22 | def _multiline2(self): | |||
|
23 | return self._multiline(self.pt_cli) | |||
|
24 | ||||
|
25 | pt_cli = None | |||
|
26 | ||||
|
27 | def get_prompt_tokens(self, cli): | |||
|
28 | return [ | |||
|
29 | (Token.Prompt, 'In ['), | |||
|
30 | (Token.Prompt, str(self.execution_count)), | |||
|
31 | (Token.Prompt, ']: '), | |||
|
32 | ] | |||
|
33 | ||||
|
34 | ||||
|
35 | def init_prompt_toolkit_cli(self): | |||
|
36 | layout = create_prompt_layout( | |||
|
37 | get_prompt_tokens=self.get_prompt_tokens, | |||
|
38 | lexer=PygmentsLexer(Python3Lexer), | |||
|
39 | multiline=Condition(self._multiline), | |||
|
40 | ) | |||
|
41 | buffer = Buffer( | |||
|
42 | is_multiline=Condition(self._multiline2), | |||
|
43 | accept_action=AcceptAction.RETURN_DOCUMENT, | |||
|
44 | ) | |||
|
45 | app = Application(layout=layout, buffer=buffer) | |||
|
46 | self.pt_cli = CommandLineInterface(app) | |||
|
47 | ||||
|
48 | def __init__(self, *args, **kwargs): | |||
|
49 | super(PTInteractiveShell, self).__init__(*args, **kwargs) | |||
|
50 | self.init_prompt_toolkit_cli() | |||
|
51 | self.keep_running = True | |||
|
52 | ||||
|
53 | def ask_exit(self): | |||
|
54 | self.keep_running = False | |||
|
55 | ||||
|
56 | def interact(self): | |||
|
57 | while self.keep_running: | |||
|
58 | document = self.pt_cli.run() | |||
|
59 | if document: | |||
|
60 | self.run_cell(document.text) | |||
|
61 | ||||
|
62 | ||||
|
63 | if __name__ == '__main__': | |||
|
64 | PTInteractiveShell().interact() |
General Comments 0
You need to be logged in to leave comments.
Login now