1 #include "dialogsformaction.h"
2 
3 #include <cstdio>
4 #include <string>
5 
6 #include "config.h"
7 #include "fmtstrformatter.h"
8 #include "listformatter.h"
9 #include "strprintf.h"
10 #include "utils.h"
11 #include "view.h"
12 
13 namespace newsboat {
14 
DialogsFormAction(View * vv,std::string formstr,ConfigContainer * cfg)15 DialogsFormAction::DialogsFormAction(View* vv,
16 	std::string formstr,
17 	ConfigContainer* cfg)
18 	: FormAction(vv, formstr, cfg)
19 	, dialogs_list("dialogs", FormAction::f,
20 		  cfg->get_configvalue_as_int("scrolloff"))
21 {
22 }
23 
~DialogsFormAction()24 DialogsFormAction::~DialogsFormAction() {}
25 
init()26 void DialogsFormAction::init()
27 {
28 	set_keymap_hints();
29 
30 	f.run(-3); // compute all widget dimensions
31 }
32 
prepare()33 void DialogsFormAction::prepare()
34 {
35 	if (do_redraw) {
36 		update_heading();
37 
38 		ListFormatter listfmt;
39 
40 		unsigned int i = 1;
41 		for (const auto& fa : v->get_formaction_names()) {
42 			LOG(Level::DEBUG,
43 				"DialogsFormAction::prepare: p1 = %p p2 = %p",
44 				v->get_formaction(fa.first).get(),
45 				get_parent_formaction().get());
46 			listfmt.add_line(
47 				utils::quote_for_stfl(
48 					strprintf::fmt("%4u %s %s",
49 						i,
50 						(v->get_formaction(fa.first).get() ==
51 							get_parent_formaction().get())
52 						? "*"
53 						: " ",
54 						fa.second)));
55 			i++;
56 		}
57 
58 		dialogs_list.stfl_replace_lines(listfmt);
59 
60 		do_redraw = false;
61 	}
62 }
63 
update_heading()64 void DialogsFormAction::update_heading()
65 {
66 
67 	const unsigned int width = dialogs_list.get_width();
68 	const std::string title_format = cfg->get_configvalue("dialogs-title-format");
69 	FmtStrFormatter fmt;
70 	fmt.register_fmt('N', PROGRAM_NAME);
71 	fmt.register_fmt('V', utils::program_version());
72 	f.set("head", fmt.do_format(title_format, width));
73 }
74 
get_keymap_hint()75 KeyMapHintEntry* DialogsFormAction::get_keymap_hint()
76 {
77 	static KeyMapHintEntry hints[] = {{OP_QUIT, _("Close")},
78 		{OP_OPEN, _("Goto Dialog")},
79 		{OP_CLOSEDIALOG, _("Close Dialog")},
80 		{OP_NIL, nullptr}
81 	};
82 	return hints;
83 }
84 
process_operation(Operation op,bool,std::vector<std::string> *)85 bool DialogsFormAction::process_operation(Operation op,
86 	bool /* automatic */,
87 	std::vector<std::string>* /* args */)
88 {
89 	switch (op) {
90 	case OP_OPEN: {
91 		const unsigned int pos = dialogs_list.get_position();
92 		v->set_current_formaction(pos);
93 	}
94 	break;
95 	case OP_CLOSEDIALOG: {
96 		const unsigned int pos = dialogs_list.get_position();
97 		if (pos != 0) {
98 			v->remove_formaction(pos);
99 			do_redraw = true;
100 		} else {
101 			v->show_error(
102 				_("Error: you can't remove the feed list!"));
103 		}
104 	}
105 	break;
106 	case OP_PREV:
107 	case OP_SK_UP:
108 		dialogs_list.move_up(cfg->get_configvalue_as_bool("wrap-scroll"));
109 		break;
110 	case OP_NEXT:
111 	case OP_SK_DOWN:
112 		dialogs_list.move_down(cfg->get_configvalue_as_bool("wrap-scroll"));
113 		break;
114 	case OP_SK_HOME:
115 		dialogs_list.move_to_first();
116 		break;
117 	case OP_SK_END:
118 		dialogs_list.move_to_last();
119 		break;
120 	case OP_SK_PGUP:
121 		dialogs_list.move_page_up(cfg->get_configvalue_as_bool("wrap-scroll"));
122 		break;
123 	case OP_SK_PGDOWN:
124 		dialogs_list.move_page_down(cfg->get_configvalue_as_bool("wrap-scroll"));
125 		break;
126 	case OP_QUIT:
127 		v->pop_current_formaction();
128 		break;
129 	default:
130 		break;
131 	}
132 	return true;
133 }
134 
title()135 std::string DialogsFormAction::title()
136 {
137 	return ""; // will never be displayed
138 }
139 
handle_cmdline(const std::string & cmd)140 void DialogsFormAction::handle_cmdline(const std::string& cmd)
141 {
142 	unsigned int idx = 0;
143 	if (1 == sscanf(cmd.c_str(), "%u", &idx)) {
144 		if (idx >= 1 && idx <= v->formaction_stack_size()) {
145 			dialogs_list.set_position(idx - 1);
146 		} else {
147 			v->show_error(_("Invalid position!"));
148 		}
149 	} else {
150 		FormAction::handle_cmdline(cmd);
151 	}
152 }
153 
154 } // namespace newsboat
155