Skip to content

Commit c916183

Browse files
Give CompletionMenu a is_visible function.
1 parent 6c4c5e5 commit c916183

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

prompt_toolkit/layout/__init__.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def write_input_scrolled(self, cli, screen, write_content,
216216
self.vertical_scroll = (temp_screen.cursor_position.y + 1) - max_height
217217

218218
# Scroll down if we need space for the menu.
219-
if self.need_to_show_completion_menu(cli):
219+
if self._need_to_show_completion_menu(cli):
220220
menu_size = self.menus[0].get_height(self._line(cli).complete_state)
221221
if temp_screen.cursor_position.y - self.vertical_scroll >= max_height - menu_size:
222222
self.vertical_scroll = (temp_screen.cursor_position.y + 1) - (max_height - menu_size)
@@ -240,7 +240,7 @@ def write_input_scrolled(self, cli, screen, write_content,
240240
y_after_input = y + top_margin
241241

242242
# Show completion menu.
243-
if not is_done and self.need_to_show_completion_menu(cli):
243+
if not is_done and self._need_to_show_completion_menu(cli):
244244
y, x = temp_screen._cursor_mappings[self._line(cli).complete_state.original_document.cursor_position]
245245
self.menus[0].write(screen, (y - self.vertical_scroll + top_margin, x + left_margin_width), self._line(cli).complete_state)
246246

@@ -256,11 +256,8 @@ def write_input_scrolled(self, cli, screen, write_content,
256256

257257
return return_value
258258

259-
def need_to_show_completion_menu(self, cli): # XXX: remove
260-
if cli.input_processor.input_mode in (InputMode.SYSTEM, InputMode.INCREMENTAL_SEARCH):
261-
return False
262-
263-
return self.menus and self._line(cli).complete_state
259+
def _need_to_show_completion_menu(self, cli):
260+
return self.menus and self.menus[0].is_visible(cli)
264261

265262
def write_to_screen(self, cli, screen, min_height):
266263
"""

prompt_toolkit/layout/menus.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import unicode_literals
22

33
from pygments.token import Token
4+
from ..enums import InputMode
45

56
__all__ = (
67
'CompletionMenu',
@@ -20,8 +21,18 @@ class CompletionMenu(object):
2021
progress_button_token = Token.CompletionMenu.ProgressButton
2122
progress_bar_token = Token.CompletionMenu.ProgressBar
2223

23-
def __init__(self, max_height=5):
24+
def __init__(self, max_height=5, line_name='default'):
2425
self.max_height = max_height
26+
self.line_name = line_name
27+
28+
def is_visible(self, cli):
29+
"""
30+
True when this menu is visible.
31+
"""
32+
if cli.input_processor.input_mode in (InputMode.SYSTEM, InputMode.INCREMENTAL_SEARCH):
33+
return False
34+
35+
return bool(cli.lines[self.line_name].complete_state)
2536

2637
def get_height(self, complete_state):
2738
"""

0 commit comments

Comments
 (0)