# main.py from rich.console import Console from rich.table import Table from rich.console import Group from rich import print from rich.panel import Panel from rich.traceback import install # ***edit here to assign Class for menu generation*** from template import TEMPLATE as Class_For_Menu # Import the class(es) from specified file def main_menu(): obj = Class_For_Menu() # create object(s) of imported class(es) install() console = Console() INPUT_TXT = "[green]Input[/green] or [red][bold]q[/bold][/red] to quit:\n" while True: # display titles as menu options console.clear() menu_text = "" for i, method in enumerate(obj.methods, start=1): menu_text += f"{i}. {method['title']}\n" menu_text += f"{len(obj.methods) + 1}. Exit" console.print(print_panel(obj, menu_text)) # get and proceess menu choice choice = console.input("Select an option: ") try: # Convert the choice to an index choice_index = int(choice) - 1 if 0 <= choice_index < len(obj.methods): # print the method description, get input console.clear() method_description = obj.methods[choice_index]["description"] form_text = f"[green]{method_description}[/green]" console.print(print_panel(obj, form_text)) input_string = console.input(INPUT_TXT) # Check for Quit / 'q' while input_string != "q": # dynamically call the method and print result console.clear() method_name = obj.methods[choice_index]["name"] method = getattr(obj, method_name) # print the results result_table = method(input_string) form_group = Group(format_results(result_table), form_text) console.print(print_panel(obj, form_group)) input_string = console.input(INPUT_TXT) elif choice_index == len(obj.methods): console.print("[bold magenta]Goodbye![/bold magenta]") break else: console.print("[bold red]Invalid option, please try again.[/bold red]") except ValueError: console.print("[bold red]Please enter a valid number.[/bold red]") except Exception as e: console.print(f"[bold red]An error occurred: {e}[/bold red]") def format_results(result_array): """print the result of the operation as a table""" table = Table() # Add header with white color for header in result_array[0]: table.add_column(header, style="white", header_style="bold white") # Define colors for up to 5 columns colors = ["cyan", "magenta", "green", "yellow", "blue"] # Add the rest of the rows with colored columns for row in result_array[1:]: colored_row = [ f"[{colors[i % len(colors)]}]{row[i]}[/{colors[i % len(colors)]}]" for i in range(len(row)) ] table.add_row(*colored_row) return table def print_panel(object, form): """print the output data in a panel""" panel = Panel.fit( form, title=object.title, border_style="blue", padding=(1, 2) ) return panel if __name__ == "__main__": main_menu()