Show More
@@ -0,0 +1,36 b'' | |||||
|
1 | from pygments.token import Token | |||
|
2 | ||||
|
3 | class Prompts(object): | |||
|
4 | def __init__(self, shell): | |||
|
5 | self.shell = shell | |||
|
6 | ||||
|
7 | def in_prompt_tokens(self, cli=None): | |||
|
8 | return [ | |||
|
9 | (Token.Prompt, 'In ['), | |||
|
10 | (Token.PromptNum, str(self.shell.execution_count)), | |||
|
11 | (Token.Prompt, ']: '), | |||
|
12 | ] | |||
|
13 | ||||
|
14 | def _width(self): | |||
|
15 | in_tokens = self.in_prompt_tokens() | |||
|
16 | return sum(len(s) for (t, s) in in_tokens) | |||
|
17 | ||||
|
18 | def continuation_prompt_tokens(self, cli=None, width=None): | |||
|
19 | if width is None: | |||
|
20 | width = self._width() | |||
|
21 | return [ | |||
|
22 | (Token.Prompt, (' ' * (width - 5)) + '...: '), | |||
|
23 | ] | |||
|
24 | ||||
|
25 | def rewrite_prompt_tokens(self): | |||
|
26 | width = self._width() | |||
|
27 | return [ | |||
|
28 | (Token.Prompt, ('-' * (width - 2)) + '> '), | |||
|
29 | ] | |||
|
30 | ||||
|
31 | def out_prompt_tokens(self): | |||
|
32 | return [ | |||
|
33 | (Token.OutPrompt, 'Out['), | |||
|
34 | (Token.OutPromptNum, str(self.shell.execution_count)), | |||
|
35 | (Token.OutPrompt, ']: '), | |||
|
36 | ] |
@@ -2339,16 +2339,9 b' class InteractiveShell(SingletonConfigurable):' | |||||
2339 | """ |
|
2339 | """ | |
2340 | if not self.show_rewritten_input: |
|
2340 | if not self.show_rewritten_input: | |
2341 | return |
|
2341 | return | |
2342 |
|
||||
2343 | rw = self.prompt_manager.render('rewrite') + cmd |
|
|||
2344 |
|
2342 | |||
2345 | try: |
|
2343 | # This is overridden in TerminalInteractiveShell to use fancy prompts | |
2346 | # plain ascii works better w/ pyreadline, on some machines, so |
|
2344 | print("------> " + cmd) | |
2347 | # we use it and only print uncolored rewrite if we have unicode |
|
|||
2348 | rw = str(rw) |
|
|||
2349 | print(rw) |
|
|||
2350 | except UnicodeEncodeError: |
|
|||
2351 | print("------> " + cmd) |
|
|||
2352 |
|
2345 | |||
2353 | #------------------------------------------------------------------------- |
|
2346 | #------------------------------------------------------------------------- | |
2354 | # Things related to extracting values/expressions from kernel and user_ns |
|
2347 | # Things related to extracting values/expressions from kernel and user_ns |
@@ -11,7 +11,7 b' from IPython.core.interactiveshell import InteractiveShell' | |||||
11 | from IPython.utils.py3compat import cast_unicode_py2, input |
|
11 | from IPython.utils.py3compat import cast_unicode_py2, input | |
12 | from IPython.utils.terminal import toggle_set_term_title, set_term_title |
|
12 | from IPython.utils.terminal import toggle_set_term_title, set_term_title | |
13 | from IPython.utils.process import abbrev_cwd |
|
13 | from IPython.utils.process import abbrev_cwd | |
14 | from traitlets import Bool, Unicode, Dict, Integer, observe |
|
14 | from traitlets import Bool, Unicode, Dict, Integer, observe, Instance | |
15 |
|
15 | |||
16 | from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER, EditingMode |
|
16 | from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER, EditingMode | |
17 | from prompt_toolkit.filters import HasFocus, HasSelection, Condition, ViInsertMode, EmacsInsertMode, IsDone |
|
17 | from prompt_toolkit.filters import HasFocus, HasSelection, Condition, ViInsertMode, EmacsInsertMode, IsDone | |
@@ -29,6 +29,7 b' from pygments.token import Token' | |||||
29 | from .debugger import TerminalPdb, Pdb |
|
29 | from .debugger import TerminalPdb, Pdb | |
30 | from .pt_inputhooks import get_inputhook_func |
|
30 | from .pt_inputhooks import get_inputhook_func | |
31 | from .interactiveshell import get_default_editor, TerminalMagics |
|
31 | from .interactiveshell import get_default_editor, TerminalMagics | |
|
32 | from .prompts import Prompts | |||
32 | from .ptutils import IPythonPTCompleter, IPythonPTLexer |
|
33 | from .ptutils import IPythonPTCompleter, IPythonPTLexer | |
33 |
|
34 | |||
34 | _use_simple_prompt = 'IPY_TEST_SIMPLE_PROMPT' in os.environ or not sys.stdin.isatty() |
|
35 | _use_simple_prompt = 'IPY_TEST_SIMPLE_PROMPT' in os.environ or not sys.stdin.isatty() | |
@@ -98,6 +99,11 b' class TerminalInteractiveShell(InteractiveShell):' | |||||
98 | editor = Unicode(get_default_editor(), |
|
99 | editor = Unicode(get_default_editor(), | |
99 | help="Set the editor used by IPython (default to $EDITOR/vi/notepad)." |
|
100 | help="Set the editor used by IPython (default to $EDITOR/vi/notepad)." | |
100 | ).tag(config=True) |
|
101 | ).tag(config=True) | |
|
102 | ||||
|
103 | prompts = Instance(Prompts) | |||
|
104 | ||||
|
105 | def _prompts_default(self): | |||
|
106 | return Prompts(self) | |||
101 |
|
107 | |||
102 | term_title = Bool(True, |
|
108 | term_title = Bool(True, | |
103 | help="Automatically set the terminal title" |
|
109 | help="Automatically set the terminal title" | |
@@ -120,18 +126,6 b' class TerminalInteractiveShell(InteractiveShell):' | |||||
120 | else: |
|
126 | else: | |
121 | toggle_set_term_title(False) |
|
127 | toggle_set_term_title(False) | |
122 |
|
128 | |||
123 | def get_prompt_tokens(self, cli): |
|
|||
124 | return [ |
|
|||
125 | (Token.Prompt, 'In ['), |
|
|||
126 | (Token.PromptNum, str(self.execution_count)), |
|
|||
127 | (Token.Prompt, ']: '), |
|
|||
128 | ] |
|
|||
129 |
|
||||
130 | def get_continuation_tokens(self, cli, width): |
|
|||
131 | return [ |
|
|||
132 | (Token.Prompt, (' ' * (width - 5)) + '...: '), |
|
|||
133 | ] |
|
|||
134 |
|
||||
135 | def init_prompt_toolkit_cli(self): |
|
129 | def init_prompt_toolkit_cli(self): | |
136 | if self.simple_prompt: |
|
130 | if self.simple_prompt: | |
137 | # Fall back to plain non-interactive output for tests. |
|
131 | # Fall back to plain non-interactive output for tests. | |
@@ -261,8 +255,8 b' class TerminalInteractiveShell(InteractiveShell):' | |||||
261 | return { |
|
255 | return { | |
262 | 'lexer':IPythonPTLexer(), |
|
256 | 'lexer':IPythonPTLexer(), | |
263 | 'reserve_space_for_menu':self.space_for_menu, |
|
257 | 'reserve_space_for_menu':self.space_for_menu, | |
264 |
'get_prompt_tokens':self. |
|
258 | 'get_prompt_tokens':self.prompts.in_prompt_tokens, | |
265 |
'get_continuation_tokens':self. |
|
259 | 'get_continuation_tokens':self.prompts.continuation_prompt_tokens, | |
266 | 'multiline':True, |
|
260 | 'multiline':True, | |
267 | 'display_completions_in_columns': self.display_completions_in_columns, |
|
261 | 'display_completions_in_columns': self.display_completions_in_columns, | |
268 |
|
262 | |||
@@ -439,6 +433,19 b' class TerminalInteractiveShell(InteractiveShell):' | |||||
439 | # work correctly. |
|
433 | # work correctly. | |
440 | system = InteractiveShell.system_raw |
|
434 | system = InteractiveShell.system_raw | |
441 |
|
435 | |||
|
436 | def auto_rewrite_input(self, cmd): | |||
|
437 | """Overridden from the parent class to use fancy rewriting prompt""" | |||
|
438 | if not self.show_rewritten_input: | |||
|
439 | return | |||
|
440 | ||||
|
441 | tokens = self.prompts.rewrite_prompt_tokens() | |||
|
442 | if self.pt_cli: | |||
|
443 | self.pt_cli.print_tokens(tokens) | |||
|
444 | print(cmd) | |||
|
445 | else: | |||
|
446 | prompt = ''.join(s for t, s in tokens) | |||
|
447 | print(prompt, cmd, sep='') | |||
|
448 | ||||
442 |
|
449 | |||
443 | if __name__ == '__main__': |
|
450 | if __name__ == '__main__': | |
444 | TerminalInteractiveShell.instance().interact() |
|
451 | TerminalInteractiveShell.instance().interact() |
General Comments 0
You need to be logged in to leave comments.
Login now