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