|
10 | 10 | from prompt_toolkit.layout.controls import BufferControl, TokenListControl, FillControl |
11 | 11 | from prompt_toolkit.layout.dimension import LayoutDimension |
12 | 12 | from prompt_toolkit.layout.lexers import SimpleLexer |
13 | | -from prompt_toolkit.layout.margins import Margin |
| 13 | +from prompt_toolkit.layout.margins import PromptMargin |
14 | 14 | from prompt_toolkit.layout.menus import CompletionsMenu, MultiColumnCompletionsMenu |
15 | 15 | from prompt_toolkit.layout.highlighters import SearchHighlighter, SelectionHighlighter, MatchingBracketHighlighter, ConditionalHighlighter |
16 | 16 | from prompt_toolkit.layout.processors import ConditionalProcessor, AppendAutoSuggestion |
|
19 | 19 | from prompt_toolkit.layout.utils import token_list_width |
20 | 20 | from prompt_toolkit.reactive import Integer |
21 | 21 | from prompt_toolkit.selection import SelectionType |
22 | | -from prompt_toolkit.utils import get_cwidth |
23 | 22 |
|
24 | 23 | from .filters import HasSignature, ShowSidebar, ShowSignature, ShowDocstring |
25 | 24 | from .utils import if_mousedown |
@@ -250,42 +249,25 @@ def get_tokens(cli): |
250 | 249 | ~IsDone()) |
251 | 250 |
|
252 | 251 |
|
253 | | -class PromptMargin(Margin): |
| 252 | +class PythonPromptMargin(PromptMargin): |
254 | 253 | """ |
255 | 254 | Create margin that displays the prompt. |
256 | 255 | It shows something like "In [1]:". |
257 | 256 | """ |
258 | 257 | def __init__(self, python_input): |
259 | 258 | self.python_input = python_input |
260 | 259 |
|
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] |
263 | 262 |
|
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) |
268 | 265 |
|
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) |
271 | 268 |
|
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)) |
289 | 271 |
|
290 | 272 |
|
291 | 273 | def status_bar(key_bindings_manager, python_input): |
@@ -518,7 +500,7 @@ def menu_position(cli): |
518 | 500 | # Make sure that we always see the result of an reverse-i-search: |
519 | 501 | preview_search=Always(), |
520 | 502 | ), |
521 | | - left_margins=[PromptMargin(python_input)], |
| 503 | + left_margins=[PythonPromptMargin(python_input)], |
522 | 504 | # Scroll offsets. The 1 at the bottom is important to make sure the |
523 | 505 | # cursor is never below the "Press [Meta+Enter]" message which is a float. |
524 | 506 | scroll_offsets=ScrollOffsets(bottom=1, left=4, right=4), |
|
0 commit comments