##// END OF EJS Templates
Support vi-mode 'InputFormat' prompt...
Jonathan Feinberg -
Show More
@@ -1,103 +1,108 b''
1 """Terminal input and output prompts."""
1 """Terminal input and output prompts."""
2
2
3 from pygments.token import Token
3 from pygments.token import Token
4 import sys
4 import sys
5
5
6 from IPython.core.displayhook import DisplayHook
6 from IPython.core.displayhook import DisplayHook
7
7
8 from prompt_toolkit.formatted_text import fragment_list_width, PygmentsTokens
8 from prompt_toolkit.formatted_text import fragment_list_width, PygmentsTokens
9 from prompt_toolkit.shortcuts import print_formatted_text
9 from prompt_toolkit.shortcuts import print_formatted_text
10 from prompt_toolkit.enums import EditingMode
10 from prompt_toolkit.enums import EditingMode
11
11
12
12
13 class Prompts(object):
13 class Prompts(object):
14 def __init__(self, shell):
14 def __init__(self, shell):
15 self.shell = shell
15 self.shell = shell
16
16
17 def vi_mode(self):
17 def vi_mode(self):
18 if (getattr(self.shell.pt_app, 'editing_mode', None) == EditingMode.VI
18 if (getattr(self.shell.pt_app, 'editing_mode', None) == EditingMode.VI
19 and self.shell.prompt_includes_vi_mode):
19 and self.shell.prompt_includes_vi_mode):
20 return '['+str(self.shell.pt_app.app.vi_state.input_mode)[3:6]+'] '
20 mode = str(self.shell.pt_app.app.vi_state.input_mode)
21 if mode.startswith('InputMode.'):
22 mode = mode[10:13].lower()
23 elif mode.startswith('vi-'):
24 mode = mode[3:6]
25 return '['+mode+'] '
21 return ''
26 return ''
22
27
23
28
24 def in_prompt_tokens(self):
29 def in_prompt_tokens(self):
25 return [
30 return [
26 (Token.Prompt, self.vi_mode() ),
31 (Token.Prompt, self.vi_mode() ),
27 (Token.Prompt, 'In ['),
32 (Token.Prompt, 'In ['),
28 (Token.PromptNum, str(self.shell.execution_count)),
33 (Token.PromptNum, str(self.shell.execution_count)),
29 (Token.Prompt, ']: '),
34 (Token.Prompt, ']: '),
30 ]
35 ]
31
36
32 def _width(self):
37 def _width(self):
33 return fragment_list_width(self.in_prompt_tokens())
38 return fragment_list_width(self.in_prompt_tokens())
34
39
35 def continuation_prompt_tokens(self, width=None):
40 def continuation_prompt_tokens(self, width=None):
36 if width is None:
41 if width is None:
37 width = self._width()
42 width = self._width()
38 return [
43 return [
39 (Token.Prompt, (' ' * (width - 5)) + '...: '),
44 (Token.Prompt, (' ' * (width - 5)) + '...: '),
40 ]
45 ]
41
46
42 def rewrite_prompt_tokens(self):
47 def rewrite_prompt_tokens(self):
43 width = self._width()
48 width = self._width()
44 return [
49 return [
45 (Token.Prompt, ('-' * (width - 2)) + '> '),
50 (Token.Prompt, ('-' * (width - 2)) + '> '),
46 ]
51 ]
47
52
48 def out_prompt_tokens(self):
53 def out_prompt_tokens(self):
49 return [
54 return [
50 (Token.OutPrompt, 'Out['),
55 (Token.OutPrompt, 'Out['),
51 (Token.OutPromptNum, str(self.shell.execution_count)),
56 (Token.OutPromptNum, str(self.shell.execution_count)),
52 (Token.OutPrompt, ']: '),
57 (Token.OutPrompt, ']: '),
53 ]
58 ]
54
59
55 class ClassicPrompts(Prompts):
60 class ClassicPrompts(Prompts):
56 def in_prompt_tokens(self):
61 def in_prompt_tokens(self):
57 return [
62 return [
58 (Token.Prompt, '>>> '),
63 (Token.Prompt, '>>> '),
59 ]
64 ]
60
65
61 def continuation_prompt_tokens(self, width=None):
66 def continuation_prompt_tokens(self, width=None):
62 return [
67 return [
63 (Token.Prompt, '... ')
68 (Token.Prompt, '... ')
64 ]
69 ]
65
70
66 def rewrite_prompt_tokens(self):
71 def rewrite_prompt_tokens(self):
67 return []
72 return []
68
73
69 def out_prompt_tokens(self):
74 def out_prompt_tokens(self):
70 return []
75 return []
71
76
72 class RichPromptDisplayHook(DisplayHook):
77 class RichPromptDisplayHook(DisplayHook):
73 """Subclass of base display hook using coloured prompt"""
78 """Subclass of base display hook using coloured prompt"""
74 def write_output_prompt(self):
79 def write_output_prompt(self):
75 sys.stdout.write(self.shell.separate_out)
80 sys.stdout.write(self.shell.separate_out)
76 # If we're not displaying a prompt, it effectively ends with a newline,
81 # If we're not displaying a prompt, it effectively ends with a newline,
77 # because the output will be left-aligned.
82 # because the output will be left-aligned.
78 self.prompt_end_newline = True
83 self.prompt_end_newline = True
79
84
80 if self.do_full_cache:
85 if self.do_full_cache:
81 tokens = self.shell.prompts.out_prompt_tokens()
86 tokens = self.shell.prompts.out_prompt_tokens()
82 prompt_txt = ''.join(s for t, s in tokens)
87 prompt_txt = ''.join(s for t, s in tokens)
83 if prompt_txt and not prompt_txt.endswith('\n'):
88 if prompt_txt and not prompt_txt.endswith('\n'):
84 # Ask for a newline before multiline output
89 # Ask for a newline before multiline output
85 self.prompt_end_newline = False
90 self.prompt_end_newline = False
86
91
87 if self.shell.pt_app:
92 if self.shell.pt_app:
88 print_formatted_text(PygmentsTokens(tokens),
93 print_formatted_text(PygmentsTokens(tokens),
89 style=self.shell.pt_app.app.style, end='',
94 style=self.shell.pt_app.app.style, end='',
90 )
95 )
91 else:
96 else:
92 sys.stdout.write(prompt_txt)
97 sys.stdout.write(prompt_txt)
93
98
94 def write_format_data(self, format_dict, md_dict=None) -> None:
99 def write_format_data(self, format_dict, md_dict=None) -> None:
95 if self.shell.mime_renderers:
100 if self.shell.mime_renderers:
96
101
97 for mime, handler in self.shell.mime_renderers.items():
102 for mime, handler in self.shell.mime_renderers.items():
98 if mime in format_dict:
103 if mime in format_dict:
99 handler(format_dict[mime], None)
104 handler(format_dict[mime], None)
100 return
105 return
101
106
102 super().write_format_data(format_dict, md_dict)
107 super().write_format_data(format_dict, md_dict)
103
108
General Comments 0
You need to be logged in to leave comments. Login now