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
|
#include "pbview.h"
#include <cinttypes>
#include <cstring>
#include <ncurses.h>
#include "config.h"
#include "configcontainer.h"
#include "dllist.h"
#include "download.h"
#include "fmtstrformatter.h"
#include "help.h"
#include "listformatter.h"
#include "logger.h"
#include "pbcontroller.h"
#include "strprintf.h"
using namespace newsboat;
namespace podboat {
PbView::PbView(PbController& c)
: update_view(true)
, ctrl(c)
, dllist_form(dllist_str)
, help_form(help_str)
, title_line_dllist_form(dllist_form, "head")
, title_line_help_form(help_form, "head")
, msg_line_dllist_form(dllist_form, "msg")
, msg_line_help_form(help_form, "msg")
, keys(ctrl.get_keymap())
, colorman(ctrl.get_colormanager())
, downloads_list("dls", dllist_form,
ctrl.get_cfgcont()->get_configvalue_as_int("scrolloff"))
, help_textview("helptext", help_form)
{
if (getenv("ESCDELAY") == nullptr) {
set_escdelay(25);
}
}
PbView::~PbView()
{
Stfl::reset();
}
void PbView::run(bool auto_download, bool wrap_scroll)
{
bool quit = false;
// Make sure curses is initialized
dllist_form.run(-3);
// Hide cursor using curses
curs_set(0);
set_dllist_keymap_hint();
do {
if (update_view) {
const double total_kbps = ctrl.get_total_kbps();
const auto speed = get_speed_human_readable(total_kbps);
auto title = strprintf::fmt(
_("Queue (%u downloads in progress, %u total) - %.2f %s total"),
static_cast<unsigned int>(ctrl.downloads_in_progress()),
static_cast<unsigned int>(ctrl.downloads().size()),
speed.first,
speed.second);
if (ctrl.get_maxdownloads() > 1) {
title += strprintf::fmt(_(" - %u parallel downloads"), ctrl.get_maxdownloads());
}
title_line_dllist_form.set_text(title);
LOG(Level::DEBUG,
"PbView::run: updating view... "
"downloads().size() "
"= %" PRIu64,
static_cast<uint64_t>(ctrl.downloads().size()));
ListFormatter listfmt;
const std::string line_format =
ctrl.get_cfgcont()->get_configvalue("podlist-format");
dllist_form.run(-3); // compute all widget dimensions
auto render_line = [this, line_format](std::uint32_t line,
std::uint32_t width) -> StflRichText {
const auto& downloads = ctrl.downloads();
const auto& dl = downloads.at(line);
return format_line(line_format, dl, line, width);
};
downloads_list.invalidate_list_content(ctrl.downloads().size(), render_line);
update_view = false;
}
// If there's no status message, we know there's no error to show
// Thus, it's safe to replace with the download's status
if (msg_line_dllist_form.get_text().empty() && ctrl.downloads().size() > 0) {
const auto idx = downloads_list.get_position();
msg_line_dllist_form.set_text(ctrl.downloads()[idx].status_msg());
}
const auto event = dllist_form.run(500);
if (auto_download) {
if (ctrl.get_maxdownloads() >
ctrl.downloads_in_progress()) {
ctrl.start_downloads();
}
}
if (event.empty() || event == "TIMEOUT") {
continue;
}
if (event == "RESIZE") {
handle_resize();
continue;
}
const auto key_combination = KeyCombination::from_bindkey(event);
Operation op = keys.get_operation(key_combination, "podboat");
if (msg_line_dllist_form.get_text().length() > 0) {
msg_line_dllist_form.set_text("");
update_view = true;
}
switch (op) {
case OP_REDRAW:
Stfl::reset();
break;
case OP_PREV:
case OP_SK_UP:
downloads_list.move_up(wrap_scroll);
break;
case OP_NEXT:
case OP_SK_DOWN:
downloads_list.move_down(wrap_scroll);
break;
case OP_SK_HOME:
downloads_list.move_to_first();
break;
case OP_SK_END:
downloads_list.move_to_last();
break;
case OP_SK_PGUP:
downloads_list.move_page_up(wrap_scroll);
break;
case OP_SK_PGDOWN:
downloads_list.move_page_down(wrap_scroll);
break;
case OP_SK_HALF_PAGE_UP:
downloads_list.scroll_halfpage_up(wrap_scroll);
break;
case OP_SK_HALF_PAGE_DOWN:
downloads_list.scroll_halfpage_down(wrap_scroll);
break;
case OP_PB_TOGGLE_DLALL:
auto_download = !auto_download;
break;
case OP_HARDQUIT:
case OP_QUIT:
if (ctrl.downloads_in_progress() > 0) {
msg_line_dllist_form.set_text(_("Error: can't quit: download(s) in progress."));
update_view = true;
} else {
quit = true;
}
break;
case OP_PB_MOREDL:
ctrl.increase_parallel_downloads();
break;
case OP_PB_LESSDL:
ctrl.decrease_parallel_downloads();
break;
case OP_PB_DOWNLOAD: {
if (ctrl.downloads().size() >= 1) {
const auto idx = downloads_list.get_position();
auto& item = ctrl.downloads()[idx];
if (item.status() != DlStatus::DOWNLOADING) {
ctrl.start_download(item);
}
}
}
break;
case OP_PB_PLAY: {
if (ctrl.downloads().size() >= 1) {
const auto idx = downloads_list.get_position();
DlStatus status =
ctrl.downloads()[idx].status();
if (status == DlStatus::FINISHED ||
status == DlStatus::PLAYED ||
status == DlStatus::READY) {
ctrl.play_file(ctrl.downloads()[idx]
.filename());
ctrl.downloads()[idx].set_status(
DlStatus::PLAYED);
} else {
msg_line_dllist_form.set_text(_("Error: download needs to be "
"finished before the file can be played."));
}
}
}
break;
case OP_PB_MARK_FINISHED: {
auto& downloads = ctrl.downloads();
if (downloads.size() >= 1) {
const auto idx = downloads_list.get_position();
DlStatus status =
downloads[idx].status();
if (status == DlStatus::PLAYED) {
downloads[idx].set_status(DlStatus::FINISHED);
if (idx + 1 < downloads.size()) {
downloads_list.set_position(idx + 1);
}
}
}
}
break;
case OP_PB_CANCEL: {
if (ctrl.downloads().size() >= 1) {
const auto idx = downloads_list.get_position();
if (ctrl.downloads()[idx].status() ==
DlStatus::DOWNLOADING) {
ctrl.downloads()[idx].set_status(
DlStatus::CANCELLED);
}
}
}
break;
case OP_PB_DELETE: {
auto& downloads = ctrl.downloads();
if (downloads.size() >= 1) {
const auto idx = downloads_list.get_position();
if (downloads[idx].status() !=
DlStatus::DOWNLOADING) {
downloads[idx].set_status(DlStatus::DELETED);
if (idx + 1 < downloads.size()) {
downloads_list.set_position(idx + 1);
}
}
}
}
break;
case OP_PB_PURGE:
if (ctrl.downloads_in_progress() > 0) {
msg_line_dllist_form.set_text(_("Error: unable to perform operation: "
"download(s) in progress."));
} else {
ctrl.purge_queue();
}
update_view = true;
break;
case OP_HELP:
run_help();
break;
default:
break;
}
} while (!quit);
}
void PbView::handle_resize()
{
std::vector<std::reference_wrapper<newsboat::Stfl::Form>> forms = {dllist_form, help_form};
for (const auto& form : forms) {
form.get().run(-3);
}
update_view = true;
}
void PbView::apply_colors_to_all_forms()
{
using namespace std::placeholders;
colorman.apply_colors(std::bind(&newsboat::Stfl::Form::set, &dllist_form, _1,
_2));
colorman.apply_colors(std::bind(&newsboat::Stfl::Form::set, &help_form, _1,
_2));
}
std::pair<double, std::string> PbView::get_speed_human_readable(double kbps)
{
if (kbps < 1024) {
return std::make_pair(kbps, _("KB/s"));
} else if (kbps < 1024 * 1024) {
return std::make_pair(kbps / 1024, _("MB/s"));
} else {
return std::make_pair(kbps / 1024 / 1024, _("GB/s"));
}
}
void PbView::run_help()
{
set_help_keymap_hint();
title_line_help_form.set_text(_("Help"));
const auto descs = keys.get_keymap_descriptions("podboat");
ListFormatter listfmt;
for (const auto& desc : descs) {
const std::string descline = strprintf::fmt("%-7s %-23s %s",
desc.key.to_bindkey_string(),
desc.cmd,
desc.desc);
listfmt.add_line(StflRichText::from_plaintext(descline));
}
help_textview.stfl_replace_lines(listfmt.get_lines_count(),
listfmt.format_list());
bool quit = false;
do {
const auto event = help_form.run(0);
if (event.empty()) {
continue;
}
if (event == "RESIZE") {
handle_resize();
continue;
}
const auto key_combination = KeyCombination::from_bindkey(event);
Operation op = keys.get_operation(key_combination, "help");
switch (op) {
case OP_SK_UP:
help_textview.scroll_up();
break;
case OP_SK_DOWN:
help_textview.scroll_down();
break;
case OP_SK_HOME:
help_textview.scroll_to_top();
break;
case OP_SK_END:
help_textview.scroll_to_bottom();
break;
case OP_SK_PGUP:
help_textview.scroll_page_up();
break;
case OP_SK_PGDOWN:
help_textview.scroll_page_down();
break;
case OP_SK_HALF_PAGE_UP:
help_textview.scroll_halfpage_up();
break;
case OP_SK_HALF_PAGE_DOWN:
help_textview.scroll_halfpage_down();
break;
case OP_HARDQUIT:
case OP_QUIT:
quit = true;
break;
default:
break;
}
} while (!quit);
}
void PbView::set_help_keymap_hint()
{
static const std::vector<KeyMapHintEntry> hints = {{OP_QUIT, _("Quit")}};
const auto keymap_hint = keys.prepare_keymap_hint(hints, "podboat");
help_form.set("help", keymap_hint);
}
void PbView::set_dllist_keymap_hint()
{
static const std::vector<KeyMapHintEntry> hints = {{OP_QUIT, _("Quit")},
{OP_PB_DOWNLOAD, _("Download")},
{OP_PB_CANCEL, _("Cancel")},
{OP_PB_DELETE, _("Delete")},
{OP_PB_PURGE, _("Purge Finished")},
{OP_PB_TOGGLE_DLALL, _("Toggle Automatic Download")},
{OP_PB_PLAY, _("Play")},
{OP_PB_MARK_FINISHED, _("Mark as Finished")},
{OP_HELP, _("Help")}
};
const auto keymap_hint = keys.prepare_keymap_hint(hints, "podboat");
dllist_form.set("help", keymap_hint);
}
StflRichText PbView::format_line(const std::string& podlist_format,
const Download& dl,
unsigned int pos,
unsigned int width)
{
FmtStrFormatter fmt;
const double speed_kbps = dl.kbps();
const auto speed = get_speed_human_readable(speed_kbps);
fmt.register_fmt('i', strprintf::fmt("%u", pos + 1));
fmt.register_fmt('d',
strprintf::fmt("%.1f", dl.current_size() / (1024 * 1024)));
fmt.register_fmt(
't', strprintf::fmt("%.1f", dl.total_size() / (1024 * 1024)));
fmt.register_fmt('p', strprintf::fmt("%.1f", dl.percents_finished()));
fmt.register_fmt('k', strprintf::fmt("%.2f", speed_kbps));
fmt.register_fmt('K', strprintf::fmt("%.2f %s", speed.first, speed.second));
fmt.register_fmt('S', strprintf::fmt("%s", dl.status_text()));
fmt.register_fmt('u', strprintf::fmt("%s", dl.url()));
fmt.register_fmt('F', strprintf::fmt("%s", dl.filename()));
fmt.register_fmt('b', strprintf::fmt("%s", dl.basename()));
auto formattedLine = fmt.do_format(podlist_format, width);
return StflRichText::from_plaintext(formattedLine);
}
} // namespace podboat
|