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
|
#include <select_formaction.h>
#include <formatstring.h>
#include <view.h>
#include <config.h>
#include <utils.h>
#include <strprintf.h>
#include <listformatter.h>
#include <sstream>
#include <cassert>
namespace newsbeuter {
/*
* The select_formaction is used both for the "select tag" dialog
* and the "select filter", as they do practically the same. That's
* why there is the decision between SELECTTAG and SELECTFILTER on
* a few places.
*/
select_formaction::select_formaction(view * vv, std::string formstr)
: formaction(vv, formstr), quit(false), type(selection_type::TAG)
{ }
select_formaction::~select_formaction() { }
void select_formaction::handle_cmdline(const std::string& cmd) {
unsigned int idx = 0;
if (1==sscanf(cmd.c_str(),"%u",&idx)) {
if (idx > 0 && idx <= ((type == selection_type::TAG) ? tags.size() : filters.size())) {
f->set("tagpos", std::to_string(idx - 1));
}
} else {
formaction::handle_cmdline(cmd);
}
}
void select_formaction::process_operation(operation op, bool /* automatic */, std::vector<std::string> * /* args */) {
bool hardquit = false;
switch (op) {
case OP_QUIT:
value = "";
quit = true;
break;
case OP_HARDQUIT:
value = "";
hardquit = true;
break;
case OP_OPEN: {
std::string tagposname = f->get("tagposname");
unsigned int pos = utils::to_u(tagposname);
if (tagposname.length() > 0) {
switch (type) {
case selection_type::TAG: {
if (pos < tags.size()) {
value = tags[pos];
quit = true;
}
}
break;
case selection_type::FILTER: {
if (pos < filters.size()) {
value = filters[pos].second;
quit = true;
}
}
break;
default:
assert(0); // should never happen
}
}
}
break;
default:
break;
}
if (hardquit) {
while (v->formaction_stack_size() > 0) {
v->pop_current_formaction();
}
} else if (quit) {
v->pop_current_formaction();
}
}
void select_formaction::prepare() {
if (do_redraw) {
listformatter listfmt;
unsigned int i=0;
switch (type) {
case selection_type::TAG:
for (auto tag : tags) {
std::string tagstr = strprintf::fmt("%4u %s (%u)", i+1, tag, v->get_ctrl()->get_feed_count_per_tag(tag));
listfmt.add_line(tagstr, i);
i++;
}
break;
case selection_type::FILTER:
for (auto filter : filters) {
std::string tagstr = strprintf::fmt("%4u %s", i+1, filter.first);
listfmt.add_line(tagstr, i);
i++;
}
break;
default:
assert(0);
}
f->modify("taglist", "replace_inner", listfmt.format_list());
do_redraw = false;
}
}
void select_formaction::init() {
std::string title;
do_redraw = true;
quit = false;
value = "";
std::string viewwidth = f->get("taglist:w");
unsigned int width = utils::to_u(viewwidth, 80);
set_keymap_hints();
fmtstr_formatter fmt;
fmt.register_fmt('N', PROGRAM_NAME);
fmt.register_fmt('V', PROGRAM_VERSION);
switch (type) {
case selection_type::TAG:
title = fmt.do_format(v->get_cfg()->get_configvalue("selecttag-title-format"), width);
break;
case selection_type::FILTER:
title = fmt.do_format(v->get_cfg()->get_configvalue("selectfilter-title-format"), width);
break;
default:
assert(0); // should never happen
}
f->set("head", title);
}
keymap_hint_entry * select_formaction::get_keymap_hint() {
static keymap_hint_entry hints_tag[] = {
{ OP_QUIT, _("Cancel") },
{ OP_OPEN, _("Select Tag") },
{ OP_NIL, nullptr }
};
static keymap_hint_entry hints_filter[] = {
{ OP_QUIT, _("Cancel") },
{ OP_OPEN, _("Select Filter") },
{ OP_NIL, nullptr }
};
switch (type) {
case selection_type::TAG:
return hints_tag;
case selection_type::FILTER:
return hints_filter;
}
return nullptr;
}
std::string select_formaction::title() {
switch (type) {
case selection_type::TAG:
return _("Select Tag");
case selection_type::FILTER:
return _("Select Filter");
default:
return "";
}
}
}
|