-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathterminal_gui.py
106 lines (79 loc) · 2.85 KB
/
terminal_gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import curses
HIGHLIGHTED_COLOR_ID = 1
TEXT_COLOR_ID = 2
def print_wrapper(stdscr):
stdscr.clear()
h, w = stdscr.getmaxyx()
h = h // 2
w = w // 2
# Init colors
curses.init_pair(HIGHLIGHTED_COLOR_ID, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(TEXT_COLOR_ID, curses.COLOR_WHITE, curses.COLOR_BLACK)
# Draw border
stdscr.border()
# Print title
title = "Research Chain"
title_id = 1
stdscr.addstr(title_id, w - len(title) // 2, title)
return h, w
def print_menu(stdscr, selected_row_idx, options):
h, w = print_wrapper(stdscr)
for idx, option in enumerate(options):
x = w - len(option) // 2
y = h - len(options) // 2 + idx
if idx == selected_row_idx:
stdscr.attron(curses.color_pair(HIGHLIGHTED_COLOR_ID))
stdscr.addstr(y, x, option)
stdscr.attroff(curses.color_pair(HIGHLIGHTED_COLOR_ID))
else:
stdscr.attron(curses.color_pair(TEXT_COLOR_ID))
stdscr.addstr(y, x, option)
stdscr.attroff(curses.color_pair(TEXT_COLOR_ID))
stdscr.refresh()
def select_input(stdscr):
curses.curs_set(0) # Hide cursor
stdscr.keypad(True) # Enable keypad for non-character keys
options = ["News", "Docs", "Wiki", "Exit"]
selected_row_idx = 0
print_menu(stdscr, selected_row_idx, options)
while True:
key = stdscr.getch()
if key == curses.KEY_UP:
selected_row_idx = max(0, selected_row_idx - 1)
elif key == curses.KEY_DOWN:
selected_row_idx = min(len(options) - 1, selected_row_idx + 1)
elif key in [curses.KEY_ENTER, 10, 13]:
if selected_row_idx == len(options) - 1:
exit()
else:
break
print_menu(stdscr, selected_row_idx, options)
return options[selected_row_idx]
def print_input_field(stdscr, text_input_value):
stdscr.clear()
h, w = print_wrapper(stdscr)
# Print text input field
stdscr.addstr(h, w - 20, "Enter Text:")
stdscr.attron(curses.color_pair(HIGHLIGHTED_COLOR_ID))
stdscr.addstr(h, w - 8, text_input_value)
stdscr.attroff(curses.color_pair(HIGHLIGHTED_COLOR_ID))
stdscr.refresh()
def user_input(stdscr):
def get_input():
text = ""
print_input_field(stdscr, text)
while True:
char = stdscr.getch()
if char in [curses.KEY_ENTER, 10, 13]:
break
elif char in [curses.KEY_BACKSPACE, 8, 127]:
text = text[:-1]
elif 32 <= char <= 126:
text += chr(char)
print_input_field(stdscr, text)
return text
curses.curs_set(1) # Show cursor
stdscr.keypad(True) # Enable keypad for non-character keys
text_input_value = get_input()
curses.endwin() # End curses window
return text_input_value