Enhacements to list_manager: (#1346)

* Enhacements to list_manager:
method filter_option. To filter options based on selected entry
attrib. last_choice. Which is the last action executed before exiting the loop

* last_choice is now a calculated attribute, therefore readonly

* Added last_choice to selection_menu

* bug at selection_menu handling. Translations can be a problem
This commit is contained in:
Werner Llácer 2022-08-01 09:44:26 +02:00 committed by GitHub
parent d2f58362c9
commit 3bc3922545
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -44,6 +44,12 @@ class ListManager:
self._base_actions = base_actions
self._sub_menu_actions = sub_menu_actions
self._last_choice = None
@property
def last_choice(self):
return self._last_choice
def run(self):
while True:
# this will return a dictionary with the key as the menu entry to be displayed
@ -73,6 +79,7 @@ class ListManager:
selected_entry = data_formatted[choice.value]
self._run_actions_on_entry(selected_entry)
self._last_choice = choice
if choice.value == self._cancel_action:
return self._original_data # return the original list
else:
@ -97,7 +104,7 @@ class ListManager:
return options, header
def _run_actions_on_entry(self, entry: Any):
options = self._sub_menu_actions + [self._cancel_action]
options = self.filter_options(entry,self._sub_menu_actions) + [self._cancel_action]
display_value = self.selected_action_display(entry)
prompt = _("Select an action for '{}'").format(display_value)
@ -129,3 +136,7 @@ class ListManager:
# this function is called when a base action or
# a specific action for an entry is triggered
raise NotImplementedError('Please implement me in the child class')
def filter_options(self, selection :Any, options :List[str]) -> List[str]:
# filter which actions to show for an specific selection
return options

View File

@ -188,6 +188,11 @@ class GeneralMenu:
self._menu_options: Dict[str, Selector] = {}
self._setup_selection_menu_options()
self.preview_size = preview_size
self._last_choice = None
@property
def last_choice(self):
return self._last_choice
def __enter__(self, *args :Any, **kwargs :Any) -> GeneralMenu:
self.is_context_mgr = True
@ -325,6 +330,10 @@ class GeneralMenu:
if not self._process_selection(value):
break
# we get the last action key
actions = {str(v.description):k for k,v in self._menu_options.items()}
self._last_choice = actions[selection.value.strip()]
if not self.is_context_mgr:
self.__exit__()