-
Notifications
You must be signed in to change notification settings - Fork 3
/
ui.py
145 lines (124 loc) · 4.57 KB
/
ui.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
134
135
136
137
138
139
140
141
142
143
144
145
import curses
class ChatUI:
def __init__(self, stdscr, userlist_width=16):
curses.use_default_colors()
for i in range(0, curses.COLORS):
curses.init_pair(i, i, -1);
self.stdscr = stdscr
self.userlist = []
self.inputbuffer = ""
self.linebuffer = []
self.chatbuffer = []
# Curses, why must you confuse me with your height, width, y, x
userlist_hwyx = (curses.LINES - 2, userlist_width - 1, 0, 0)
chatbuffer_hwyx = (curses.LINES - 2, curses.COLS-userlist_width-1,
0, userlist_width + 1)
chatline_yx = (curses.LINES - 1, 0)
self.win_userlist = stdscr.derwin(*userlist_hwyx)
self.win_chatline = stdscr.derwin(*chatline_yx)
self.win_chatbuffer = stdscr.derwin(*chatbuffer_hwyx)
self.redraw_ui()
def resize(self):
"""Handles a change in terminal size"""
u_h, u_w = self.win_userlist.getmaxyx()
h, w = self.stdscr.getmaxyx()
self.win_chatline.mvwin(h - 1, 0)
self.win_chatline.resize(1, w)
self.win_userlist.resize(h - 2, u_w)
self.win_chatbuffer.resize(h - 2, w - u_w - 2)
self.linebuffer = []
for msg in self.chatbuffer:
self._linebuffer_add(msg)
self.redraw_ui()
def redraw_ui(self):
"""Redraws the entire UI"""
h, w = self.stdscr.getmaxyx()
u_h, u_w = self.win_userlist.getmaxyx()
self.stdscr.clear()
self.stdscr.vline(0, u_w + 1, "|", h - 2)
self.stdscr.hline(h - 2, 0, "-", w)
self.stdscr.refresh()
self.redraw_userlist()
self.redraw_chatbuffer()
self.redraw_chatline()
def redraw_chatline(self):
"""Redraw the user input textbox"""
h, w = self.win_chatline.getmaxyx()
self.win_chatline.clear()
start = len(self.inputbuffer) - w + 1
if start < 0:
start = 0
self.win_chatline.addstr(0, 0, self.inputbuffer[start:])
self.win_chatline.refresh()
def redraw_userlist(self):
"""Redraw the userlist"""
self.win_userlist.clear()
h, w = self.win_userlist.getmaxyx()
for i, name in enumerate(self.userlist):
if i >= h:
break
#name = name.ljust(w - 1) + "|"
self.win_userlist.addstr(i, 0, name[:w - 1])
self.win_userlist.refresh()
def redraw_chatbuffer(self):
"""Redraw the chat message buffer"""
self.win_chatbuffer.clear()
h, w = self.win_chatbuffer.getmaxyx()
j = len(self.linebuffer) - h
if j < 0:
j = 0
for i in range(min(h, len(self.linebuffer))):
self.win_chatbuffer.addstr(i, 0, self.linebuffer[j])
j += 1
self.win_chatbuffer.refresh()
def chatbuffer_add(self, msg):
"""
Add a message to the chat buffer, automatically slicing it to
fit the width of the buffer
"""
self.chatbuffer.append(msg)
self._linebuffer_add(msg)
self.redraw_chatbuffer()
self.redraw_chatline()
self.win_chatline.cursyncup()
def _linebuffer_add(self, msg):
h, w = self.stdscr.getmaxyx()
u_h, u_w = self.win_userlist.getmaxyx()
w = w - u_w - 2
while len(msg) >= w:
self.linebuffer.append(msg[:w])
msg = msg[w:]
if msg:
self.linebuffer.append(msg)
def prompt(self, msg):
"""Prompts the user for input and returns it"""
self.inputbuffer = msg
self.redraw_chatline()
res = self.wait_input()
res = res[len(msg):]
return res
def wait_input(self, prompt=""):
"""
Wait for the user to input a message and hit enter.
Returns the message
"""
self.inputbuffer = prompt
self.redraw_chatline()
self.win_chatline.cursyncup()
last = -1
while last != ord('\n'):
last = self.stdscr.getch()
if last == ord('\n'):
tmp = self.inputbuffer
self.inputbuffer = ""
self.redraw_chatline()
self.win_chatline.cursyncup()
return tmp[len(prompt):]
elif last == curses.KEY_BACKSPACE or last == 127:
if len(self.inputbuffer) > len(prompt):
self.inputbuffer = self.inputbuffer[:-1]
elif last == curses.KEY_RESIZE:
self.resize()
elif 32 <= last <= 126:
self.inputbuffer += chr(last)
self.redraw_chatline()