This repository has been archived on 2022-12-03. You can view files and clone it, but cannot push or open issues or pull requests.
texty/lib/menu.py

44 lines
1.3 KiB
Python
Raw Normal View History

2022-03-24 18:20:39 +01:00
from os import system
2022-03-23 14:20:59 +01:00
import keyboard
2022-03-24 18:20:39 +01:00
from colorama import Fore
class MenuManager:
'''
Creates text menus controllable with arrow keys
'''
def __init__(self,selections:list,additional:str):
2022-03-23 14:20:59 +01:00
self.selected = 0 # current selection
self.selections = selections # available selections
2022-03-24 18:20:39 +01:00
self.additional = additional # additional text to display above the menu
keyboard.add_hotkey("up",self.up)
keyboard.add_hotkey("down",self.down)
keyboard.add_hotkey("enter",self.make_selection)
self.show_menu()
input()
def make_selection(self) -> int:
keyboard.remove_all_hotkeys()
def up(self):
if self.selected == 0:
self.selected = len(self.selections)-1
else:
self.selected -= 1
system("cls||clear")
self.show_menu()
def down(self):
if self.selected == len(self.selections)-1:
self.selected = 0
else:
self.selected += 1
system("cls||clear")
self.show_menu()
def show_menu(self):
print(self.additional)
for selection in self.selections:
if(self.selected == self.selections.index(selection)):
print(f"{Fore.RED}->{Fore.RESET} {selection}")
else:
print(f" {selection}")