From 5990491292fb1a9b7dbd80af6109d9e71630a75c Mon Sep 17 00:00:00 2001 From: Werner Llácer Date: Fri, 11 Feb 2022 13:22:35 +0100 Subject: Some refinements to the Menu infraestructure (#962) * Correct definition of btrfs standard layout * Solve issue #936 * make ask_for_a_timezone as synonym to ask_timezone * Some refining in GeneralMenu secret is now a general function * Revert "Some refining in GeneralMenu" This reverts commit e6e131cb19795e0ddc169e897ae4df57a1c7f9fb. * New version of the FlexibleMenu * Added new functionality to Selector * Created a GeneralMenu class * GlobalMenu is made a child of GeneralMenu * Some refining in GeneralMenu secret is now a general function * log is invoked in GeneralMenu directly * Materialize again _setup_selection_menu_options. Gives more room to play * Callbacks converted as methods Synch() (data area and menu) decoupled from enable() and made general before any run * Only_hd gets a new implementation of the menu flake8 corrections * New version of the FlexibleMenu * Added new functionality to Selector * Created a GeneralMenu class * GlobalMenu is made a child of GeneralMenu * changes from the rebase left dangling * Adapt to PR #874 * Adapted to nationalization framework (PR 893). String still NOT adapted * flake8 complains * Use of archinstall.output_config instead of local copy at swiss.py * Problems with the last merge * git complains * Menu admits now preset values and cursor positioning * Now GeneralMenu moves to the next entry after each selection * flake8 complains * Control of limits for cursor position at GeneralMenu * Make auto cursor positioning optional at GeneralMenu. True for GlobalMenu * Code cleanup after rebase, and flake8 complains --- archinstall/lib/menu/menu.py | 46 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) (limited to 'archinstall/lib/menu/menu.py') diff --git a/archinstall/lib/menu/menu.py b/archinstall/lib/menu/menu.py index 1811482d..ac075b66 100644 --- a/archinstall/lib/menu/menu.py +++ b/archinstall/lib/menu/menu.py @@ -7,7 +7,7 @@ import sys import logging class Menu(TerminalMenu): - def __init__(self, title, p_options, skip=True, multi=False, default_option=None, sort=True): + def __init__(self, title, p_options, skip=True, multi=False, default_option=None, sort=True, preset_values=None, cursor_index=None): """ Creates a new menu @@ -29,6 +29,12 @@ class Menu(TerminalMenu): :param sort: Indicate if the options should be sorted alphabetically before displaying :type sort: bool + + :param preset_values: Predefined value(s) of the menu. In a multi menu, it selects the options included therein. If the selection is simple, moves the cursor to the position of the value + :type preset_values: str or list + + :param cursor_index: The position where the cursor will be located. If it is not in range (number of elements of the menu) it goes to the first position + :type cursor_index: int """ # we guarantee the inmutability of the options outside the class. # an unknown number of iterables (.keys(),.values(),generator,...) can't be directly copied, in this case @@ -61,6 +67,7 @@ class Menu(TerminalMenu): self.skip = skip self.default_option = default_option self.multi = multi + self.preselection(preset_values,cursor_index) menu_title = f'\n{title}\n\n' @@ -86,7 +93,9 @@ class Menu(TerminalMenu): cycle_cursor=True, clear_screen=True, multi_select=multi, - show_search_hint=True + show_search_hint=True, + preselected_entries=self.preset_values, + cursor_index=self.cursor_index ) def _show(self): @@ -114,3 +123,36 @@ class Menu(TerminalMenu): return self.run() return ret + + def set_cursor_pos(self,pos :int): + if pos and 0 < pos < len(self._menu_entries): + self._view.active_menu_index = pos + else: + self._view.active_menu_index = 0 # we define a default + + def set_cursor_pos_entry(self,value :str): + pos = self._menu_entries.index(value) + self.set_cursor_pos(pos) + + def preselection(self,preset_values :list = [],cursor_index :int = None): + def from_preset_to_cursor(): + if preset_values: + if isinstance(preset_values,str): + self.cursor_index = self.menu_options.index(preset_values) + else: # should return an error, but this is smoother + self.cursor_index = self.menu_options.index(preset_values[0]) + + self.preset_values = preset_values + self.cursor_index = cursor_index + if preset_values and cursor_index is None: + from_preset_to_cursor() + if preset_values and not self.multi: # Not supported by the infraestructure + self.preset_values = None + from_preset_to_cursor() + + if self.default_option and self.multi: + if isinstance(preset_values,str) and self.default_option == preset_values: + self.preset_values = f"{preset_values} (default)" + elif isinstance(preset_values,(list,tuple)) and self.default_option in preset_values: + idx = preset_values.index(self.default_option) + self.preset_values[idx] = f"{preset_values[idx]} (default)" -- cgit v1.2.3-54-g00ecf