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