refactor: use 1-based indexing for klipper instance selection

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2024-06-30 18:06:23 +02:00
parent 372712ba32
commit 9ec12ba0b8
2 changed files with 12 additions and 6 deletions

View File

@@ -32,6 +32,7 @@ def print_instance_overview(
display_type: DisplayType = DisplayType.SERVICE_NAME, display_type: DisplayType = DisplayType.SERVICE_NAME,
show_headline=True, show_headline=True,
show_index=False, show_index=False,
start_index=0,
show_select_all=False, show_select_all=False,
): ):
dialog = "╔═══════════════════════════════════════════════════════╗\n" dialog = "╔═══════════════════════════════════════════════════════╗\n"
@@ -55,7 +56,7 @@ def print_instance_overview(
name = s.get_service_file_name() name = s.get_service_file_name()
else: else:
name = s.data_dir name = s.data_dir
line = f"{COLOR_CYAN}{f'{i})' if show_index else ''} {name}{RESET_FORMAT}" line = f"{COLOR_CYAN}{f'{i + start_index})' if show_index else ''} {name}{RESET_FORMAT}"
dialog += f"{line:<63}\n" dialog += f"{line:<63}\n"
dialog += "╟───────────────────────────────────────────────────────╢\n" dialog += "╟───────────────────────────────────────────────────────╢\n"

View File

@@ -50,11 +50,17 @@ def run_klipper_removal(
def select_instances_to_remove( def select_instances_to_remove(
instances: List[Klipper], instances: List[Klipper],
) -> Union[List[Klipper], None]: ) -> Union[List[Klipper], None]:
print_instance_overview(instances, show_index=True, show_select_all=True) start_index = 1
options = [str(i + start_index) for i in range(len(instances))]
options = [str(i) for i in range(len(instances))]
options.extend(["a", "A", "b", "B"]) options.extend(["a", "A", "b", "B"])
instance_map = {options[i]: instances[i] for i in range(len(instances))}
print_instance_overview(
instances,
start_index=start_index,
show_index=True,
show_select_all=True,
)
selection = get_selection_input("Select Klipper instance to remove", options) selection = get_selection_input("Select Klipper instance to remove", options)
instances_to_remove = [] instances_to_remove = []
@@ -63,8 +69,7 @@ def select_instances_to_remove(
elif selection == "a".lower(): elif selection == "a".lower():
instances_to_remove.extend(instances) instances_to_remove.extend(instances)
else: else:
instance = instances[int(selection)] instances_to_remove.append(instance_map[selection])
instances_to_remove.append(instance)
return instances_to_remove return instances_to_remove