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