From 1f2fe467d1956fd3d4550c33069da2f9f0017c72 Mon Sep 17 00:00:00 2001 From: SecondThundeR Date: Thu, 29 Apr 2021 00:59:35 +0300 Subject: Update generic_multi_select Changes: - Add useful checks from `generic_select` - Sorting is now disabled by default (As many lists are already sorted) - Some checks have been changed (This includes unnecessary checks with `len()`, etc.) - Removed x, y from `print_large_list` as they aren't used in code - Added check for string to strip it without getting `AttributeError` - Switched to RequirementError handling as in `generic_select` - Added a log when the default option is selected with unselected options by the user - Added break when adding default option to empty list (See comments for more info) - Added support for selecting option by name --- archinstall/lib/user_interaction.py | 70 +++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 22 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 451251cd..8a4b4c49 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -99,7 +99,18 @@ def print_large_list(options, padding=5, margin_bottom=0, separator=': '): return column, row -def generic_multi_select(options, text="Select one or more of the options above (leave blank to continue): ", sort=True, default=None, allow_empty=False): +def generic_multi_select(options, text="Select one or more of the options above (leave blank to continue): ", sort=False, default=None, allow_empty=False): + # For now, we check for list, but in future it's better to have support for dictionary + # (At the moment there are no cases of using dictionaries with this function) + if type(options) not in [list]: + log(f" * Generic multi-select doesn't support ({type(options)}) as type of options * ", fg='red') + log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') + raise RequirementError("generic_multi_select() requires list as options.") + if not options: + log(f" * Generic multi-select didn't find any options to choose from * ", fg='red') + log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') + raise RequirementError('generic_multi_select() requires at least one option to proceed.') + # After passing the checks, function continues to work if sort: options = sorted(options) @@ -108,7 +119,7 @@ def generic_multi_select(options, text="Select one or more of the options above selected_options = [] while True: - if len(selected_options) <= 0 and default and default in options: + if not selected_options and default in options: selected_options.append(default) printed_options = [] @@ -119,32 +130,47 @@ def generic_multi_select(options, text="Select one or more of the options above printed_options.append(f'{option}') section.clear(0, get_terminal_height()-section._cursor_y-1) - x, y = print_large_list(printed_options, margin_bottom=2) + print_large_list(printed_options, margin_bottom=2) section._cursor_y = len(printed_options) section._cursor_x = 0 section.write_line(text) section.input_pos = section._cursor_x selected_option = section.get_keyboard_input(end=None) - - if selected_option is None: - if len(selected_options) <= 0 and default: - selected_options = [default] - - if len(selected_options) or allow_empty is True: - break - else: - log('* Need to select at least one option!', fg='red') - continue - - elif selected_option.isdigit(): - if (selected_option := int(selected_option)) >= len(options): - log('* Option is out of range, please select another one!', fg='red') - continue - selected_option = options[selected_option] - if selected_option in selected_options: - selected_options.remove(selected_option) + # This string check is necessary to correct work with it + # Without this, Python can raise AttributeError because of stripping `None` + # It also allows you to remove empty spaces if the user accidentally entered them. + if isinstance(selected_option, str): + selected_option = selected_option.strip() + try: + if not selected_option: + # Added break when adding default option to empty list + # So that the check doesn't go to the next elif + # Since it still breaks the loop + if not selected_options and default: + selected_options = [default] + log(f'Default option selected: "{default}"', fg='yellow') + break + elif selected_options or allow_empty: + break + else: + raise RequirementError('Please select at least one option to continue!') + elif selected_option.isnumeric(): + if (selected_option := int(selected_option)) >= len(options): + raise RequirementError(f'Selected option "{selected_option}" is out of range!') + selected_option = options[selected_option] + if selected_option in selected_options: + selected_options.remove(selected_option) + else: + selected_options.append(selected_option) + elif selected_option in options: + if selected_option in selected_options: + selected_options.remove(selected_option) + else: + selected_options.append(selected_option) else: - selected_options.append(selected_option) + raise RequirementError(f'Selected option "{selected_option}" does not exist in available options') + except RequirementError as e: + log(f" * {e} * ", fg='red') return selected_options -- cgit v1.2.3-70-g09d2 From 5c8748dbb5f663f579b6a462d317162de163fa84 Mon Sep 17 00:00:00 2001 From: SecondThundeR Date: Thu, 29 Apr 2021 01:13:19 +0300 Subject: Update generic_select Changes: - Moved some functions for options below checks for the correctness of passed options - Removed unnecessary `continue` from `except ...`, since the loop will return to the beginning anyway - Added stripping of `selected_option` straight on input - Changed check `len() == 0` to `not ...` - Returned changing string to number on check === - Removed '!' as they look weird inside such ` * ... * ` log style (Change for generic_multi_select) --- archinstall/lib/user_interaction.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 8a4b4c49..437ca68f 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -153,10 +153,10 @@ def generic_multi_select(options, text="Select one or more of the options above elif selected_options or allow_empty: break else: - raise RequirementError('Please select at least one option to continue!') + raise RequirementError('Please select at least one option to continue') elif selected_option.isnumeric(): if (selected_option := int(selected_option)) >= len(options): - raise RequirementError(f'Selected option "{selected_option}" is out of range!') + raise RequirementError(f'Selected option "{selected_option}" is out of range') selected_option = options[selected_option] if selected_option in selected_options: selected_options.remove(selected_option) @@ -461,20 +461,24 @@ def generic_select(options, input_text="Select one of the above by index or abso this function returns an item from list, a string, or None """ - # Checking if options are different from `list` or `dict` + # Checking if the options are different from `list` or `dict` or if they are empty if type(options) not in [list, dict]: log(f" * Generic select doesn't support ({type(options)}) as type of options * ", fg='red') log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') raise RequirementError("generic_select() requires list or dictionary as options.") - # To allow only `list` and `dict`, converting values of options here. - # Therefore, now we can only provide the dictionary itself - if type(options) == dict: options = list(options.values()) - if sort: options = sorted(options) # As we pass only list and dict (converted to list), we can skip converting to list - if len(options) == 0: + if not options: log(f" * Generic select didn't find any options to choose from * ", fg='red') log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') raise RequirementError('generic_select() requires at least one option to proceed.') - + # After passing the checks, function continues to work + if type(options) == dict: + # To allow only `list` and `dict`, converting values of options here. + # Therefore, now we can only provide the dictionary itself + options = list(options.values()) + if sort: + # As we pass only list and dict (converted to list), we can skip converting to list + options = sorted(options) + # Added ability to disable the output of options items, # if another function displays something different from this @@ -486,8 +490,8 @@ def generic_select(options, input_text="Select one of the above by index or abso # Now the try...except block handles validation for invalid input from the user while True: try: - selected_option = input(input_text) - if len(selected_option.strip()) == 0: + selected_option = input(input_text).strip() + if not selected_option: # `allow_empty_input` parameter handles return of None on empty input, if necessary # Otherwise raise `RequirementError` if allow_empty_input: @@ -495,8 +499,7 @@ def generic_select(options, input_text="Select one of the above by index or abso raise RequirementError('Please select an option to continue') # Replaced `isdigit` with` isnumeric` to discard all negative numbers elif selected_option.isnumeric(): - selected_option = int(selected_option) - if selected_option >= len(options): + if (selected_option := int(selected_option)) >= len(options): raise RequirementError(f'Selected option "{selected_option}" is out of range') selected_option = options[selected_option] break @@ -506,7 +509,6 @@ def generic_select(options, input_text="Select one of the above by index or abso raise RequirementError(f'Selected option "{selected_option}" does not exist in available options') except RequirementError as err: log(f" * {err} * ", fg='red') - continue return selected_option -- cgit v1.2.3-70-g09d2 From 5f1156f80e4038a52719f4ba01c87eae4f0a8660 Mon Sep 17 00:00:00 2001 From: SecondThundeR Date: Thu, 29 Apr 2021 01:30:35 +0300 Subject: Fix multi select and video card driver selection Changes: - Rephrased input text for kernel selection - Fixed crash with empty video card driver selection - Removed log info for default option --- archinstall/lib/user_interaction.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 437ca68f..10d74b63 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -118,10 +118,10 @@ def generic_multi_select(options, text="Select one or more of the options above selected_options = [] - while True: - if not selected_options and default in options: - selected_options.append(default) + if not selected_options and default in options: + selected_options.append(default) + while True: printed_options = [] for option in options: if option in selected_options: @@ -148,7 +148,6 @@ def generic_multi_select(options, text="Select one or more of the options above # Since it still breaks the loop if not selected_options and default: selected_options = [default] - log(f'Default option selected: "{default}"', fg='yellow') break elif selected_options or allow_empty: break @@ -687,7 +686,7 @@ def select_driver(options=AVAILABLE_GFX_DRIVERS): elif b'amd' in line.lower(): print(' ** AMD card detected, suggested driver: AMD / ATI **') - initial_option = generic_select(drivers, input_text="Select your graphics card driver: ") + initial_option = generic_select(drivers, input_text="Select your graphics card driver: ", allow_empty_input=False) selected_driver = options[initial_option] if type(selected_driver) == dict: @@ -719,6 +718,6 @@ def select_kernel(options): kernels = sorted(list(options)) if kernels: - return generic_multi_select(kernels, f"Choose which kernel to use (leave blank for default: {DEFAULT_KERNEL}): ", default=DEFAULT_KERNEL) + return generic_multi_select(kernels, f"Choose which kernels to use (Default value for empty selection: {DEFAULT_KERNEL}): ", default=DEFAULT_KERNEL) raise RequirementError("Selecting kernels require a least one kernel to be given as an option.") -- cgit v1.2.3-70-g09d2 From ea14e860c7f730897544cb1bdb43964e25785c74 Mon Sep 17 00:00:00 2001 From: SecondThundeR Date: Thu, 29 Apr 2021 18:47:31 +0300 Subject: Update `user_interaction.py` - Reverted some changes for default options in multi select - Added check for dict and convert from dict to list - Replaced spaces with tabs for certain comment line --- archinstall/lib/user_interaction.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 10d74b63..f06354d2 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -100,17 +100,18 @@ def print_large_list(options, padding=5, margin_bottom=0, separator=': '): def generic_multi_select(options, text="Select one or more of the options above (leave blank to continue): ", sort=False, default=None, allow_empty=False): - # For now, we check for list, but in future it's better to have support for dictionary - # (At the moment there are no cases of using dictionaries with this function) - if type(options) not in [list]: + # Checking if the options are different from `list` or `dict` or if they are empty + if type(options) not in [list, dict]: log(f" * Generic multi-select doesn't support ({type(options)}) as type of options * ", fg='red') log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') - raise RequirementError("generic_multi_select() requires list as options.") + raise RequirementError("generic_multi_select() requires list or dictionary as options.") if not options: log(f" * Generic multi-select didn't find any options to choose from * ", fg='red') log(" * If problem persists, please create an issue on https://github.com/archlinux/archinstall/issues * ", fg='yellow') raise RequirementError('generic_multi_select() requires at least one option to proceed.') # After passing the checks, function continues to work + if type(options) == dict: + options = list(options.values()) if sort: options = sorted(options) @@ -118,10 +119,10 @@ def generic_multi_select(options, text="Select one or more of the options above selected_options = [] - if not selected_options and default in options: - selected_options.append(default) - while True: + if not selected_options and default in options: + selected_options.append(default) + printed_options = [] for option in options: if option in selected_options: @@ -136,19 +137,15 @@ def generic_multi_select(options, text="Select one or more of the options above section.write_line(text) section.input_pos = section._cursor_x selected_option = section.get_keyboard_input(end=None) - # This string check is necessary to correct work with it + # This string check is necessary to correct work with it # Without this, Python can raise AttributeError because of stripping `None` # It also allows you to remove empty spaces if the user accidentally entered them. if isinstance(selected_option, str): selected_option = selected_option.strip() try: if not selected_option: - # Added break when adding default option to empty list - # So that the check doesn't go to the next elif - # Since it still breaks the loop if not selected_options and default: selected_options = [default] - break elif selected_options or allow_empty: break else: @@ -718,6 +715,6 @@ def select_kernel(options): kernels = sorted(list(options)) if kernels: - return generic_multi_select(kernels, f"Choose which kernels to use (Default value for empty selection: {DEFAULT_KERNEL}): ", default=DEFAULT_KERNEL) + return generic_multi_select(kernels, f"Choose which kernels to use (leave blank for default: {DEFAULT_KERNEL}): ", default=DEFAULT_KERNEL) raise RequirementError("Selecting kernels require a least one kernel to be given as an option.") -- cgit v1.2.3-70-g09d2 From efadd4a42694d87b3fe86bd7bcedf1da9a9e1773 Mon Sep 17 00:00:00 2001 From: SecondThundeR Date: Fri, 30 Apr 2021 22:31:31 +0300 Subject: Revert disabling default sorting This change reverts a previous change that disabled sorting by default in the multi select function, which would be better disabled manually for pre-sorted lists than manually enabling for unsorted lists. Also, comments of the line check have been slightly changed --- archinstall/lib/user_interaction.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index f06354d2..81f04db4 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -99,7 +99,7 @@ def print_large_list(options, padding=5, margin_bottom=0, separator=': '): return column, row -def generic_multi_select(options, text="Select one or more of the options above (leave blank to continue): ", sort=False, default=None, allow_empty=False): +def generic_multi_select(options, text="Select one or more of the options above (leave blank to continue): ", sort=True, default=None, allow_empty=False): # Checking if the options are different from `list` or `dict` or if they are empty if type(options) not in [list, dict]: log(f" * Generic multi-select doesn't support ({type(options)}) as type of options * ", fg='red') @@ -138,8 +138,8 @@ def generic_multi_select(options, text="Select one or more of the options above section.input_pos = section._cursor_x selected_option = section.get_keyboard_input(end=None) # This string check is necessary to correct work with it - # Without this, Python can raise AttributeError because of stripping `None` - # It also allows you to remove empty spaces if the user accidentally entered them. + # Without this, Python will raise AttributeError because of stripping `None` + # It also allows to remove empty spaces if the user accidentally entered them. if isinstance(selected_option, str): selected_option = selected_option.strip() try: @@ -715,6 +715,6 @@ def select_kernel(options): kernels = sorted(list(options)) if kernels: - return generic_multi_select(kernels, f"Choose which kernels to use (leave blank for default: {DEFAULT_KERNEL}): ", default=DEFAULT_KERNEL) + return generic_multi_select(kernels, f"Choose which kernels to use (leave blank for default: {DEFAULT_KERNEL}): ", default=DEFAULT_KERNEL, sort=False) raise RequirementError("Selecting kernels require a least one kernel to be given as an option.") -- cgit v1.2.3-70-g09d2 From feae13ac84e10cdf624cb4b3b19ba4707a96dabd Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 30 Apr 2021 22:34:54 -0400 Subject: Tweak wording for superuser prompt a little bit --- archinstall/lib/user_interaction.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 451251cd..ac9ceefb 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -292,7 +292,7 @@ def ask_for_additional_users(prompt='Any additional users to install (leave blan continue password = get_password(prompt=f'Password for user {new_user}: ') - if input("Should this user be a sudo (super) user (y/N): ").strip(' ').lower() in ('y', 'yes'): + if input("Should this user be a superuser (sudoer) [y/N]: ").strip(' ').lower() in ('y', 'yes'): super_users[new_user] = {"!password" : password} else: users[new_user] = {"!password" : password} -- cgit v1.2.3-70-g09d2 From 4efa01c4fd1b14103f2690a92c7195bd6bb6fe42 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 30 Apr 2021 22:42:08 -0400 Subject: Make the style of the word superuser consistent --- archinstall/lib/user_interaction.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index ac9ceefb..be01594e 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -263,14 +263,14 @@ class MiniCurses(): if response: return response -def ask_for_superuser_account(prompt='Username for required super-user with sudo privileges: ', forced=False): +def ask_for_superuser_account(prompt='Username for required superuser with sudo privileges: ', forced=False): while 1: new_user = input(prompt).strip(' ') if not new_user and forced: # TODO: make this text more generic? # It's only used to create the first sudo user when root is disabled in guided.py - log(' * Since root is disabled, you need to create a least one (super) user!', fg='red') + log(' * Since root is disabled, you need to create a least one superuser!', fg='red') continue elif not new_user and not forced: raise UserError("No superuser was created.") @@ -282,7 +282,7 @@ def ask_for_superuser_account(prompt='Username for required super-user with sudo def ask_for_additional_users(prompt='Any additional users to install (leave blank for no users): '): users = {} - super_users = {} + superusers = {} while 1: new_user = input(prompt).strip(' ') @@ -293,11 +293,11 @@ def ask_for_additional_users(prompt='Any additional users to install (leave blan password = get_password(prompt=f'Password for user {new_user}: ') if input("Should this user be a superuser (sudoer) [y/N]: ").strip(' ').lower() in ('y', 'yes'): - super_users[new_user] = {"!password" : password} + superusers[new_user] = {"!password" : password} else: users[new_user] = {"!password" : password} - return users, super_users + return users, superusers def ask_for_a_timezone(): while True: -- cgit v1.2.3-70-g09d2 From 493814d8bd9a89089c6bedf82f9c85ca7075edfb Mon Sep 17 00:00:00 2001 From: SecondThundeR Date: Thu, 6 May 2021 21:38:20 +0300 Subject: Add default graphics card driver option --- archinstall/lib/user_interaction.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index 81f04db4..aea708c8 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -673,6 +673,7 @@ def select_driver(options=AVAILABLE_GFX_DRIVERS): """ drivers = sorted(list(options)) + default_option = options["All open-source (default)"] if drivers: lspci = sys_command(f'/usr/bin/lspci') @@ -683,7 +684,11 @@ def select_driver(options=AVAILABLE_GFX_DRIVERS): elif b'amd' in line.lower(): print(' ** AMD card detected, suggested driver: AMD / ATI **') - initial_option = generic_select(drivers, input_text="Select your graphics card driver: ", allow_empty_input=False) + initial_option = generic_select(drivers, input_text="Select your graphics card driver: ") + + if not initial_option: + return default_option + selected_driver = options[initial_option] if type(selected_driver) == dict: -- cgit v1.2.3-70-g09d2 From 24a14d1800e990e2e90dd628b8460f85fe3d1495 Mon Sep 17 00:00:00 2001 From: aboven Date: Sat, 8 May 2021 13:18:03 +0200 Subject: fix error when to many options and calculation spaces --- archinstall/lib/user_interaction.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'archinstall/lib/user_interaction.py') diff --git a/archinstall/lib/user_interaction.py b/archinstall/lib/user_interaction.py index be01594e..1240e0be 100644 --- a/archinstall/lib/user_interaction.py +++ b/archinstall/lib/user_interaction.py @@ -83,16 +83,18 @@ def get_password(prompt="Enter a password: "): def print_large_list(options, padding=5, margin_bottom=0, separator=': '): highest_index_number_length = len(str(len(options))) longest_line = highest_index_number_length + len(separator) + get_longest_option(options) + padding + spaces_without_option = longest_line - (len(separator) + highest_index_number_length) max_num_of_columns = get_terminal_width() // longest_line max_options_in_cells = max_num_of_columns * (get_terminal_height()-margin_bottom) if (len(options) > max_options_in_cells): for index, option in enumerate(options): print(f"{index}: {option}") + return 1, index else: for row in range(0, (get_terminal_height()-margin_bottom)): for column in range(row, len(options), (get_terminal_height()-margin_bottom)): - spaces = " "*(longest_line - len(options[column])) + spaces = " "*(spaces_without_option - len(options[column])) print(f"{str(column): >{highest_index_number_length}}{separator}{options[column]}", end = spaces) print() -- cgit v1.2.3-70-g09d2