made a class for text
This commit is contained in:
parent
ac8653783d
commit
8f459b072e
@ -1,26 +1,35 @@
|
|||||||
import keyboard
|
import keyboard
|
||||||
import time
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
HIGHLIGHTED = '\033[38;5;0m\033[48;5;2m'
|
TEXT_REGULAR = '\033[38;5;2m'
|
||||||
REGULAR = '\033[38;5;2m'
|
TEXT_HIGHLIGHTED = '\033[38;5;0m\033[48;5;2m'
|
||||||
|
RESET_CODE = '\033[2;0;0m'
|
||||||
|
|
||||||
|
|
||||||
def put_cursor(line, col):
|
class Text:
|
||||||
return f'\033[{int(line)};{int(col)}f'
|
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')
|
||||||
|
|
||||||
|
|
||||||
class Menu:
|
class Menu:
|
||||||
def __init__(self, start_line):
|
def __init__(self, title: str, options: dict):
|
||||||
self.start_line = start_line
|
self.title = f'[{title}]'
|
||||||
self.options = [
|
self.options = options
|
||||||
'test 1',
|
|
||||||
'test 2',
|
|
||||||
'test 3'
|
|
||||||
]
|
|
||||||
self.selected = 0
|
self.selected = 0
|
||||||
self.highlight = '\033[38;5;0m\033[48;5;2m'
|
|
||||||
self.regular = '\033[38;5;2m'
|
|
||||||
|
|
||||||
def next(self):
|
def next(self):
|
||||||
self.selected += 1
|
self.selected += 1
|
||||||
@ -33,28 +42,26 @@ class Menu:
|
|||||||
self.selected = len(self.options) - 1
|
self.selected = len(self.options) - 1
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
term_size = os.get_terminal_size()
|
term_dims = os.get_terminal_size()
|
||||||
current_line = self.start_line
|
title_start = int((term_dims.columns / 2) - (len(self.title) / 2))
|
||||||
|
Text(self.title).draw_at(1, title_start)
|
||||||
|
|
||||||
for i, option in enumerate(self.options):
|
for i, k in enumerate(self.options):
|
||||||
print(f'{put_cursor(self.start_line + i, 0)}\033[2;0;0m', end='')
|
|
||||||
print('' * term_size.columns, end='\r')
|
|
||||||
if i == self.selected:
|
if i == self.selected:
|
||||||
print(f'{self.highlight}• {option}')
|
Text(k).highlight().draw_at(i + 2, 1)
|
||||||
else:
|
else:
|
||||||
print(f'{self.regular}• {option}')
|
Text(k).draw_at(i + 2, 1)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
os.system('clear')
|
os.system('clear')
|
||||||
os.system('stty -echo')
|
os.system('stty -echo')
|
||||||
term_size = os.get_terminal_size()
|
|
||||||
|
|
||||||
print('\033[?25l', end='')
|
menu = Menu('Test Menu', {
|
||||||
title = '[Interactive Text Menu]'
|
'Option 1:': 'opt',
|
||||||
title_col_start = int((term_size.columns / 2) - (len(title) / 2))
|
'Option 2:': 'opt2',
|
||||||
print(f'{put_cursor(0, title_col_start)}{REGULAR}{title}', end='\n')
|
'Option 3:': 'opt3'
|
||||||
menu = Menu(10)
|
})
|
||||||
menu.draw()
|
menu.draw()
|
||||||
while True:
|
while True:
|
||||||
pressed_key = keyboard.read_event()
|
pressed_key = keyboard.read_event()
|
||||||
|
Loading…
Reference in New Issue
Block a user