aboutsummaryrefslogtreecommitdiff
path: root/src/dialogsformaction.cpp
blob: d50b7f2fae361dda7b4342c9d96666c4ad34e98e (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
#include "dialogsformaction.h"

#include <cstdio>
#include <string>

#include "config.h"
#include "fmtstrformatter.h"
#include "stflrichtext.h"
#include "strprintf.h"
#include "utils.h"
#include "view.h"

namespace newsboat {

DialogsFormAction::DialogsFormAction(View& vv,
	std::string formstr,
	ConfigContainer* cfg, RegexManager& r)
	: ListFormAction(vv, "dialoglist", formstr, "dialogs", cfg, r)
{
}

void DialogsFormAction::init()
{
	set_keymap_hints();

	recalculate_widget_dimensions();
}

void DialogsFormAction::prepare()
{
	if (do_redraw) {
		update_heading();

		auto render_line = [this](std::uint32_t line, std::uint32_t width) -> StflRichText {
			(void)width;
			const auto formaction_names = v.get_formaction_names();
			const auto& fa = formaction_names[line];
			const bool is_current_formaction =
			v.get_formaction(fa.first) == get_parent_formaction();
			return StflRichText::from_plaintext(
				strprintf::fmt("%4u %s %s",
					line + 1,
					is_current_formaction ? "*" : " ",
					fa.second));
		};

		list.invalidate_list_content(v.get_formaction_names().size(), render_line);

		do_redraw = false;
	}
}

void DialogsFormAction::update_heading()
{

	const unsigned int width = list.get_width();
	const std::string title_format = cfg->get_configvalue("dialogs-title-format");
	FmtStrFormatter fmt;
	fmt.register_fmt('N', PROGRAM_NAME);
	fmt.register_fmt('V', utils::program_version());
	set_title(fmt.do_format(title_format, width));
}

std::vector<KeyMapHintEntry> DialogsFormAction::get_keymap_hint() const
{
	static const std::vector<KeyMapHintEntry> hints = {{OP_QUIT, _("Close")},
		{OP_OPEN, _("Goto Dialog")},
		{OP_CLOSEDIALOG, _("Close Dialog")}
	};
	return hints;
}

bool DialogsFormAction::process_operation(Operation op,
	const std::vector<std::string>& args,
	BindingType bindingType)
{
	switch (op) {
	case OP_OPEN: {
		const unsigned int pos = list.get_position();
		v.set_current_formaction(pos);
	}
	break;
	case OP_CLOSEDIALOG: {
		const unsigned int pos = list.get_position();
		if (pos != 0) {
			v.remove_formaction(pos);
			do_redraw = true;
		} else {
			v.get_statusline().show_error(
				_("Error: you can't remove the feed list!"));
		}
	}
	break;
	case OP_PREV:
		list.move_up(cfg->get_configvalue_as_bool("wrap-scroll"));
		break;
	case OP_NEXT:
		list.move_down(cfg->get_configvalue_as_bool("wrap-scroll"));
		break;
	case OP_QUIT:
		v.pop_current_formaction();
		break;
	default:
		ListFormAction::process_operation(op, args, bindingType);
		break;
	}
	return true;
}

std::string DialogsFormAction::title()
{
	return ""; // will never be displayed
}

void DialogsFormAction::handle_cmdline(const std::string& cmd)
{
	unsigned int idx = 0;
	if (1 == sscanf(cmd.c_str(), "%u", &idx)) {
		if (idx >= 1 && idx <= v.formaction_stack_size()) {
			list.set_position(idx - 1);
		} else {
			v.get_statusline().show_error(_("Invalid position!"));
		}
	} else {
		FormAction::handle_cmdline(cmd);
	}
}

} // namespace newsboat