##// END OF EJS Templates
Inspect continuation prompt signature and pass only viable arguments. (#14274)...
M Bussonnier -
r28567:59eccb24 merge
parent child Browse files
Show More
@@ -2,6 +2,7 b''
2
2
3 import os
3 import os
4 import sys
4 import sys
5 import inspect
5 from warnings import warn
6 from warnings import warn
6 from typing import Union as UnionType, Optional
7 from typing import Union as UnionType, Optional
7
8
@@ -65,8 +66,8 b' from .shortcuts.auto_suggest import ('
65 PTK3 = ptk_version.startswith('3.')
66 PTK3 = ptk_version.startswith('3.')
66
67
67
68
68 class _NoStyle(Style): pass
69 class _NoStyle(Style):
69
70 pass
70
71
71
72
72 _style_overrides_light_bg = {
73 _style_overrides_light_bg = {
@@ -83,6 +84,20 b' _style_overrides_linux = {'
83 Token.OutPromptNum: '#ansired bold',
84 Token.OutPromptNum: '#ansired bold',
84 }
85 }
85
86
87
88 def _backward_compat_continuation_prompt_tokens(method, width: int, *, lineno: int):
89 """
90 Sagemath use custom prompt and we broke them in 8.19.
91 """
92 sig = inspect.signature(method)
93 if "lineno" in inspect.signature(method).parameters or any(
94 [p.kind == p.VAR_KEYWORD for p in sig.parameters.values()]
95 ):
96 return method(width, lineno=lineno)
97 else:
98 return method(width)
99
100
86 def get_default_editor():
101 def get_default_editor():
87 try:
102 try:
88 return os.environ['EDITOR']
103 return os.environ['EDITOR']
@@ -762,7 +777,9 b' class TerminalInteractiveShell(InteractiveShell):'
762 "message": get_message,
777 "message": get_message,
763 "prompt_continuation": (
778 "prompt_continuation": (
764 lambda width, lineno, is_soft_wrap: PygmentsTokens(
779 lambda width, lineno, is_soft_wrap: PygmentsTokens(
765 self.prompts.continuation_prompt_tokens(width, lineno=lineno)
780 _backward_compat_continuation_prompt_tokens(
781 self.prompts.continuation_prompt_tokens, width, lineno=lineno
782 )
766 )
783 )
767 ),
784 ),
768 "multiline": True,
785 "multiline": True,
@@ -33,8 +33,8 b' which defines the defaults. The required interface is like this:'
33 Prompt style definition. *shell* is a reference to the
33 Prompt style definition. *shell* is a reference to the
34 :class:`~.TerminalInteractiveShell` instance.
34 :class:`~.TerminalInteractiveShell` instance.
35
35
36 .. method:: in_prompt_tokens(cli=None)
36 .. method:: in_prompt_tokens()
37 continuation_prompt_tokens(self, cli=None, width=None)
37 continuation_prompt_tokens(self, width=None)
38 rewrite_prompt_tokens()
38 rewrite_prompt_tokens()
39 out_prompt_tokens()
39 out_prompt_tokens()
40
40
@@ -43,8 +43,6 b' which defines the defaults. The required interface is like this:'
43 For continuation prompts, *width* is an integer representing the width of
43 For continuation prompts, *width* is an integer representing the width of
44 the prompt area in terminal columns.
44 the prompt area in terminal columns.
45
45
46 *cli*, where used, is the prompt_toolkit ``CommandLineInterface`` instance.
47 This is mainly for compatibility with the API prompt_toolkit expects.
48
46
49 Here is an example Prompt class that will show the current working directory
47 Here is an example Prompt class that will show the current working directory
50 in the input prompt:
48 in the input prompt:
@@ -55,7 +53,7 b' in the input prompt:'
55 import os
53 import os
56
54
57 class MyPrompt(Prompts):
55 class MyPrompt(Prompts):
58 def in_prompt_tokens(self, cli=None):
56 def in_prompt_tokens(self):
59 return [(Token, os.getcwd()),
57 return [(Token, os.getcwd()),
60 (Token.Prompt, ' >>>')]
58 (Token.Prompt, ' >>>')]
61
59
@@ -18,10 +18,11 b' The code in this file is deliberately extra-verbose, meant for learning."""'
18 # %run example-embed.py)
18 # %run example-embed.py)
19
19
20 from IPython.terminal.prompts import Prompts, Token
20 from IPython.terminal.prompts import Prompts, Token
21 from traitlets.config.loader import Config
21
22
22 class CustomPrompt(Prompts):
23 class CustomPrompt(Prompts):
23
24
24 def in_prompt_tokens(self, cli=None):
25 def in_prompt_tokens(self):
25
26
26 return [
27 return [
27 (Token.Prompt, 'In <'),
28 (Token.Prompt, 'In <'),
@@ -37,7 +38,6 b' class CustomPrompt(Prompts):'
37 ]
38 ]
38
39
39
40
40 from traitlets.config.loader import Config
41 try:
41 try:
42 get_ipython
42 get_ipython
43 except NameError:
43 except NameError:
@@ -6,9 +6,9 b' import os'
6
6
7 class MyPrompt(Prompts):
7 class MyPrompt(Prompts):
8
8
9 def in_prompt_tokens(self, cli=None):
9 def in_prompt_tokens(self):
10 return [(Token, os.getcwd()),
10 return [(Token, os.getcwd()), (Token.Prompt, ">>>")]
11 (Token.Prompt, '>>>')]
11
12
12
13 def load_ipython_extension(shell):
13 def load_ipython_extension(shell):
14 new_prompts = MyPrompt(shell)
14 new_prompts = MyPrompt(shell)
@@ -20,7 +20,3 b' def unload_ipython_extension(shell):'
20 print("cannot unload")
20 print("cannot unload")
21 else:
21 else:
22 shell.prompts = shell.prompts.old_prompts
22 shell.prompts = shell.prompts.old_prompts
23
24
25
26
General Comments 0
You need to be logged in to leave comments. Login now