Skip to content

Commit 447f995

Browse files
Fixed ptipython.
1 parent 7895a42 commit 447f995

File tree

2 files changed

+25
-68
lines changed

2 files changed

+25
-68
lines changed

ptpython/ipython.py

Lines changed: 23 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
from __future__ import unicode_literals, print_function
1212

1313
from prompt_toolkit.completion import Completion, Completer
14-
from prompt_toolkit.contrib.completers import PathCompleter, WordCompleter, SystemCompleter
14+
from prompt_toolkit.completion import PathCompleter, WordCompleter
15+
from prompt_toolkit.contrib.completers import SystemCompleter
1516
from prompt_toolkit.contrib.regular_languages.compiler import compile
1617
from prompt_toolkit.contrib.regular_languages.completion import GrammarCompleter
1718
from prompt_toolkit.contrib.regular_languages.lexer import GrammarLexer
1819
from prompt_toolkit.document import Document
19-
from prompt_toolkit.interface import CommandLineInterface
20-
from prompt_toolkit.layout.lexers import PygmentsLexer, SimpleLexer
20+
from prompt_toolkit.formatted_text import PygmentsTokens
21+
from prompt_toolkit.lexers import PygmentsLexer, SimpleLexer
22+
from prompt_toolkit.styles import Style
2123

2224
from .python_input import PythonInput, PythonValidator, PythonCompleter
2325
from .style import default_ui_style
@@ -36,40 +38,17 @@
3638

3739

3840
class IPythonPrompt(PromptStyle):
39-
"""
40-
PromptStyle that uses the templates, as set by IPython.
41-
Usually, something like "In [1]:".
42-
"""
43-
def __init__(self, prompt_manager):
44-
self.prompt_manager = prompt_manager
45-
46-
def in_prompt(self):
47-
text = self.prompt_manager.render('in', color=False, just=False)
48-
return [('class:in', text)]
49-
50-
def in2_prompt(self, width):
51-
text = self.prompt_manager.render('in2', color=False, just=False)
52-
return [('class:in', text.rjust(width))]
53-
54-
def out_tokens(self):
55-
# This function is currently not used by IPython. But for completeness,
56-
# it would look like this.
57-
text = self.prompt_manager.render('out', color=False, just=False)
58-
return [('class:out', text)]
59-
60-
61-
class IPython5Prompt(PromptStyle):
6241
"""
6342
Style for IPython >5.0, use the prompt_toolkit tokens directly.
6443
"""
6544
def __init__(self, prompts):
6645
self.prompts = prompts
6746

6847
def in_prompt(self):
69-
return self.prompts.in_prompt_tokens()
48+
return PygmentsTokens(self.prompts.in_prompt_tokens())
7049

7150
def in2_prompt(self, width):
72-
return self.prompts.continuation_prompt_tokens()
51+
return PygmentsTokens(self.prompts.continuation_prompt_tokens())
7352

7453
def out_prompt(self):
7554
return []
@@ -183,29 +162,21 @@ def __init__(self, ipython_shell, *a, **kw):
183162
super(IPythonInput, self).__init__(*a, **kw)
184163
self.ipython_shell = ipython_shell
185164

186-
# Prompt for IPython < 5.0
187-
if hasattr(ipython_shell, 'prompt_manager'):
188-
self.all_prompt_styles['ipython'] = IPythonPrompt(ipython_shell.prompt_manager)
189-
self.prompt_style = 'ipython'
190-
191-
# Prompt for IPython >=5.0:
192-
if hasattr(ipython_shell, 'prompts'):
193-
self.all_prompt_styles['ipython'] = IPython5Prompt(ipython_shell.prompts)
194-
self.prompt_style = 'ipython'
195-
165+
self.all_prompt_styles['ipython'] = IPythonPrompt(ipython_shell.prompts)
166+
self.prompt_style = 'ipython'
196167

197168
# UI style for IPython. Add tokens that are used by IPython>5.0
198169
style_dict = {}
199170
style_dict.update(default_ui_style)
200171
style_dict.update({
201-
'prompt': '#009900',
202-
'prompt-num': '#00ff00 bold',
203-
'out-prompt': '#990000',
204-
'out-prompt-num': '#ff0000 bold',
172+
'pygments.prompt': '#009900',
173+
'pygments.prompt-num': '#00ff00 bold',
174+
'pygments.out-prompt': '#990000',
175+
'pygments.out-prompt-num': '#ff0000 bold',
205176
})
206177

207178
self.ui_styles = {
208-
'default': style_dict,
179+
'default': Style.from_dict(style_dict),
209180
}
210181
self.use_ui_colorscheme('default')
211182

@@ -223,44 +194,30 @@ def __init__(self, *a, **kw):
223194
configure = kw.pop('configure', None)
224195
title = kw.pop('title', None)
225196

197+
# Don't ask IPython to confirm for exit. We have our own exit prompt.
198+
self.confirm_exit = False
199+
226200
super(InteractiveShellEmbed, self).__init__(*a, **kw)
227201

228202
def get_globals():
229203
return self.user_ns
230204

231-
ipython_input = IPythonInput(
205+
python_input = IPythonInput(
232206
self,
233207
get_globals=get_globals, vi_mode=vi_mode,
234208
history_filename=history_filename)
235209

236210
if title:
237-
ipython_input.terminal_title = title
211+
python_input.terminal_title = title
238212

239213
if configure:
240-
configure(ipython_input)
241-
ipython_input.prompt_style = 'ipython' # Don't take from config.
214+
configure(python_input)
215+
python_input.prompt_style = 'ipython' # Don't take from config.
242216

243-
self._cli = CommandLineInterface(
244-
application=ipython_input.create_application())
217+
self.python_input = python_input
245218

246219
def prompt_for_code(self):
247-
# IPython 5.0 calls `prompt_for_code` instead of `raw_input`.
248-
return self.raw_input(self)
249-
250-
def raw_input(self, prompt=''):
251-
print('')
252-
try:
253-
string = self._cli.run(reset_current_buffer=True).text
254-
255-
# In case of multiline input, make sure to append a newline to the input,
256-
# otherwise, IPython will ask again for more input in some cases.
257-
if '\n' in string:
258-
return string + '\n\n'
259-
else:
260-
return string
261-
except EOFError:
262-
self.ask_exit()
263-
return ''
220+
return self.python_input.app.run()
264221

265222

266223
def initialize_extensions(shell, extensions):

ptpython/layout.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from prompt_toolkit.application import get_app
77
from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER
88
from prompt_toolkit.filters import is_done, has_completions, renderer_height_is_known, has_focus, Condition
9-
from prompt_toolkit.formatted_text.utils import fragment_list_width
9+
from prompt_toolkit.formatted_text import fragment_list_width, to_formatted_text
1010
from prompt_toolkit.key_binding.vi_state import InputMode
1111
from prompt_toolkit.layout.containers import Window, HSplit, VSplit, FloatContainer, Float, ConditionalContainer, ScrollOffsets
1212
from prompt_toolkit.layout.controls import BufferControl, FormattedTextControl
@@ -275,7 +275,7 @@ def get_prompt_style():
275275
return python_input.all_prompt_styles[python_input.prompt_style]
276276

277277
def get_prompt():
278-
return get_prompt_style().in_prompt()
278+
return to_formatted_text(get_prompt_style().in_prompt())
279279

280280
def get_continuation(width, line_number, is_soft_wrap):
281281
if python_input.show_line_numbers and not is_soft_wrap:

0 commit comments

Comments
 (0)