Show More
@@ -1,73 +1,74 b'' | |||
|
1 | 1 | """Terminal input and output prompts.""" |
|
2 | 2 | from __future__ import print_function |
|
3 | 3 | |
|
4 | 4 | from pygments.token import Token |
|
5 | 5 | import sys |
|
6 | 6 | |
|
7 | 7 | from IPython.core.displayhook import DisplayHook |
|
8 | 8 | |
|
9 | from prompt_toolkit.layout.utils import token_list_width | |
|
10 | ||
|
9 | 11 | class Prompts(object): |
|
10 | 12 | def __init__(self, shell): |
|
11 | 13 | self.shell = shell |
|
12 | 14 | |
|
13 | 15 | def in_prompt_tokens(self, cli=None): |
|
14 | 16 | return [ |
|
15 | 17 | (Token.Prompt, 'In ['), |
|
16 | 18 | (Token.PromptNum, str(self.shell.execution_count)), |
|
17 | 19 | (Token.Prompt, ']: '), |
|
18 | 20 | ] |
|
19 | 21 | |
|
20 | 22 | def _width(self): |
|
21 |
|
|
|
22 | return sum(len(s) for (t, s) in in_tokens) | |
|
23 | return token_list_width(self.in_prompt_tokens()) | |
|
23 | 24 | |
|
24 | 25 | def continuation_prompt_tokens(self, cli=None, width=None): |
|
25 | 26 | if width is None: |
|
26 | 27 | width = self._width() |
|
27 | 28 | return [ |
|
28 | 29 | (Token.Prompt, (' ' * (width - 5)) + '...: '), |
|
29 | 30 | ] |
|
30 | 31 | |
|
31 | 32 | def rewrite_prompt_tokens(self): |
|
32 | 33 | width = self._width() |
|
33 | 34 | return [ |
|
34 | 35 | (Token.Prompt, ('-' * (width - 2)) + '> '), |
|
35 | 36 | ] |
|
36 | 37 | |
|
37 | 38 | def out_prompt_tokens(self): |
|
38 | 39 | return [ |
|
39 | 40 | (Token.OutPrompt, 'Out['), |
|
40 | 41 | (Token.OutPromptNum, str(self.shell.execution_count)), |
|
41 | 42 | (Token.OutPrompt, ']: '), |
|
42 | 43 | ] |
|
43 | 44 | |
|
44 | 45 | class ClassicPrompts(Prompts): |
|
45 | 46 | def in_prompt_tokens(self, cli=None): |
|
46 | 47 | return [ |
|
47 | 48 | (Token.Prompt, '>>> '), |
|
48 | 49 | ] |
|
49 | 50 | |
|
50 | 51 | def continuation_prompt_tokens(self, cli=None, width=None): |
|
51 | 52 | return [ |
|
52 | 53 | (Token.Prompt, '... ') |
|
53 | 54 | ] |
|
54 | 55 | |
|
55 | 56 | def rewrite_prompt_tokens(self): |
|
56 | 57 | return [] |
|
57 | 58 | |
|
58 | 59 | def out_prompt_tokens(self): |
|
59 | 60 | return [] |
|
60 | 61 | |
|
61 | 62 | class RichPromptDisplayHook(DisplayHook): |
|
62 | 63 | """Subclass of base display hook using coloured prompt""" |
|
63 | 64 | def write_output_prompt(self): |
|
64 | 65 | sys.stdout.write(self.shell.separate_out) |
|
65 | 66 | self.prompt_end_newline = False |
|
66 | 67 | if self.do_full_cache: |
|
67 | 68 | tokens = self.shell.prompts.out_prompt_tokens() |
|
68 | 69 | if tokens and tokens[-1][1].endswith('\n'): |
|
69 | 70 | self.prompt_end_newline = True |
|
70 | 71 | if self.shell.pt_cli: |
|
71 | 72 | self.shell.pt_cli.print_tokens(tokens) |
|
72 | 73 | else: |
|
73 | 74 | print(*(s for t, s in tokens), sep='') |
General Comments 0
You need to be logged in to leave comments.
Login now