Repository navigation
Expand file tree
/
Copy pathtui_frame.py
More file actions
456 lines (388 loc) · 16.4 KB
/
Copy pathtui_frame.py
File metadata and controls
456 lines (388 loc) · 16.4 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# This is the main application "window manager"; it manages the UI
# from a high level and holds everything together.
import asyncio
import copy
import datetime
import logging
import json
import os
import time
import urwid
from ...config import APPNAME, APPVER
from ...api.requests import *
from ..suggestions import Suggestion, SuggestionWelcome
from .decorations import EMOJI
from .browser import Browser
from .changepassdialog import ChangePassDialog
from .contextlist import ContextList
from .emaillist import EmailList
from .retrydialog import RetryDialog
from .searchdialog import SearchDialog
from .suggestionbox import SuggestionBox
from .unlockdialog import UnlockDialog
from .widgets import *
def _w(w, attr={}, valign='top'):
return urwid.AttrWrap(urwid.Filler(w, valign=valign), attr)
class TuiFrame(urwid.Frame):
current_context = property(lambda s: s.context_list.active['key'])
def __init__(self, screen, conn_manager):
self.screen = screen
self.is_locked = True
self.was_locked = True
self.user_moved = False
self.render_cols_rows = self.screen.get_cols_rows()
self.conn_manager = conn_manager
suggestions = SuggestionBox(self,
fallbacks=[SuggestionWelcome],
max_suggestions=5)
self.filler1 = SplashCat(suggestions, 'Welcome to Moggie!')
self.filler2 = SplashMoreWide()
self.filler3 = SplashMoreNarrow()
self.hidden = 0
self.crumbs = []
self.notifications = []
self.columns = urwid.Columns([self.filler1], dividechars=1)
self.context_list = ContextList(self,
first=False, update_parent=self.update_columns)
self.all_columns = [self.context_list]
self.topbar_pile = urwid.Pile([])
self.topbar = PopUpManager(self, self.topbar_pile)
self.update_columns(update=False)
urwid.Frame.__init__(self, self.columns, header=self.topbar)
self.contents['header'] = (self.topbar, None)
loop = asyncio.get_event_loop()
loop.create_task(self.topbar_clock())
conn_manager.add_handler('tui', '*', '*', self.handle_bridge_messages)
def set_initial_state(self, initial_state):
self.is_locked = initial_state.get('app_is_locked')
self.was_locked = self.is_locked
show_draft = initial_state.get('show_draft')
show_browser = initial_state.get('show_browser')
show_mailbox = initial_state.get('show_mailbox')
if show_draft:
# Display the composer; whether we are locked or not.
# But what happens to any composed mail will vary!
pass # FIXME
elif show_browser:
self.show_browser(show_browser, history=False)
elif show_mailbox:
self.show_mailbox(show_mailbox)
else:
# What the default view is, depends on what the context
# has configured. Let the ContextList figure it out?
# Or is that terrible UX and we value consistency?
# self.context_list.activate_default_view()
self.columns.set_focus_path([1]) # Focus the cat!
async def topbar_clock(self):
while True:
self.update_topbar()
if self.notifications:
await asyncio.sleep(0.25)
else:
await asyncio.sleep(1)
expired = time.time() - 20
self.notifications = [
n for n in self.notifications if n['ts'] > expired]
def send_with_context(self, message_obj, context=None):
return self.context_list.send_with_context(message_obj, context)
def handle_bridge_messages(self, bridge_name, message):
#logging.debug('Incoming(%s): %s' % (bridge_name, message))
try:
for widget in self.all_columns:
if hasattr(widget, 'handle_bridge_messages'):
try:
widget.handle_bridge_messages(bridge_name, message)
except:
logging.exception('Incoming message asploded')
if 'error' in message and 'exception' in message:
# This only applies if we have a NeedInfo ?
self.topbar.open_with(RetryDialog, message)
elif message.get('req_type') in ('notification', 'unlocked'):
if message['req_type'] == 'unlocked':
self.is_locked = False
self.context_list.activate_default_view()
self.notifications.append(message)
except:
logging.exception('Exception handling message: %s' % (message,))
def set_context(self, i):
self.context_list.expand(i)
self.update_columns()
def show_browser(self, which=True, context=None, history=True):
ctx_id, ctx_src_id = self.context_list.get_context_and_src_ids(context)
self.col_show(self.all_columns[0], Browser(self, ctx_src_id, which))
if history:
label = 'Browse' if which is True else os.path.basename(which)
self.context_list.add_history(
label,
lambda: self.show_browser(which, context),
icon=EMOJI.get('browsing', '-'))
def show_mailbox(self, which, context=None, history=True, keep=None):
_, ctx_src_id = self.context_list.get_context_and_src_ids(context)
terms = 'mailbox:%s' % which
self.col_show(
keep or self.all_columns[0],
EmailList(self, ctx_src_id, terms))
if history:
self.context_list.add_history(
os.path.basename(which),
lambda: self.show_mailbox(which, context),
icon=EMOJI.get('mailbox', '-'))
def show_search_result(self, terms, context=None, history=True):
# FIXME: The app should return an error and we retry
if self.is_locked:
self.topbar.open_with(UnlockDialog)
return
_, ctx_src_id = self.context_list.get_context_and_src_ids(context)
self.col_show(self.all_columns[0], EmailList(self, ctx_src_id, terms))
if history:
self.context_list.add_history(
terms,
lambda: self.show_search_result(terms, context, True),
icon=EMOJI.get('search', '-'))
def ui_quit(self):
raise urwid.ExitMainLoop()
def unlock(self, passphrase):
logging.info('Passphrase supplied, attempting unlock')
self.conn_manager.send(RequestUnlock(passphrase))
def ui_change_passphrase(self):
self.topbar.open_with(ChangePassDialog)
def change_passphrase(self, old_passphrase, new_passphrase,
disconnect=False):
logging.info(
'New passphrase supplied, requesting change (disconnect=%s)'
% (disconnect,))
self.conn_manager.send(RequestChangePassphrase(
old_passphrase, new_passphrase,
disconnect=disconnect))
def max_child_rows(self):
return self.screen.get_cols_rows()[1] - 2
def render(self, *args, **kwargs):
# This lets us adapt our display to screen width;
# hiding or showing columns as necessary.
cols_rows = self.screen.get_cols_rows()
if self.render_cols_rows != cols_rows:
self.render_cols_rows = cols_rows
for wdgt in self.all_columns:
if hasattr(wdgt, 'update_content'):
wdgt.update_content()
self.update_columns()
return urwid.Frame.render(self, *args, **kwargs)
def locked_emoji(self):
return (' %s' % EMOJI.get('lock', '!')) if self.is_locked else ''
def update_topbar(self, update=True):
# FIXME: Calculate/hint hotkeys based on what our columns suggest?
maxwidth = self.render_cols_rows[0] - 2
now = time.time()
self.crumbs = []
for widget in self.all_columns:
if hasattr(widget, 'crumb'):
self.crumbs.append(widget.crumb)
crumbs = copy.copy(self.crumbs)
for i, crumb in enumerate(crumbs):
if i < len(self.crumbs)-1 and maxwidth < 100:
if crumb.endswith(')'):
crumbs[i] = crumb = crumb.rsplit(' (', 1)[0]
if '/' in crumb:
crumbs[i] = crumb.rsplit('/')[-1]
crumbshift = max(0, 17 - len(crumbs[0] if crumbs else ''))
crumbtrail = (' ' * crumbshift) + ': '.join(crumbs)
crumblen = len(crumbtrail)
pad = ' ' if maxwidth > 80 else ''
global_hks = []
for col in self.all_columns:
if hasattr(col, 'global_hks'):
for hk in col.global_hks.values():
if isinstance(hk, list):
global_hks.extend(hk[1:])
column_hints = []
fpath = self.columns.get_focus_path()
try:
wdgt = self.all_columns[self.hidden + fpath[0]]
if hasattr(wdgt, 'column_hks'):
hks = wdgt.column_hks
if not isinstance(hks, list):
hks = hks()
if hks:
tw = urwid.Text(hks)
column_hints.append(('fixed', len(tw.text), tw))
except IndexError:
pass
ntime = datetime.datetime.now()
if maxwidth > 150:
cfmt = '%s'
clock_a = clock_b = ' %A, %Y-%m-%d %H:%M:%S'
elif maxwidth > 115:
cfmt = '%s'
clock_a = clock_b = ' %a %Y-%m-%d %H:%M:%S'
elif maxwidth > 84:
cfmt = '%14s'
clock_a = '%a %H:%M:%S'
clock_b = '%a %Y-%m-%d'
elif maxwidth > 74:
cfmt = '%10s'
clock_a = '%a %H:%M'
clock_b = '%Y-%m-%d'
else:
cfmt = '%5s'
clock_a = '%H:%M'
clock_b = '%a '
if (now // 8) % 3 == 1:
clock = cfmt % ntime.strftime(clock_b) + self.locked_emoji()
else:
clock = cfmt % ntime.strftime(clock_a) + self.locked_emoji()
global_hints = []
nage = 0
if self.notifications:
nage = now - self.notifications[-1]['ts']
if 0 < nage <= 30:
msg = self.notifications[-1]['message']
global_hints = [('weight', len(msg),
urwid.Text(msg, align='left', wrap='ellipsis'))]
else:
nage = 0
global_hints.append(('weight', len(clock),
urwid.Text(('subtle', clock), align='center')))
if not nage or (maxwidth > 70 + 8*(3+len(global_hks))):
# FIXME: Calculate actual width and use that.
search = [] if self.is_locked else [('top_hk', '/:'), 'Search ']
search = [] # FIXME
unlock = [('top_hk', '/:'), 'Unlock '] if self.is_locked else []
global_hints.extend([
('fixed', 23+6*len(global_hks), urwid.Text(
global_hks + search + unlock + [
#FIXME: ('top_hk', '?:'), 'Help ',
('top_hk', 'q:'), 'Exit'+pad],
align='right', wrap='clip'))])
mv = '%s%s v%s ' % (pad, APPNAME, APPVER)
_p = lambda w: (w, ('pack', None))
self.topbar_pile.contents = [
_p(urwid.AttrMap(urwid.Columns([
('fixed', len(mv), urwid.Text(mv, align='left')),
] + global_hints), 'header')),
_p(urwid.AttrMap(urwid.Columns([
('weight', crumblen, urwid.Text(crumbtrail, wrap='ellipsis'))
] + column_hints, dividechars=1), 'crumbs'))]
#if update:
# self.contents['header'] = (self.topbar, None)
def focus_last_column(self):
try:
last = len(self.all_columns) - self.hidden - 1
self.columns.set_focus_path([last])
except IndexError:
pass #logging.exception('Focus last failed')
def col_show(self, ref, widget):
logging.debug('Adding %s' % widget)
self.col_remove(ref, ofs=1, update=False)
self.all_columns.append(widget)
self.update_columns()
self.focus_last_column()
def col_replace(self, ref, widget):
self.col_remove(ref, update=False)
self.all_columns.append(widget)
self.update_columns()
self.focus_last_column()
def col_remove(self, ref, ofs=0, update=True):
pos = self.all_columns.index(ref)
if pos >= 0:
pos += ofs
if pos > 0:
for widget in self.all_columns[pos:]:
if hasattr(widget, 'cleanup'):
widget.cleanup()
self.all_columns[pos:] = []
if update:
self.update_columns()
self.focus_last_column()
def update_columns(self, update=True):
cols, rows = self.screen.get_cols_rows()
try:
focus_path = self.get_focus_path()
except AttributeError:
focus_path = None
self.hidden = 0
widgets = []
widgets.extend(self.all_columns)
while sum(col.COLUMN_NEEDS for col in widgets) > cols:
widgets = widgets[1:]
self.hidden += 1
focus_path = None
# Add our cute fillers, if we have screen real-estate to burn.
used = sum(col.COLUMN_NEEDS for col in widgets)
if used + self.filler1.COLUMN_NEEDS < cols and (len(widgets) < 2):
widgets.append(self.filler1)
used += self.filler1.COLUMN_NEEDS
if used + self.filler2.COLUMN_NEEDS < cols and (len(widgets) < 3):
widgets.append(self.filler2)
used += self.filler2.COLUMN_NEEDS
if used + self.filler3.COLUMN_NEEDS < cols:
widgets.append(self.filler3)
used += self.filler3.COLUMN_NEEDS
def _b(w):
if hasattr(w, 'rows'):
widget = _w(w, w.COLUMN_STYLE)
else:
widget = _w(urwid.BoxAdapter(w, rows-2), w.COLUMN_STYLE)
return (w.COLUMN_FIT, w.COLUMN_WANTS, widget)
columns = [_b(c) for c in widgets]
self.columns = urwid.Columns(columns, dividechars=1)
self.update_topbar(update=update)
if update:
self.contents['body'] = (self.columns, None)
try:
if focus_path:
self.set_focus_path(focus_path)
except IndexError:
pass
def keypress(self, size, key):
if key in ('q', 'esc', 'left', 'right', 'up', 'down'):
self.user_moved = True
try:
return super().keypress(size, key)
except AttributeError:
logging.exception('FIXME: Urwid bug in keypress handler?')
self.focus_last_column()
def unhandled_input(self, key):
try:
if key in ('q', 'esc', 'left', 'right', 'up', 'down'):
self.user_moved = True
cols_rows = self.screen.get_cols_rows()
if key == 'Q':
self.ui_quit()
elif key in ('q', 'esc', 'backspace'):
if len(self.all_columns) > 1:
self.col_remove(self.all_columns[-1])
elif key == 'q':
self.ui_quit()
elif key == 'left':
if len(self.all_columns) > 1 and self.hidden:
self.col_remove(self.all_columns[-1])
elif key == 'right':
self.columns.keypress(cols_rows, 'enter')
# FIXME: Searching or unlocking is a global thing
elif key == '/':
if self.is_locked:
self.topbar.open_with(UnlockDialog)
else:
self.topbar.open_with(SearchDialog)
# FIXME: This definitely belongs elsewhere!
elif key == 'C':
self.ui_change_passphrase()
# hjkl navigation
elif key == 'h':
if len(self.all_columns) > 1 and self.hidden:
self.col_remove(self.all_columns[-1])
else:
self.columns.keypress(cols_rows, 'left')
elif key == 'j':
self.columns.keypress(cols_rows, 'down')
elif key == 'k':
self.columns.keypress(cols_rows, 'up')
elif key == 'l':
self.columns.keypress(cols_rows, 'right')
else:
for col in self.all_columns:
if hasattr(col, 'global_hks') and key in col.global_hks:
return col.keypress(cols_rows, key)
return key
except IndexError:
return key