2023-12-21 08:33:05 -05:00
|
|
|
import keyboard
|
2023-12-21 10:18:38 -05:00
|
|
|
import os
|
|
|
|
|
2023-12-21 13:15:35 -05:00
|
|
|
TEXT_REGULAR = '\033[38;5;2m'
|
|
|
|
TEXT_HIGHLIGHTED = '\033[38;5;0m\033[48;5;2m'
|
|
|
|
RESET_CODE = '\033[2;0;0m'
|
2023-12-21 12:43:38 -05:00
|
|
|
|
2023-12-21 10:18:38 -05:00
|
|
|
|
2023-12-21 13:31:53 -05:00
|
|
|
def matrix():
|
|
|
|
os.system('cmatrix')
|
|
|
|
|
|
|
|
|
2023-12-21 13:15:35 -05:00
|
|
|
class Text:
|
|
|
|
def __init__(self, text: str):
|
|
|
|
self.text = text
|
|
|
|
self.prefix = TEXT_REGULAR
|
|
|
|
self.suffix = RESET_CODE
|
|
|
|
|
|
|
|
def highlight(self):
|
|
|
|
self.prefix = TEXT_HIGHLIGHTED
|
|
|
|
return self
|
|
|
|
|
|
|
|
def regular(self):
|
|
|
|
self.prefix = TEXT_REGULAR
|
|
|
|
return self
|
|
|
|
|
|
|
|
def draw_at(self, line: int, col: int):
|
|
|
|
position = f'\033[{int(line)};{int(col)}f'
|
|
|
|
print(f'{position}{self.prefix}{self.text}{self.suffix}', end='\r')
|
2023-12-21 10:18:38 -05:00
|
|
|
|
|
|
|
|
|
|
|
class Menu:
|
2023-12-21 13:15:35 -05:00
|
|
|
def __init__(self, title: str, options: dict):
|
|
|
|
self.title = f'[{title}]'
|
|
|
|
self.options = options
|
2023-12-21 13:31:53 -05:00
|
|
|
self.cursor_pos = 0
|
|
|
|
self.selected = None
|
2023-12-21 10:18:38 -05:00
|
|
|
|
|
|
|
def next(self):
|
2023-12-21 13:31:53 -05:00
|
|
|
self.cursor_pos += 1
|
|
|
|
if self.cursor_pos > len(self.options) - 1:
|
|
|
|
self.cursor_pos = 0
|
2023-12-21 10:18:38 -05:00
|
|
|
|
|
|
|
def prev(self):
|
2023-12-21 13:31:53 -05:00
|
|
|
self.cursor_pos -= 1
|
|
|
|
if self.cursor_pos < 0:
|
|
|
|
self.cursor_pos = len(self.options) - 1
|
|
|
|
|
|
|
|
def execute(self):
|
|
|
|
globals()[self.selected]()
|
2023-12-21 10:18:38 -05:00
|
|
|
|
|
|
|
def draw(self):
|
2023-12-21 13:15:35 -05:00
|
|
|
term_dims = os.get_terminal_size()
|
|
|
|
title_start = int((term_dims.columns / 2) - (len(self.title) / 2))
|
|
|
|
Text(self.title).draw_at(1, title_start)
|
2023-12-21 10:18:38 -05:00
|
|
|
|
2023-12-21 13:15:35 -05:00
|
|
|
for i, k in enumerate(self.options):
|
2023-12-21 13:31:53 -05:00
|
|
|
if i == self.cursor_pos:
|
2023-12-21 13:15:35 -05:00
|
|
|
Text(k).highlight().draw_at(i + 2, 1)
|
2023-12-21 13:34:02 -05:00
|
|
|
self.selected = self.options.get(k)
|
2023-12-21 10:18:38 -05:00
|
|
|
else:
|
2023-12-21 13:15:35 -05:00
|
|
|
Text(k).draw_at(i + 2, 1)
|
2023-12-21 08:33:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2023-12-21 10:59:02 -05:00
|
|
|
os.system('clear')
|
2023-12-21 12:43:38 -05:00
|
|
|
os.system('stty -echo')
|
2023-12-21 13:20:26 -05:00
|
|
|
print('\033[?25l', end='')
|
2023-12-21 12:43:38 -05:00
|
|
|
|
2023-12-21 13:15:35 -05:00
|
|
|
menu = Menu('Test Menu', {
|
|
|
|
'Option 1:': 'opt',
|
|
|
|
'Option 2:': 'opt2',
|
2023-12-21 13:31:53 -05:00
|
|
|
'Option 3:': 'opt3',
|
|
|
|
'Enter the Matrix': 'matrix'
|
2023-12-21 13:15:35 -05:00
|
|
|
})
|
2023-12-21 10:18:38 -05:00
|
|
|
menu.draw()
|
2023-12-21 08:33:05 -05:00
|
|
|
while True:
|
2023-12-21 08:46:59 -05:00
|
|
|
pressed_key = keyboard.read_event()
|
|
|
|
if pressed_key.event_type == 'down':
|
2023-12-21 10:18:38 -05:00
|
|
|
if pressed_key.name == 'j':
|
|
|
|
menu.next()
|
|
|
|
elif pressed_key.name == 'k':
|
|
|
|
menu.prev()
|
2023-12-21 13:31:53 -05:00
|
|
|
elif pressed_key.name == 'enter':
|
|
|
|
menu.execute()
|
2023-12-21 10:18:38 -05:00
|
|
|
menu.draw()
|
2023-12-21 08:33:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|