-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
323 lines (259 loc) · 8.58 KB
/
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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
from prompt_toolkit import Application
from prompt_toolkit.enums import EditingMode
from prompt_toolkit.buffer import Buffer
from prompt_toolkit.document import Document
from prompt_toolkit.widgets import Frame, FormattedTextToolbar, Label
from prompt_toolkit.layout.containers import VSplit, Window, HSplit
from prompt_toolkit.layout.controls import BufferControl
from prompt_toolkit.layout.layout import Layout
from prompt_toolkit.layout import FormattedTextControl
from prompt_toolkit.formatted_text import HTML, merge_formatted_text
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.key_binding.bindings.named_commands import accept_line
from prompt_toolkit.key_binding.bindings import scroll
from prompt_toolkit.layout.margins import ScrollbarMargin
from prompt_toolkit.key_binding.vi_state import InputMode, ViState
from prompt_toolkit.styles import Style
from prompt_toolkit.output import Output
from collections import namedtuple
from fbchat import Message
import fbserver
import websockets
import fbchat
import pickle
import jsonpickle
import comms
import sys
import pymess
import asyncio
from collections import OrderedDict
import tracemalloc
tracemalloc.start()
conversations = {}
curr_convo = None
chat_area = True
kb = KeyBindings()
app = None
ws_handler = None
class handler:
def __init__(self, URL):
self.ws = None
self.URL = URL
async def connect(self):
self.ws = await websockets.connect(self.URL)
async def listen(self):
while True:
try:
async for message in self.ws:
# print(type(read_command((*jsonpickle.decode(message)))))
read_command(*jsonpickle.decode(message))
except Exception as e:
print(e)
print("Lost connection to server")
break
async def command(self, cmd):
await self.ws.send(jsonpickle.encode(cmd))
def read_command(command, data):
if command == 'rcv_all_convo':
update_conversation(list(data.values()))
if command == 'rcv_convo':
update_conversation(data)
def update_conversation(recv_conversations):
global conversations
global curr_convo
global log_buf
if type(recv_conversations) is not list:
recv_conversations = [recv_conversations]
for conversation in recv_conversations:
Conversation = namedtuple('Conversation', list(
conversation.keys()))
convo_obj = Conversation(**conversation)
if curr_convo is None:
curr_convo = convo_obj
conversations.update({convo_obj.thread.uid: convo_obj})
if curr_convo.thread.uid == convo_obj.thread.uid:
curr_convo = convo_obj
update_buffer()
update_status()
auto_scroll(log_buf)
return
def vi_mode_to_cursor(mode):
return {InputMode.NAVIGATION: 1, InputMode.REPLACE: 3}.get(mode, 5)
def get_input_mode(self):
if sys.version_info[0] == 3:
global app
# Decrease input flush timeout from 500ms to 10ms.
from prompt_toolkit.application.current import get_app
app = get_app()
app.ttimeoutlen = 0.01
return self._input_mode
def toggle_convo_list():
global chat_area
if chat_area:
app.layout = Layout(root)
app.layout.focus(convos)
chat_area = False
change_cursor(1)
else:
app.layout = Layout(chat_container)
app.layout.focus(input_buf)
chat_area = True
change_cursor(vi_mode_to_cursor(get_input_mode))
@ kb.add('tab')
def toggle_convo_list_(event):
toggle_convo_list()
def change_cursor(shape):
cursor = "\x1b[{} q".format(shape)
if hasattr(sys.stdout, '_cli'):
write = sys.stdout._cli.output.write_raw
else:
write = sys.stdout.write
write(cursor)
sys.stdout.flush()
def set_input_mode(self, mode):
shape = {InputMode.NAVIGATION: 1, InputMode.REPLACE: 3}.get(mode, 5)
change_cursor(shape)
self._input_mode = mode
def vi_movement_mode(event):
event.cli.key_processor.feed(KeyPress(Keys.Escape))
@ kb.add('c-q')
def exit_(event):
event.app.exit()
def accept_message(input_buff):
if input_buff.text != "":
input_win.height = 1
asyncio.ensure_future(
ws_handler.command(("msg_out", [
input_buff.text, curr_convo.thread.uid, curr_convo.thread.type])))
# ws_handler.command(('get_convo', 'all')))
# asyncio.get_event_loop().create_task(ws_handler.command(("msg_out", [
# input_buff.text, curr_convo.thread.uid, curr_convo.thread.type])))
def auto_scroll(buff):
buff.cursor_position = len(buff.text)-1
def resize_input(buff):
input_win.height = buff.text.count("\n") + 1
@ kb.add('enter')
def _(event):
if event.app.layout.current_buffer == input_buf:
accept_line(event)
event.app.invalidate()
def update_buffer():
log_buf.text = "\n".join(curr_convo.buffer)
def update_status():
status_bar.content.text = HTML("<b>"+curr_convo.name+"</b>")
class convo_list_widget:
def __init__(self):
self.selected_line = 0
self.container = Window(
content=FormattedTextControl(
text=self._get_formatted_text,
focusable=True,
key_bindings=self._get_key_bindings(),
),
style="class:select-box",
cursorline=True,
right_margins=[ScrollbarMargin(display_arrows=True), ],
width=20,
always_hide_cursor=True,
)
def _get_formatted_text(self):
result = []
for i, convs in enumerate(conversations.values()):
name = convs.name
if i == self.selected_line:
result.append([("[SetCursorPosition]", "")])
result.append(name)
result.append("\n")
return merge_formatted_text(result)
def _get_key_bindings(self):
kb = KeyBindings()
@ kb.add("k")
def _go_up(event) -> None:
self.selected_line = (self.selected_line - 1) % len(conversations)
app.output.hide_cursor()
@ kb.add("j")
def _go_down(event) -> None:
self.selected_line = (self.selected_line + 1) % len(conversations)
app.output.hide_cursor()
@ kb.add('enter')
def _(event):
global curr_convo
curr_convo = list(conversations.values())[self.selected_line]
update_status()
update_buffer()
toggle_convo_list()
return kb
def __pt_container__(self):
return self.container
root = None
chat_container = None
app = None
log_buf = None
log_win = None
input_buf = None
input_win = None
convos = None
status_bar = None
status_label = None
client = None
async def init():
global root
global chat_container
global app
global log_buf
global log_win
global input_buf
global input_win
global convos
global status_bar
global status_label
global client
global convo_stack
global websocket
global app
global uri
global websocket
global ws_handler
uri = "ws://localhost:15555"
ws_handler = handler(uri)
await ws_handler.connect()
# message area
log_buf = Buffer(document=Document())
log_win = Window(BufferControl(log_buf), wrap_lines=True)
# input area
input_buf = Buffer(document=Document())
input_win = Window(BufferControl(input_buf), height=1, wrap_lines=True)
# status bar
status_bar = FormattedTextToolbar(
text=HTML("<b>Chatting with: Loading </b>"), style="bg:ansired fg:ansiblack")
status_label = Label(text="[ 00:29 ] ", width=10)
# call backs
input_buf.accept_handler = accept_message
input_buf.on_text_changed += resize_input
log_buf.on_text_changed += auto_scroll
convos = convo_list_widget()
chat_container = HSplit([
log_win,
status_bar,
VSplit([
status_label,
input_win])
])
root = VSplit([
convos,
chat_container,
])
style = Style.from_dict(
{"select-box cursor-line": "nounderline bg:ansired fg:ansiwhite"})
app = Application(editing_mode=EditingMode.VI, key_bindings=kb, layout=Layout(
chat_container), full_screen=True, style=style)
app.invalidate()
app.layout.focus(input_buf)
ViState._input_mode = InputMode.INSERT
ViState.input_mode = property(get_input_mode, set_input_mode)
asyncio.ensure_future(
ws_handler.listen())
asyncio.ensure_future(
ws_handler.command(('get_convo', 'all')))
auto_scroll(log_buf)
await app.run_async()