Skip to content

Commit f9bca69

Browse files
Use PromptMargin from prompt_toolkit.
1 parent bb849fc commit f9bca69

File tree

1 file changed

+11
-29
lines changed

1 file changed

+11
-29
lines changed

ptpython/layout.py

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from prompt_toolkit.layout.controls import BufferControl, TokenListControl, FillControl
1111
from prompt_toolkit.layout.dimension import LayoutDimension
1212
from prompt_toolkit.layout.lexers import SimpleLexer
13-
from prompt_toolkit.layout.margins import Margin
13+
from prompt_toolkit.layout.margins import PromptMargin
1414
from prompt_toolkit.layout.menus import CompletionsMenu, MultiColumnCompletionsMenu
1515
from prompt_toolkit.layout.highlighters import SearchHighlighter, SelectionHighlighter, MatchingBracketHighlighter, ConditionalHighlighter
1616
from prompt_toolkit.layout.processors import ConditionalProcessor, AppendAutoSuggestion
@@ -19,7 +19,6 @@
1919
from prompt_toolkit.layout.utils import token_list_width
2020
from prompt_toolkit.reactive import Integer
2121
from prompt_toolkit.selection import SelectionType
22-
from prompt_toolkit.utils import get_cwidth
2322

2423
from .filters import HasSignature, ShowSidebar, ShowSignature, ShowDocstring
2524
from .utils import if_mousedown
@@ -250,42 +249,25 @@ def get_tokens(cli):
250249
~IsDone())
251250

252251

253-
class PromptMargin(Margin):
252+
class PythonPromptMargin(PromptMargin):
254253
"""
255254
Create margin that displays the prompt.
256255
It shows something like "In [1]:".
257256
"""
258257
def __init__(self, python_input):
259258
self.python_input = python_input
260259

261-
def _get_prompt_style(self):
262-
return self.python_input.all_prompt_styles[self.python_input.prompt_style]
260+
def get_prompt_style():
261+
return python_input.all_prompt_styles[python_input.prompt_style]
263262

264-
def get_width(self, cli):
265-
# Take the width from the first line.
266-
text = ''.join(t[1] for t in self.python_input.get_input_prompt_tokens(cli))
267-
return get_cwidth(text)
263+
def get_prompt(cli):
264+
return get_prompt_style().in_tokens(cli)
268265

269-
def create_margin(self, cli, window_render_info, width, height):
270-
style = self._get_prompt_style()
266+
def get_continuation_prompt(cli, width):
267+
return get_prompt_style().in2_tokens(cli, width)
271268

272-
# First line.
273-
tokens = style.in_tokens(cli)
274-
275-
# Next lines. (Show line numbering when numbering is enabled.)
276-
tokens2 = style.in2_tokens(cli, width)
277-
show_numbers = self.python_input.show_line_numbers
278-
visible_line_to_input_line = window_render_info.visible_line_to_input_line
279-
280-
for y in range(1, min(window_render_info.content_height, height)):
281-
tokens.append((Token, '\n'))
282-
if show_numbers:
283-
line_number = visible_line_to_input_line.get(y) or 0
284-
tokens.append((Token.LineNumber, ('%i ' % (line_number + 1)).rjust(width)))
285-
else:
286-
tokens.extend(tokens2)
287-
288-
return tokens
269+
super(PythonPromptMargin, self).__init__(get_prompt, get_continuation_prompt,
270+
show_numbers=Condition(lambda cli: python_input.show_line_numbers))
289271

290272

291273
def status_bar(key_bindings_manager, python_input):
@@ -518,7 +500,7 @@ def menu_position(cli):
518500
# Make sure that we always see the result of an reverse-i-search:
519501
preview_search=Always(),
520502
),
521-
left_margins=[PromptMargin(python_input)],
503+
left_margins=[PythonPromptMargin(python_input)],
522504
# Scroll offsets. The 1 at the bottom is important to make sure the
523505
# cursor is never below the "Press [Meta+Enter]" message which is a float.
524506
scroll_offsets=ScrollOffsets(bottom=1, left=4, right=4),

0 commit comments

Comments
 (0)