-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_terminal.py
executable file
·133 lines (112 loc) · 4.75 KB
/
main_terminal.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
import os, curses, itertools, time, queue, atexit
import link
from housepy import config, log, util
CHARACTERS = config['characters']
receiver = link.Receiver(23232)
sender = link.Sender(23234)
try:
messages = util.load("transcript.pkl")
except Exception:
messages = []
spinner = itertools.cycle(['—', '\\', '|', '/'])
ready = config['start']
current = []
def curses_main(args):
global ready, receiver, sender, current
try:
w = curses.initscr() # initialize
curses.use_default_colors() # use the terminal settings for color_pair 0, otherwise it's black and white
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_WHITE) # define some other color pairs
curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_YELLOW)
w.bkgd(" ", curses.color_pair(1)) # use a custom for the default
# curses.echo() # show what's being typed
curses.noecho() # or dont
while True:
# draw chat history
LINES, COLUMNS = w.getmaxyx()
for i in range(LINES - 1):
if i < len(messages):
message = messages[-1 * (i + 1)]
index = messages.index(message)
color = 1 if message[0] == 0 else 2
w.addstr(LINES - i - 2, 0, " " + message[1], curses.A_BOLD | curses.color_pair(color)) # draw the line
else:
w.addstr(LINES - i - 2, 0, "")
w.clrtoeol() # erase the rest of it
# take input
if ready:
curses.flushinp()
w.addstr(LINES - 1, 0, "> %s" % "".join(current), curses.color_pair(1)) # display something
w.clrtoeol()
curses.curs_set(1)
ch = w.getch()
if ch != 10:
if ch == 127 or ch == 8:
if len(current):
current.pop()
# elif ch < 128 and len(current) < 30: # restrict to ascii. make it 256 if you need latin.
elif len(current) < 30 and chr(ch).upper() in CHARACTERS:
current.append(chr(ch).upper())
else:
message_s = "".join(current).strip()
if message_s == "MARADONA":
exit()
elif message_s == "UNDO":
if len(messages):
messages.pop()
current = []
elif len(message_s):
messages.append((0, message_s))
sender.messages.put(message_s)
current = []
flush_messages()
ready = False
# w.addstr(LINES - 1, 0, "> %s" % "".join(current), curses.color_pair(1)) # display something
w.clrtoeol() # erase from cursor to end of line
w.refresh()
# get a response
else:
curses.curs_set(0)
w.addstr(LINES - 1, 0, "> " + "".join(current) + " " + next(spinner), curses.A_REVERSE | curses.color_pair(2))
w.clrtoeol()
w.refresh()
time.sleep(0.1)
c = get_message()
if c is not None:
if c == "DONE":
messages.append((1, "".join(current)))
ready = True
current = []
elif c == "ERASE":
if len(current):
current.pop()
else:
current.append(c)
except Exception as e:
log.error(log.exc(e))
# raise e
def get_message():
message = None
while True:
try:
message = receiver.messages.get_nowait()
except queue.Empty:
break
return message
def flush_messages():
while True:
try:
receiver.messages.get_nowait()
except queue.Empty:
break
def exit_handler():
util.save("transcript.pkl", messages)
print("Exiting...")
atexit.register(exit_handler)
curses.wrapper(curses_main)
"""
disable control-c?
it should cache the whole conversation and load it on startup (via an argument)
"""