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