Show More
@@ -1,90 +1,92 | |||
|
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 | |
|
11 | 11 | |
|
12 | 12 | class Prompts(object): |
|
13 | 13 | def __init__(self, shell): |
|
14 | 14 | self.shell = shell |
|
15 | 15 | |
|
16 | 16 | def vi_mode(self): |
|
17 | if not hasattr(self.shell.pt_app, 'editing_mode'): | |
|
18 | return '' | |
|
17 | 19 | if self.shell.pt_app.editing_mode == 'VI': |
|
18 | 20 | return '['+str(self.shell.pt_app.app.vi_state.input_mode)[3:6]+'] ' |
|
19 | 21 | return '' |
|
20 | 22 | |
|
21 | 23 | |
|
22 | 24 | def in_prompt_tokens(self): |
|
23 | 25 | return [ |
|
24 | 26 | (Token.Prompt, self.vi_mode() ), |
|
25 | 27 | (Token.Prompt, 'In ['), |
|
26 | 28 | (Token.PromptNum, str(self.shell.execution_count)), |
|
27 | 29 | (Token.Prompt, ']: '), |
|
28 | 30 | ] |
|
29 | 31 | |
|
30 | 32 | def _width(self): |
|
31 | 33 | return fragment_list_width(self.in_prompt_tokens()) |
|
32 | 34 | |
|
33 | 35 | def continuation_prompt_tokens(self, width=None): |
|
34 | 36 | if width is None: |
|
35 | 37 | width = self._width() |
|
36 | 38 | return [ |
|
37 | 39 | (Token.Prompt, (' ' * (width - 5)) + '...: '), |
|
38 | 40 | ] |
|
39 | 41 | |
|
40 | 42 | def rewrite_prompt_tokens(self): |
|
41 | 43 | width = self._width() |
|
42 | 44 | return [ |
|
43 | 45 | (Token.Prompt, ('-' * (width - 2)) + '> '), |
|
44 | 46 | ] |
|
45 | 47 | |
|
46 | 48 | def out_prompt_tokens(self): |
|
47 | 49 | return [ |
|
48 | 50 | (Token.OutPrompt, 'Out['), |
|
49 | 51 | (Token.OutPromptNum, str(self.shell.execution_count)), |
|
50 | 52 | (Token.OutPrompt, ']: '), |
|
51 | 53 | ] |
|
52 | 54 | |
|
53 | 55 | class ClassicPrompts(Prompts): |
|
54 | 56 | def in_prompt_tokens(self): |
|
55 | 57 | return [ |
|
56 | 58 | (Token.Prompt, '>>> '), |
|
57 | 59 | ] |
|
58 | 60 | |
|
59 | 61 | def continuation_prompt_tokens(self, width=None): |
|
60 | 62 | return [ |
|
61 | 63 | (Token.Prompt, '... ') |
|
62 | 64 | ] |
|
63 | 65 | |
|
64 | 66 | def rewrite_prompt_tokens(self): |
|
65 | 67 | return [] |
|
66 | 68 | |
|
67 | 69 | def out_prompt_tokens(self): |
|
68 | 70 | return [] |
|
69 | 71 | |
|
70 | 72 | class RichPromptDisplayHook(DisplayHook): |
|
71 | 73 | """Subclass of base display hook using coloured prompt""" |
|
72 | 74 | def write_output_prompt(self): |
|
73 | 75 | sys.stdout.write(self.shell.separate_out) |
|
74 | 76 | # If we're not displaying a prompt, it effectively ends with a newline, |
|
75 | 77 | # because the output will be left-aligned. |
|
76 | 78 | self.prompt_end_newline = True |
|
77 | 79 | |
|
78 | 80 | if self.do_full_cache: |
|
79 | 81 | tokens = self.shell.prompts.out_prompt_tokens() |
|
80 | 82 | prompt_txt = ''.join(s for t, s in tokens) |
|
81 | 83 | if prompt_txt and not prompt_txt.endswith('\n'): |
|
82 | 84 | # Ask for a newline before multiline output |
|
83 | 85 | self.prompt_end_newline = False |
|
84 | 86 | |
|
85 | 87 | if self.shell.pt_app: |
|
86 | 88 | print_formatted_text(PygmentsTokens(tokens), |
|
87 | 89 | style=self.shell.pt_app.app.style, end='', |
|
88 | 90 | ) |
|
89 | 91 | else: |
|
90 | 92 | sys.stdout.write(prompt_txt) |
General Comments 0
You need to be logged in to leave comments.
Login now