forked from marcdjulien/spotify-terminal
/
display.py
493 lines (404 loc) · 17.3 KB
/
display.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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import time
import unicurses as uc
import common
logger = common.logging.getLogger(__name__)
class Window(object):
"""Wrapper around a curses window."""
def __init__(self, name, window):
self.name = name
self.window = window
class CursesDisplay(object):
# How often to run the program loop.
PROGRAM_PERIOD = 0.01
# How often to re-render.
# For now, this is the same as the program loop.
# Will need to investigate the performance impact of this.
RENDER_PERIOD = PROGRAM_PERIOD
# How often to clear the screen.
CLEAR_PERIOD = 60 * 15
def __init__(self, stdscr, sp_state):
self.state = sp_state
"""The SpotifyState object."""
self.stdscr = stdscr
"""The unicurses standard window."""
self._panels = {}
"""Panels."""
self._windows = {}
"""Windows."""
self._running = True
"""Whether to continue running."""
self._ordered_windows = []
"""Windows in an odered list."""
self._register_window("popup", self._window_sizes["popup"])
self._register_window("search_results", self._window_sizes["search"])
self._register_window("select_device", self._window_sizes["select_device"])
self._register_window("user", self._window_sizes["user"])
self._register_window("tracks", self._window_sizes["tracks"])
self._register_window("player", self._window_sizes["player"])
self.periodics = [
common.PeriodicCallback(self.RENDER_PERIOD, self.render),
common.PeriodicCallback(self.CLEAR_PERIOD, self.clear),
]
# This increments each control loop. A value of -50 means that we'll have
# 2s (200 * PROGRAM_LOOP) until the footer begins to roll.
self._footer_roll_index = -200
# Initialize the display.
self._init_curses()
def _register_window(self, name, size):
"""Register a window
Args:
size (tuple): The size of the window.
name (str): The name of the window.
"""
self._windows[name] = Window(name, uc.newwin(*size))
self._panels[name] = uc.new_panel(self._windows[name].window)
self._ordered_windows.append(self._windows[name])
def _resize_window(self, name, size):
"""Register a window
Args:
size (tuple): The size of the window.
name (str): The name of the window.
"""
uc.wresize(self._windows[name].window, size[0], size[1])
uc.wmove(self._windows[name].window, size[2], size[3])
def _init_curses(self):
"""Initialize the curses environment and windows."""
for win in self.get_windows():
# Make getch and getstr non-blocking.
uc.nodelay(win, True)
# Allow non-ascii keys.
uc.keypad(win, True)
# Don't echo text.
uc.noecho()
# Don't show the cursor.
uc.curs_set(False)
logger.debug("Curses display initialized")
def start(self):
logger.info("Starting curses display loop")
# Initial render.
self.render()
while self._running:
# Time how long it takes to process input
# and render the screen.
with common.ContextDuration() as t:
# Handle user input.
self.process_input()
# Do any calculations related to rendering.
self.render_calcs()
# Execute the periodic call backs.
for periodic in self.periodics:
periodic.update(time.time())
# Are we still running?
self._running = self.state.is_running()
# Sleep for an amount of time.
sleep_time = self.PROGRAM_PERIOD - t.duration
if sleep_time > 0:
time.sleep(sleep_time)
# Tear down the display.
logger.debug("Tearing down curses display")
uc.endwin()
common.clear()
def process_input(self):
"""Process all keyboard input."""
key_pressed = False
for win in self.get_windows():
key = uc.wgetch(win)
if key != -1:
key_pressed = True
self.state.process_key(key)
# If we didn't press a key, kick the state anyway.
if not key_pressed:
self.state.process_key(None)
def render_calcs(self):
"""Perform any calculations related to rendering."""
def render(self):
# Set the panel order based on what action is going on.
self.set_panel_order()
# Update the panel size incase the terminal size changed.
self.update_panel_size()
# Clear the screen.
uc.erase()
# Draw all of the panels.
self.render_user_panel()
self.render_track_panel()
self.render_player_panel()
self.render_footer()
self.render_search_panel()
self.render_select_device_panel()
self.render_popup_panel()
# Required.
uc.update_panels()
uc.doupdate()
def clear(self):
uc.erase()
uc.move(0, 0)
uc.clrtobot()
uc.refresh()
def set_panel_order(self):
if self.is_active_window("popup"):
uc.top_panel(self._panels["popup"])
elif self.is_active_window("search_results"):
uc.top_panel(self._panels["search_results"])
elif self.is_active_window("select_device"):
uc.top_panel(self._panels["select_device"])
else:
for panel_name, panel in self._panels.items():
if panel_name not in ["search_results", "select_device", "popup"]:
uc.top_panel(panel)
def update_panel_size(self):
self._resize_window("popup", self._window_sizes["popup"])
self._resize_window("search_results", self._window_sizes["search"])
self._resize_window("select_device", self._window_sizes["select_device"])
self._resize_window("user", self._window_sizes["user"])
self._resize_window("tracks", self._window_sizes["tracks"])
self._resize_window("player", self._window_sizes["player"])
def _init_render_window(self, window_name):
win = self._windows[window_name].window
uc.werase(win)
rows, cols = uc.getmaxyx(win)
return win, rows, cols
def render_user_panel(self):
win, rows, cols = self._init_render_window("user")
# Draw border.
uc.box(win)
# Show the display_name.
title_start_line = 1
self._render_text(win, title_start_line, 2,
"[Spotify Terminal]",
cols-3,
uc.A_BOLD,
centered=True)
# Show the display_name.
display_name_start_line = title_start_line + 1
self._render_text(win, display_name_start_line, 2,
self.state.get_display_name(),
cols-3,
uc.A_BOLD,
centered=True)
# Bar.
self._render_text(win, display_name_start_line+1, 1,
"_"*cols,
cols-2,
uc.A_NORMAL)
# Show the playlists.
playlists = [str(playlist) for playlist in self.state.main_menu['user']]
selected_i = self.state.main_menu["user"].i
playlist_start_line = display_name_start_line + 2
self._render_list(win,
playlists,
playlist_start_line, rows-(playlist_start_line+1),
2, cols-3,
selected_i,
self.is_active_window("user"))
def render_track_panel(self):
win, rows, cols = self._init_render_window("tracks")
# Draw border.
uc.box(win)
# Show the title of the context.
title_start_row = 1
self._render_text(win, title_start_row, 2,
self.state.main_menu['tracks'].header, cols-3, uc.A_BOLD)
# Show the tracks.
selected_i = self.state.main_menu['tracks'].i
track_start_line = title_start_row + 2
tracks = [track.str(cols-3) for track in self.state.main_menu['tracks']]
self._render_list(win, tracks, track_start_line, rows-4,
2, cols-3, selected_i, self.is_active_window("tracks"))
def render_player_panel(self):
win, rows, cols = self._init_render_window("player")
# Draw border.
uc.box(win)
self._render_text(win, 1, 2, self.state.get_currently_playing_track().track, cols-3, uc.A_BOLD)
self._render_text(win, 2, 2, self.state.get_currently_playing_track().album, cols-3, uc.A_BOLD)
self._render_text(win, 3, 2, self.state.get_currently_playing_track().artist, cols-3, uc.A_BOLD)
device_info = "{} ({}%)".format(self.state.current_device, self.state.volume)
self._render_text(win, 7, 2, device_info, cols-3, uc.A_NORMAL)
for i, action in enumerate(self.state.main_menu["player"]):
if (i == self.state.main_menu['player'].i) and self.is_active_window("player"):
style = uc.A_BOLD | uc.A_STANDOUT
else:
style = uc.A_NORMAL
uc.mvwaddstr(win, 5, 2 + i*4, action.title, style)
def render_footer(self):
if self.state.is_loading():
percent = self.state.get_loading_progress()
text = " " * int(self._cols * percent)
uc.mvwaddstr(self.stdscr, self._rows-1, 0, text, uc.A_STANDOUT)
elif self.state.is_adding_track_to_playlist():
text = "Select a playlist to add this track"
uc.mvwaddstr(self.stdscr, self._rows-1, 0, text, uc.A_BOLD)
elif self.state.is_creating_command():
start_col = 1
text = "".join(self.state.get_command_query()) + " "
uc.mvwaddstr(self.stdscr, self._rows-1, start_col, text)
uc.mvwaddstr(self.stdscr,
self._rows-1, start_col+self.state.get_cursor_i(),
text[self.state.get_cursor_i()],
uc.A_STANDOUT)
else:
entry = self.state.current_menu.get_current_list_entry()
if entry:
ncols = self._cols-1
short_str = entry.str(ncols)
long_str = str(entry)
# Check if we need to scroll or not.
if "".join(short_str.split()) == "".join(long_str.split()):
uc.mvwaddstr(self.stdscr, self._rows-1, 0, short_str, uc.A_BOLD)
# This ensures that we always start form the same position
# when we go from a static footer to a long footer that needs rolling.
self._footer_roll_index = -200
else:
self._footer_roll_index += 1
footer_roll_index = max(0, self._footer_roll_index)
footer_roll_index /= 10
# Double the string length so that we always uniformly roll
# even in the case the entire string length is less than the terminal width.
# Also, add a border to easily identify the end.
long_str = 2 * (long_str + " | ")
text = list(long_str)
for _ in xrange(footer_roll_index):
text.append(text.pop(0))
text = "".join(text)
text = text[0:ncols]
uc.mvwaddstr(self.stdscr, self._rows-1, 0, text, uc.A_BOLD)
# Track progress bar
progress = self.state.get_track_progress()
if progress:
percent = float(progress[0])/progress[1]
text = "-"*int(self._cols*percent)
self._render_text(self.stdscr, self._rows-2, 0, text, self._cols, uc.A_BOLD)
def render_search_panel(self):
win, rows, cols = self._init_render_window("search_results")
uc.box(win)
n_display_cols = cols - 4
# Show the title of the context.
title_start_row = 1
self._render_text(win,
title_start_row, 2,
self.state.search_menu["search_results"].header,
n_display_cols, uc.A_BOLD)
# Show the results.
results = [r.str(n_display_cols) for r in self.state.search_menu["search_results"]]
selected_i = self.state.search_menu.get_current_list().i
self._render_list(win,
results,
3, rows-4,
2, n_display_cols,
selected_i,
self.is_active_window("search_results"))
def render_select_device_panel(self):
win, rows, cols = self._init_render_window("select_device")
uc.box(win)
# Show the title of the context.
title_start_row = 1
self._render_text(win,
title_start_row,
2,
"Select a Player",
cols-3,
uc.A_BOLD)
selected_i = self.state.select_device_menu.get_current_list().i
self._render_list(win, self.state.select_device_menu["devices"], 3, rows-4, 2, cols-3, selected_i, self.is_active_window("select_device"))
def render_popup_panel(self):
win, rows, cols = self._init_render_window("popup")
uc.box(win)\
current_popup_list = self.state.current_popup_menu.get_current_list()
# Show the title of the context.
prompt = current_popup_list.header
title_start_row = 1
self._render_text(win,
title_start_row,
(cols/2) - (len(prompt)/2) - 1,
prompt,
cols-3,
uc.A_BOLD)
selected_i = current_popup_list.i
list_start_row = title_start_row + 2
self._render_list(win,
current_popup_list,
list_start_row, rows-list_start_row,
2, cols-4,
selected_i,
self.is_active_window("popup"),
centered=True)
def _render_list(self, win, list,
row_start, n_rows,
col_start, n_cols,
selected_i,
is_active,
centered=False):
n_elems = len(list)
start_entry_i = common.clamp(selected_i - n_rows/2,
0, max(n_elems-n_rows, 0))
end_entry_i = start_entry_i + n_rows
display_list = list[start_entry_i:end_entry_i]
for i, entry in enumerate(display_list):
text = str(entry)
if i == (selected_i-start_entry_i) and is_active:
style = uc.A_BOLD | uc.A_STANDOUT
else:
style = uc.A_NORMAL
self._render_text(win, row_start+i, col_start, text, n_cols, style, centered=centered)
def _render_text(self, win, row, col, text, n_cols, style, centered=False):
if centered:
w2 = (n_cols-col)/2
n2 = len(text)/2
uc.mvwaddnstr(win, row, col+w2-n2, text, n_cols, style)
else:
uc.mvwaddnstr(win, row, col, text, n_cols, style)
def is_active_window(self, window_name):
if self.state.in_search_menu():
return window_name == "search_results"
elif self.state.in_select_device_menu():
return window_name == "select_device"
elif self.state.is_in_state(self.state.ADD_TO_PLAYLIST_CONFIRM_PLAYLIST) or \
self.state.is_selecting_artist():
return window_name == "popup"
else:
return self.state.main_menu.get_current_list().name == window_name
def get_cur_window(self):
return self._ordered_windows[self.state.main_menu.list_i]
def get_windows(self):
return [w.window for w in self._windows.values()]
def get_panels(self):
return self._panels.values()
@property
def _rows(self):
return uc.getmaxyx(self.stdscr)[0]
@property
def _cols(self):
return uc.getmaxyx(self.stdscr)[1]
@property
def _window_sizes(self):
user = (self._rows-2,
self._cols/4,
0,
0)
tracks = (self._rows*2/3,
self._cols-(user[1])-1,
0,
user[1]+user[3])
player = (self._rows-tracks[0]-2,
tracks[1],
tracks[0],
tracks[3])
search = (self._rows*8/10,
self._cols*8/10,
self._rows/10,
self._cols/10)
select_device = (self._rows*6/10,
self._cols*6/10,
self._rows*2/10,
self._cols*2/10)
popup = (self._rows/4,
self._cols/4,
self._rows*3/8,
self._cols*3/8)
return {
"user": user,
"tracks": tracks,
"player": player,
"search": search,
"select_device": select_device,
"popup": popup
}