terminal-emulator/terminal/__main__.py

71 lines
1.8 KiB
Python
Raw Normal View History

2023-12-21 08:33:05 -05:00
import keyboard
import time
2023-12-21 10:18:38 -05:00
import os
2023-12-21 12:43:38 -05:00
HIGHLIGHTED = '\033[38;5;0m\033[48;5;2m'
REGULAR = '\033[38;5;2m'
2023-12-21 10:18:38 -05:00
def put_cursor(line, col):
return f'\033[{int(line)};{int(col)}f'
class Menu:
def __init__(self, start_line):
self.start_line = start_line
self.options = [
'test 1',
'test 2',
'test 3'
]
self.selected = 0
2023-12-21 10:53:21 -05:00
self.highlight = '\033[38;5;0m\033[48;5;2m'
self.regular = '\033[38;5;2m'
2023-12-21 10:18:38 -05:00
def next(self):
self.selected += 1
if self.selected > len(self.options) - 1:
self.selected = 0
def prev(self):
self.selected -= 1
if self.selected < 0:
self.selected = len(self.options) - 1
def draw(self):
term_size = os.get_terminal_size()
current_line = self.start_line
for i, option in enumerate(self.options):
print(f'{put_cursor(self.start_line + i, 0)}\033[2;0;0m', end='')
2023-12-21 12:43:38 -05:00
print('' * term_size.columns, end='\r')
2023-12-21 10:18:38 -05:00
if i == self.selected:
2023-12-21 12:43:38 -05:00
print(f'{self.highlight}{option}')
2023-12-21 10:18:38 -05:00
else:
2023-12-21 12:43:38 -05:00
print(f'{self.regular}{option}')
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')
term_size = os.get_terminal_size()
print('\033[?25l', end='')
title = '[Interactive Text Menu]'
title_col_start = int((term_size.columns / 2) - (len(title) / 2))
print(f'{put_cursor(0, title_col_start)}{REGULAR}{title}', end='\n')
2023-12-21 10:18:38 -05:00
menu = Menu(10)
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()
menu.draw()
2023-12-21 08:33:05 -05:00
if __name__ == '__main__':
main()