|
21 | 21 |
|
22 | 22 | from .python_input import PythonInput, PythonValidator, PythonCompleter
|
23 | 23 | from .eventloop import create_eventloop
|
| 24 | +from .style import default_ui_style |
24 | 25 |
|
25 | 26 | from IPython.terminal.embed import InteractiveShellEmbed as _InteractiveShellEmbed
|
26 | 27 | from IPython.terminal.ipapp import load_default_config
|
@@ -59,6 +60,23 @@ def out_tokens(self, cli):
|
59 | 60 | return [(Token.Out, text)]
|
60 | 61 |
|
61 | 62 |
|
| 63 | +class IPython5Prompt(PromptStyle): |
| 64 | + """ |
| 65 | + Style for IPython >5.0, use the prompt_toolkit tokens directly. |
| 66 | + """ |
| 67 | + def __init__(self, prompts): |
| 68 | + self.prompts = prompts |
| 69 | + |
| 70 | + def in_tokens(self, cli): |
| 71 | + return self.prompts.in_prompt_tokens(cli) |
| 72 | + |
| 73 | + def in2_tokens(self, cli, width): |
| 74 | + return self.prompts.continuation_prompt_tokens(cli) |
| 75 | + |
| 76 | + def out_tokens(self, cli): |
| 77 | + return [] |
| 78 | + |
| 79 | + |
62 | 80 | class IPythonValidator(PythonValidator):
|
63 | 81 | def __init__(self, *args, **kwargs):
|
64 | 82 | super(IPythonValidator, self).__init__(*args, **kwargs)
|
@@ -167,8 +185,31 @@ def __init__(self, ipython_shell, *a, **kw):
|
167 | 185 | super(IPythonInput, self).__init__(*a, **kw)
|
168 | 186 | self.ipython_shell = ipython_shell
|
169 | 187 |
|
170 |
| - self.all_prompt_styles['ipython'] = IPythonPrompt(ipython_shell.prompt_manager) |
171 |
| - self.prompt_style = 'ipython' |
| 188 | + # Prompt for IPython < 5.0 |
| 189 | + if hasattr(ipython_shell, 'prompt_manager'): |
| 190 | + self.all_prompt_styles['ipython'] = IPythonPrompt(ipython_shell.prompt_manager) |
| 191 | + self.prompt_style = 'ipython' |
| 192 | + |
| 193 | + # Prompt for IPython >=5.0: |
| 194 | + if hasattr(ipython_shell, 'prompts'): |
| 195 | + self.all_prompt_styles['ipython'] = IPython5Prompt(ipython_shell.prompts) |
| 196 | + self.prompt_style = 'ipython' |
| 197 | + |
| 198 | + |
| 199 | + # UI style for IPython. Add tokens that are used by IPython>5.0 |
| 200 | + style_dict = {} |
| 201 | + style_dict.update(default_ui_style) |
| 202 | + style_dict.update({ |
| 203 | + Token.Prompt: '#009900', |
| 204 | + Token.PromptNum: '#00ff00 bold', |
| 205 | + Token.OutPrompt: '#990000', |
| 206 | + Token.OutPromptNum: '#ff0000 bold', |
| 207 | + }) |
| 208 | + |
| 209 | + self.ui_styles = { |
| 210 | + 'default': style_dict, |
| 211 | + } |
| 212 | + self.use_ui_colorscheme('default') |
172 | 213 |
|
173 | 214 |
|
174 | 215 | class InteractiveShellEmbed(_InteractiveShellEmbed):
|
@@ -200,11 +241,16 @@ def get_globals():
|
200 | 241 |
|
201 | 242 | if configure:
|
202 | 243 | configure(ipython_input)
|
| 244 | + ipython_input.prompt_style = 'ipython' # Don't take from config. |
203 | 245 |
|
204 | 246 | self._cli = CommandLineInterface(
|
205 | 247 | application=ipython_input.create_application(),
|
206 | 248 | eventloop=self._eventloop)
|
207 | 249 |
|
| 250 | + def prompt_for_code(self): |
| 251 | + # IPython 5.0 calls `prompt_for_code` instead of `raw_input`. |
| 252 | + return self.raw_input(self) |
| 253 | + |
208 | 254 | def raw_input(self, prompt=''):
|
209 | 255 | print('')
|
210 | 256 | try:
|
|
0 commit comments