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