index : archinstall32 | |
Archlinux32 installer | gitolite user |
summaryrefslogtreecommitdiff |
-rw-r--r-- | archinstall/lib/menu/list_manager.py | 97 |
diff --git a/archinstall/lib/menu/list_manager.py b/archinstall/lib/menu/list_manager.py index 4e6dffbe..cb567093 100644 --- a/archinstall/lib/menu/list_manager.py +++ b/archinstall/lib/menu/list_manager.py @@ -84,12 +84,12 @@ The contents in the base class of this methods serve for a very basic usage, and ``` """ +import copy +from os import system +from typing import Union, Any, TYPE_CHECKING, Dict, Optional from .text_input import TextInput from .menu import Menu -from os import system -from copy import copy -from typing import Union, Any, List, TYPE_CHECKING if TYPE_CHECKING: _: Any @@ -144,82 +144,99 @@ class ListManager: self.bottom_list = [self.confirm_action,self.cancel_action] self.bottom_item = [self.cancel_action] self.base_actions = base_actions if base_actions else [str(_('Add')),str(_('Copy')),str(_('Edit')),str(_('Delete'))] - self.base_data = base_list - self._data = copy(base_list) # as refs, changes are immediate + self._original_data = copy.deepcopy(base_list) + self._data = copy.deepcopy(base_list) # as refs, changes are immediate # default values for the null case - self.target = None + self.target: Optional[Any] = None self.action = self._null_action + if len(self._data) == 0 and self._null_action: - self.exec_action(self._data) + self._data = self.exec_action(self._data) def run(self): while True: - self._data_formatted = self.reformat(self._data) - options = self._data_formatted + [self.separator] + # this will return a dictionary with the key as the menu entry to be displayed + # and the value is the original value from the self._data container + data_formatted = self.reformat(self._data) + options = list(data_formatted.keys()) + options.append(self.separator) + if self._default_action: options += self._default_action + options += self.bottom_list + system('clear') - target = Menu(self._prompt, + + target = Menu( + self._prompt, options, sort=False, clear_screen=False, clear_menu_on_exit=False, header=self.header, - skip_empty_entries=True).run() + skip_empty_entries=True, + skip=False + ).run() - if not target or target in self.bottom_list: + if not target.value or target.value in self.bottom_list: self.action = target break - if target and target == self.separator: - continue - if target and target in self._default_action: - self.action = target - target = None + + if target.value and target.value in self._default_action: + self.action = target.value self.target = None - self.exec_action(self._data) + self._data = self.exec_action(self._data) continue + if isinstance(self._data,dict): - key = list(self._data.keys())[self._data_formatted.index(target)] - self.target = {key: self._data[key]} + data_key = data_formatted[target.value] + key = self._data[data_key] + self.target = {data_key: key} + elif isinstance(self._data, list): + self.target = [d for d in self._data if d == data_formatted[target.value]][0] else: - self.target = self._data[self._data_formatted.index(target)] + self.target = self._data[data_formatted[target.value]] + # Possible enhacement. If run_actions returns false a message line indicating the failure - self.run_actions(target) + self.run_actions(target.value) - if not target or target == self.cancel_action: # TODO dubious - return self.base_data # return the original list + if target.value == self.cancel_action: # TODO dubious + return self._original_data # return the original list else: return self._data def run_actions(self,prompt_data=None): options = self.action_list() + self.bottom_item prompt = _("Select an action for < {} >").format(prompt_data if prompt_data else self.target) - self.action = Menu( + choice = Menu( prompt, options, sort=False, clear_screen=False, clear_menu_on_exit=False, preset_values=self.bottom_item, - show_search_hint=False).run() - if not self.action or self.action == self.cancel_action: - return False - else: - return self.exec_action(self._data) + show_search_hint=False + ).run() + + self.action = choice.value + + if self.action and self.action != self.cancel_action: + self._data = self.exec_action(self._data) + """ The following methods are expected to be overwritten by the user if the needs of the list are beyond the simple case """ - def reformat(self, data: Any) -> List[Any]: + def reformat(self, data: Any) -> Dict[str, Any]: """ method to get the data in a format suitable to be shown It is executed once for run loop and processes the whole self._data structure """ if isinstance(data,dict): - return list(map(lambda x:f"{x} : {data[x]}",data)) + return {f'{k}: {v}': k for k, v in data.items()} else: - return list(map(lambda x:str(x),data)) + return {str(k): k for k in data} def action_list(self): """ @@ -238,18 +255,18 @@ class ListManager: # TODO guarantee unicity if isinstance(self._data,list): if self.action == str(_('Add')): - self.target = TextInput(_('Add :'),None).run() + self.target = TextInput(_('Add: '),None).run() self._data.append(self.target) if self.action == str(_('Copy')): while True: - target = TextInput(_('Copy to :'),self.target).run() + target = TextInput(_('Copy to: '),self.target).run() if target != self.target: self._data.append(self.target) break elif self.action == str(_('Edit')): tgt = self.target idx = self._data.index(self.target) - result = TextInput(_('Edite :'),tgt).run() + result = TextInput(_('Edit: '),tgt).run() self._data[idx] = result elif self.action == str(_('Delete')): del self._data[self._data.index(self.target)] @@ -261,8 +278,8 @@ class ListManager: origkey = None origval = None if self.action == str(_('Add')): - key = TextInput(_('Key :'),None).run() - value = TextInput(_('Value :'),None).run() + key = TextInput(_('Key: '),None).run() + value = TextInput(_('Value: '),None).run() self._data[key] = value if self.action == str(_('Copy')): while True: @@ -271,7 +288,9 @@ class ListManager: self._data[key] = origval break elif self.action == str(_('Edit')): - value = TextInput(_(f'Edit {origkey} :'),origval).run() + value = TextInput(_('Edit {}: ').format(origkey), origval).run() self._data[origkey] = value elif self.action == str(_('Delete')): del self._data[origkey] + + return self._data |