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