-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"""Menu finder widgets""" | ||
|
||
import re | ||
from typing import Callable, Iterable, Optional | ||
|
||
from prompt_toolkit.buffer import Buffer | ||
from prompt_toolkit.formatted_text import to_plain_text | ||
from prompt_toolkit.key_binding.key_processor import KeyPressEvent | ||
from prompt_toolkit.layout.containers import Container, HSplit, VSplit, Window | ||
from prompt_toolkit.layout.controls import BufferControl | ||
|
||
from .vertmenu import Item, VertMenu | ||
|
||
E = KeyPressEvent | ||
|
||
|
||
class SearchingVertMenu: | ||
def __init__( | ||
self, | ||
items: Iterable[Item], | ||
selected_item: Optional[Item] = None, | ||
selected_handler: Optional[Callable[[Optional[Item], int], None]] = None, | ||
accept_handler: Optional[Callable[[Item], None]] = None, | ||
): | ||
self._all_items = tuple(items) | ||
self._vertmenu = VertMenu( | ||
self._all_items, selected_item, selected_handler, accept_handler | ||
) | ||
self.buffer = Buffer(multiline=False, on_text_changed=self.apply_search) | ||
self.control = BufferControl( | ||
buffer=self.buffer, key_bindings=self._vertmenu._init_key_bindings() | ||
) | ||
self.window = HSplit( | ||
[ | ||
VSplit([Window(width=1, char=">", height=1), Window(self.control)]), | ||
self._vertmenu, | ||
] | ||
) | ||
|
||
def apply_search(self, buf: Buffer) -> None: | ||
raise NotImplementedError | ||
|
||
def __pt_container__(self) -> Container: | ||
return self.window | ||
|
||
|
||
class RegexSearchVertMenu(SearchingVertMenu): | ||
def apply_search(self, buf: Buffer) -> None: | ||
regex_str = buf.document.text | ||
try: | ||
regex = re.compile(regex_str) | ||
except re.error: | ||
return | ||
item = next( | ||
(item for item in self._all_items if regex.search(to_plain_text(item[0]))) | ||
) | ||
if item: | ||
self._vertmenu.control.selected_item = item | ||
|
||
|
||
class FuzzSearchVertMenu(SearchingVertMenu): | ||
def apply_search(self, buf: Buffer) -> None: | ||
text = buf.document.text | ||
regex_str = ".*".join(text) | ||
try: | ||
regex = re.compile(regex_str, re.IGNORECASE) | ||
except re.error: | ||
return | ||
item = next( | ||
(item for item in self._all_items if regex.search(to_plain_text(item[0]))) | ||
) | ||
if item: | ||
self._vertmenu.control.selected_item = item |