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