from __future__ import print_function import os import math as m def header(lines): lines = [i for i in lines if i is not None] total_length = len(max(lines, key=len)) + 4 print("#" * (total_length)) for line in lines: left_side = m.floor(((total_length - 2 - len(line.strip())) / 2)) print("#" + " " * left_side + line.strip() + " " * (total_length - len("#" + " " * left_side + line.strip()) - 1) + "#") print("#" * total_length) def cls(): os.system('cls' if os.name == 'nt' else 'clear') # def menu(title, prompt, menu_options, add_quit=True, auto_number=False, in_between=[], top_level=False): # return_option = ["Q", "Quit", None] if top_level else ["B", "Back", None] # if add_quit: menu_options.append(return_option) # cls() # header(title) # print() # for i in in_between: print(i) # if in_between: print() # for index, option in enumerate(menu_options): # if auto_number and not (index == (len(menu_options) - 1) and add_quit): # option[0] = str((index + 1)) # print(option[0] + ". " + option[1]) # print() # selected = input(prompt) # keys = [option[0].upper() for option in menu_options] # if not selected or selected.upper() not in keys: # return # if selected.upper() == return_option[0]: # return -1 # else: # menu_options[keys.index(selected.upper())][2]() if menu_options[keys.index(selected.upper())][2] else None class TUIMenu(): def __init__(self, title, prompt, options=None, return_number_instead_of_direct_call=False, add_quit=True, auto_number=False, in_between=None, top_level=False): self.title = title self.prompt = prompt self.in_between = in_between or [] self.options = options or [] self.return_number_instead_of_direct_call = return_number_instead_of_direct_call self.auto_number = auto_number self.add_quit = add_quit self.top_level = top_level def add_menu_option(self, name, description="", function=None, key=""): self.options.append([key, name, description, function]) def start(self): return_option = ["Q", "Quit"] if self.top_level else ["B", "Back"] if self.add_quit: self.add_menu_option( return_option[1], function=None, key=return_option[0]) cls() header(self.title) print() for i in self.in_between: print(i) if self.in_between: print() for index, option in enumerate(self.options): if self.auto_number and not (index == (len(self.options) - 1) and self.add_quit): option[0] = str((index + 1)) print(option[0] + ". " + option[1]) for i in option[2]: print("\t" + i) print() selected = input(self.prompt) keys = [option[0].upper() for option in self.options] if not selected or selected.upper() not in keys: return if self.add_quit and selected.upper() == return_option[0]: return -1 elif self.return_number_instead_of_direct_call: return self.options[keys.index(selected.upper())][0] else: self.options[keys.index(selected.upper())][3]() if self.options[keys.index(selected.upper())][3] else None class TUIOnlyPrint(): def __init__(self, title, prompt, in_between=None): self.title = title self.prompt = prompt self.in_between = in_between or [] def start(self): cls() header(self.title) print() for i in self.in_between: print(i) if self.in_between: print() input(self.prompt)