Additions to Listmgr et alia (#1028)

* Permit multiple default actions as a list

* Define headers for list at ListManager and Menu

* small corrections for default values

* Make subvolume info work.
The real marker that signifies that a mounted subvolume exist is the bracket notation nor the at sign
This commit is contained in:
Werner Llácer 2022-03-10 09:42:03 +01:00 committed by GitHub
parent f7aba1d31c
commit a6b1cab077
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 15 deletions

View File

@ -25,7 +25,7 @@ class BtrfsSubvolume:
root :bool = False
def get_subvolumes_from_findmnt(struct :Dict[str, Any], index=0) -> Iterator[BtrfsSubvolume]:
if '@' in struct['source']:
if '[' in struct['source']:
subvolume = re.findall(r'\[.*?\]', struct['source'])[0][1:-1]
struct['source'] = struct['source'].replace(f"[{subvolume}]", "")
yield BtrfsSubvolume(

View File

@ -90,9 +90,10 @@ from .menu import Menu
from ..general import RequirementError
from os import system
from copy import copy
from typing import Union
class ListManager:
def __init__(self,prompt :str, base_list :list ,base_actions :list = None,null_action :str = None, default_action :str = None):
def __init__(self,prompt :str, base_list :Union[list,dict] ,base_actions :list = None,null_action :str = None, default_action :Union[str,list] = None, header :Union[str,list] = None):
"""
param :prompt Text which will appear at the header
type param: string | DeferredTranslation
@ -108,7 +109,10 @@ class ListManager:
param: default_action action which will be presented at the bottom of the list. Shouldn't need a target. If not present, null_action is set there.
Both Null and Default actions can be defined outside the base_actions list, as long as they are launched in exec_action
type param: string
type param: string or list
param: header one or more header lines for the list
type param: string or list
"""
if not null_action and len(base_list) == 0:
@ -117,9 +121,13 @@ class ListManager:
self.prompt = prompt if prompt else _('Choose an object from the list')
self.null_action = str(null_action)
if not default_action:
self.default_action = self.null_action
self.default_action = [self.null_action,]
elif isinstance(default_action,(list,tuple)):
self.default_action = default_action
else:
self.default_action = str(default_action)
self.default_action = [str(default_action),]
self.header = header if header else None
self.cancel_action = str(_('Cancel'))
self.confirm_action = str(_('Confirm and exit'))
self.separator = '==>'
@ -140,23 +148,24 @@ class ListManager:
self.data_formatted = self.reformat()
options = self.data_formatted + [self.separator]
if self.default_action:
options += [self.default_action]
options += self.default_action
options += self.bottom_list
system('clear')
target = Menu(self.prompt,
options,
sort=False,
clear_screen=False,
clear_menu_on_exit=False).run()
clear_menu_on_exit=False,
header=self.header).run()
if not target or target in self.bottom_list:
break
if target and target == self.separator:
continue
if target and target == self.default_action:
if target and target in self.default_action:
self.action = target
target = None
self.target = None
self.action = self.default_action
self.exec_action()
continue
if isinstance(self.data,dict):
@ -216,7 +225,6 @@ class ListManager:
The basic code is useful for simple lists and dictionaries (key:value pairs, both strings)
"""
# TODO guarantee unicity
if isinstance(self.data,list):
if self.action == str(_('Add')):
self.target = TextInput(_('Add :'),None).run()
@ -264,6 +272,7 @@ if __name__ == "__main__":
# opciones = ['uno','dos','tres','cuatro']
# opciones = archinstall.list_mirrors()
opciones = {'uno':1,'dos':2,'tres':3,'cuatro':4}
# acciones = ['editar','borrar','añadir']
opciones = ListManager('Vamos alla',opciones,None,_('Add')).run()
acciones = ['editar','borrar','añadir']
cabecera = ["En Jaen Donde Resido","Vive don Lope de Sosa"]
opciones = ListManager('Vamos alla',opciones,None,_('Add'),default_action=acciones,header=cabecera).run()
print(opciones)

View File

@ -26,6 +26,7 @@ class Menu(TerminalMenu):
preview_command=None,
preview_size=0.75,
preview_title='Info',
header :Union[List[str],str] = None,
**kwargs
):
"""
@ -65,6 +66,9 @@ class Menu(TerminalMenu):
:param preview_title: Title of the preview window
:type preview_title: str
param: header one or more header lines for the menu
type param: string or list
:param kwargs : any SimpleTerminal parameter
"""
# we guarantee the inmutability of the options outside the class.
@ -104,10 +108,15 @@ class Menu(TerminalMenu):
self.default_option = default_option
self.multi = multi
menu_title = f'\n{title}\n\n'
if skip:
if header:
separator = '\n '
if not isinstance(header,(list,tuple)):
header = [header,]
if skip:
menu_title += str(_("Use ESC to skip\n"))
menu_title += separator + separator.join(header)
elif skip:
menu_title += str(_("Use ESC to skip\n\n"))
if default_option:
# if a default value was specified we move that one
# to the top of the list and mark it as default as well