aboutsummaryrefslogtreecommitdiff
path: root/src/helpformaction.cpp
blob: 95ea9fa7d82f7f9302023490e32193d86c8edf99 (plain) (blame)
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
#include "helpformaction.h"

#include <cstring>

#include "config.h"
#include "fmtstrformatter.h"
#include "keymap.h"
#include "listformatter.h"
#include "strprintf.h"
#include "utils.h"
#include "view.h"

namespace newsboat {

HelpFormAction::HelpFormAction(View& vv,
	std::string formstr,
	ConfigContainer* cfg,
	const std::string& ctx)
	: FormAction(vv, formstr, cfg)
	, quit(false)
	, apply_search(false)
	, context(ctx)
	, textview("helptext", FormAction::f)
{
}

bool HelpFormAction::process_operation(Operation op,
	const std::vector<std::string>& /* args */,
	BindingType /*bindingType*/)
{
	bool hardquit = false;
	switch (op) {
	case OP_QUIT:
		quit = true;
		break;
	case OP_HARDQUIT:
		hardquit = true;
		break;
	case OP_SEARCH: {
		std::vector<QnaPair> qna;
		qna.push_back(QnaPair(_("Search for: "), ""));
		this->start_qna(qna, OP_INT_START_SEARCH, &searchhistory);
	}
	break;
	case OP_CLEARFILTER:
		apply_search = false;
		do_redraw = true;
		break;
	default:
		if (handle_textview_operations(textview, op)) {
			break;
		}
		break;
	}
	if (hardquit) {
		while (v.formaction_stack_size() > 0) {
			v.pop_current_formaction();
		}
	} else if (quit) {
		v.pop_current_formaction();
	}
	return true;
}

void HelpFormAction::prepare()
{
	if (do_redraw) {
		recalculate_widget_dimensions();
		set_keymap_hints();

		const unsigned int width = textview.get_width();

		FmtStrFormatter fmt;
		fmt.register_fmt('N', PROGRAM_NAME);
		fmt.register_fmt('V', utils::program_version());
		set_title(fmt.do_format(cfg->get_configvalue("help-title-format"), width));

		const auto descs = v.get_keymap()->get_keymap_descriptions(context);

		std::vector<std::string> colors = utils::tokenize(
				cfg->get_configvalue("search-highlight-colors"), " ");
		set_value("highlight", make_colorstring(colors));
		ListFormatter listfmt;

		std::vector<KeyMapDesc> syskey_descriptions;
		std::vector<KeyMapDesc> unbound_descriptions;
		std::vector<KeyMapDesc> bound_descriptions;

		for (const auto& desc : descs) {
			if (desc.flags & KM_SYSKEYS) {
				syskey_descriptions.push_back(desc);
			} else if (desc.key.get_key().empty()) {
				unbound_descriptions.push_back(desc);
			} else {
				bound_descriptions.push_back(desc);
			}
		}

		const auto should_be_visible = [&](const KeyMapDesc& desc) {
			return !apply_search
				|| strcasestr(desc.key.to_bindkey_string().c_str(), searchphrase.c_str()) != nullptr
				|| strcasestr(desc.cmd.c_str(), searchphrase.c_str()) != nullptr
				|| strcasestr(desc.desc.c_str(), searchphrase.c_str()) != nullptr;
		};

		std::string highlighted_searchphrase = strprintf::fmt("<hl>%s</>", searchphrase);
		const auto apply_highlights = [&](const std::string& line) {
			if (apply_search && searchphrase.length() > 0) {
				return utils::replace_all(line, searchphrase, highlighted_searchphrase);
			}
			return line;
		};

		for (const auto& desc : bound_descriptions) {
			if (should_be_visible(desc)) {
				auto line = strprintf::fmt("%-15s %-23s %s",
						desc.key.to_bindkey_string(),
						desc.cmd,
						desc.desc);
				line = utils::quote_for_stfl(line);
				line = apply_highlights(line);
				listfmt.add_line(StflRichText::from_quoted(line));
			}
		}

		if (!syskey_descriptions.empty()) {
			listfmt.add_line(StflRichText::from_plaintext(""));
			listfmt.add_line(StflRichText::from_plaintext(_("Generic bindings:")));
			listfmt.add_line(StflRichText::from_plaintext(""));

			for (const auto& desc : syskey_descriptions) {
				if (should_be_visible(desc)) {
					auto line = strprintf::fmt("%-15s %-23s %s",
							desc.key.to_bindkey_string(),
							desc.cmd,
							desc.desc);
					line = utils::quote_for_stfl(line);
					line = apply_highlights(line);
					listfmt.add_line(StflRichText::from_quoted(line));
				}
			}
		}

		if (!unbound_descriptions.empty()) {
			listfmt.add_line(StflRichText::from_plaintext(""));
			listfmt.add_line(StflRichText::from_plaintext(_("Unbound functions:")));
			listfmt.add_line(StflRichText::from_plaintext(""));

			for (const auto& desc : unbound_descriptions) {
				if (should_be_visible(desc)) {
					std::string line = strprintf::fmt("%-39s %s", desc.cmd, desc.desc);
					line = utils::quote_for_stfl(line);
					line = apply_highlights(line);
					listfmt.add_line(StflRichText::from_quoted(line));
				}
			}
		}

		const auto macros = v.get_keymap()->get_macro_descriptions();
		if (!macros.empty()) {
			listfmt.add_line(StflRichText::from_plaintext(""));
			listfmt.add_line(StflRichText::from_plaintext(_("Macros:")));
			listfmt.add_line(StflRichText::from_plaintext(""));

			for (const auto& macro : macros) {
				const std::string key = macro.first.to_bindkey_string();
				const std::string description = macro.second.description;

				// "macro-prefix" is not translated because it refers to an operation name
				std::string line = strprintf::fmt("<macro-prefix>%s  %s", key, description);
				line = utils::quote_for_stfl(line);
				line = apply_highlights(line);
				listfmt.add_line(StflRichText::from_quoted(line));
			}
		}

		textview.stfl_replace_lines(listfmt.get_lines_count(), listfmt.format_list());

		do_redraw = false;
	}
	quit = false;
}

void HelpFormAction::init()
{
}

std::vector<KeyMapHintEntry> HelpFormAction::get_keymap_hint() const
{
	std::vector<KeyMapHintEntry> hints;
	hints.push_back({OP_QUIT, _("Quit")});
	hints.push_back({OP_SEARCH, _("Search")});
	if (apply_search) {
		hints.push_back({OP_CLEARFILTER, _("Clear")});
	}
	return hints;
}

void HelpFormAction::finished_qna(Operation op)
{
	v.inside_qna(false);
	switch (op) {
	case OP_INT_START_SEARCH:
		searchphrase = qna_responses[0];
		apply_search = true;
		do_redraw = true;
		break;
	default:
		FormAction::finished_qna(op);
		break;
	}
}

std::string HelpFormAction::title()
{
	return _("Help");
}

std::string HelpFormAction::make_colorstring(
	const std::vector<std::string>& colors)
{
	std::string result;
	if (colors.size() > 0) {
		if (colors[0] != "default") {
			result.append("fg=");
			result.append(colors[0]);
		}
		if (colors.size() > 1) {
			if (colors[1] != "default") {
				if (result.length() > 0) {
					result.append(",");
				}
				result.append("bg=");
				result.append(colors[1]);
			}
		}
		for (unsigned int i = 2; i < colors.size(); i++) {
			if (result.length() > 0) {
				result.append(",");
			}
			result.append("attr=");
			result.append(colors[i]);
		}
	}
	return result;
}

} // namespace newsboat