1mod descriptor;
8mod filter_field;
9mod filter_row;
10mod options;
11mod pointer;
12mod suggest;
13
14use forms::FormValues;
15
16pub use filter_row::RowShape;
17pub use options::OPTIONS_BUTTONS;
18
19use crate::event::Intent;
20use crate::modal::Spec;
21use crate::overlay::Placement;
22use crate::tabs::reports::ReportKind;
23use crate::widgets::{Editor, Hints, SelectOption, SelectWidget, Widget, WidgetKind};
24use crate::window::Origin;
25
26#[cfg(test)]
27mod tests;
28
29pub const BUTTONS: [Spec; 2] = [("~O~K", Intent::Submit), ("~C~lose", Intent::CloseTopmost)];
31
32#[derive(Debug)]
34pub struct Field {
35 pub key: Option<&'static str>,
37 pub label: &'static str,
38 pub widget: Widget,
39}
40
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub enum FormKind {
44 ConfigSet,
45 Options,
47 SshKeyAdd,
49 CsvSave,
51 Report(ReportKind),
52 TagCreate,
53 TagEdit,
55 TemplateCreate,
56 TemplateEdit,
58 ScriptCreate,
59 ScriptEdit,
61 TemplatePick,
63 CommodityCreate,
64 CommodityConvert,
65 AccountCreate,
66 TransactionCreate,
67 TransactionEdit,
69 AccountEdit,
71 CommodityEdit,
73 AccountMerge,
75 TransactionFilter,
77 FilterAdd,
79 FilterRow(RowShape),
81}
82
83#[derive(Debug)]
85pub struct Form {
86 pub fields: Vec<Field>,
87 pub focus: usize,
89 pub kind: FormKind,
90 pub hidden: Vec<(&'static str, String)>,
92 pub errors: Vec<String>,
94 pub notices: Vec<String>,
96 pub baseline: Option<FormValues>,
98 pub hints: Hints,
100 pub completing: Option<(String, String)>,
102 pub origin: Option<Origin>,
105 pub placement: Placement,
108}
109
110impl Form {
111 #[must_use]
113 pub fn config_set() -> Self {
114 Self::from_descriptor(&forms::CONFIG_SET, FormKind::ConfigSet)
115 }
116
117 #[must_use]
119 pub fn ssh_key_add() -> Self {
120 Self::from_descriptor(&forms::SSH_KEY_ADD, FormKind::SshKeyAdd)
121 }
122
123 #[must_use]
125 pub fn csv_save(path: &str) -> Self {
126 let mut form = Self::from_descriptor(&descriptor::CSV_SAVE, FormKind::CsvSave);
127 for field in &mut form.fields {
128 field.widget.set_text(path);
129 }
130 form
131 }
132
133 #[must_use]
136 pub fn report(kind: ReportKind, prefill: &forms::FormValues) -> Self {
137 let mut form = Self::from_descriptor(kind.descriptor(), FormKind::Report(kind));
138 for field in &mut form.fields {
139 if matches!(field.widget, Widget::Date(_)) {
140 field.widget.set_text("");
141 }
142 }
143 form.load(prefill);
144 form
145 }
146
147 #[must_use]
149 pub fn commodity_create() -> Self {
150 Self::from_descriptor(&forms::COMMODITY_CREATE, FormKind::CommodityCreate)
151 }
152
153 #[must_use]
155 pub fn commodity_convert() -> Self {
156 Self::from_descriptor(&forms::CONVERT, FormKind::CommodityConvert)
157 }
158
159 #[must_use]
161 pub fn account_create() -> Self {
162 Self::from_descriptor(&forms::ACCOUNT_CREATE, FormKind::AccountCreate)
163 }
164
165 #[must_use]
167 pub fn transaction_create() -> Self {
168 Self::from_descriptor(&forms::TRANSACTION_CREATE, FormKind::TransactionCreate)
169 }
170
171 #[must_use]
173 pub fn account_merge() -> Self {
174 Self::from_descriptor(&forms::ACCOUNT_MERGE, FormKind::AccountMerge)
175 }
176
177 #[must_use]
179 pub fn transaction_filter(filter: &FormValues) -> Self {
180 let mut form = Self::from_fields(
181 forms::TRANSACTION_QUERY.filter_fields(),
182 FormKind::TransactionFilter,
183 );
184 for field in &mut form.fields {
185 if matches!(field.widget, Widget::Date(_)) {
186 field.widget.set_text("");
187 }
188 }
189 form.load(filter);
190 form
191 }
192
193 #[must_use]
195 pub fn select_label(&self, key: &str) -> Option<&str> {
196 self.fields
197 .iter()
198 .find(|field| field.key == Some(key))
199 .and_then(|field| match &field.widget {
200 Widget::Select(sw) => Some(sw.display()),
201 _ => None,
202 })
203 }
204
205 #[must_use]
207 pub fn tag_create() -> Self {
208 Self::from_descriptor(&forms::TAG_CREATE, FormKind::TagCreate)
209 }
210
211 #[must_use]
214 pub fn tag_edit(loaded: &forms::FormValues) -> Self {
215 let mut form = Self::from_descriptor(&forms::TAG_EDIT, FormKind::TagEdit);
216 form.load(loaded);
217 form
218 }
219
220 #[must_use]
222 pub fn template_create() -> Self {
223 Self::from_descriptor(&forms::TEMPLATE_CREATE, FormKind::TemplateCreate)
224 }
225
226 #[must_use]
229 pub fn template_edit(loaded: &forms::FormValues) -> Self {
230 let mut form = Self::from_descriptor(&forms::TEMPLATE_EDIT, FormKind::TemplateEdit);
231 form.load(loaded);
232 form
233 }
234
235 #[must_use]
237 pub fn template_pick(options: &[SelectOption]) -> Self {
238 let mut form = Self::from_descriptor(&descriptor::TEMPLATE_PICK, FormKind::TemplatePick);
239 for field in &mut form.fields {
240 if let Widget::Select(select) = &mut field.widget {
241 select.set_options(options.to_vec());
242 }
243 }
244 form
245 }
246
247 #[must_use]
249 pub fn transaction_from_template(loaded: &forms::FormValues) -> Self {
250 let mut form = Self::transaction_create();
251 form.load(loaded);
252 form
253 }
254
255 #[must_use]
257 pub fn script_create() -> Self {
258 Self::from_descriptor(&forms::SCRIPT_CREATE, FormKind::ScriptCreate)
259 }
260
261 #[must_use]
264 pub fn script_edit(loaded: &forms::FormValues) -> Self {
265 let mut form = Self::from_descriptor(&forms::SCRIPT_EDIT, FormKind::ScriptEdit);
266 form.load(loaded);
267 form
268 }
269
270 #[must_use]
273 pub fn transaction_edit(loaded: &forms::FormValues) -> Self {
274 let mut form = Self::from_descriptor(&forms::TRANSACTION_EDIT, FormKind::TransactionEdit);
275 form.load(loaded);
276 form
277 }
278
279 #[must_use]
282 pub fn account_edit(loaded: &forms::FormValues) -> Self {
283 let mut form = Self::from_descriptor(&forms::ACCOUNT_EDIT, FormKind::AccountEdit);
284 form.load(loaded);
285 form
286 }
287
288 #[must_use]
291 pub fn commodity_edit(loaded: &forms::FormValues) -> Self {
292 let mut form = Self::from_descriptor(&forms::COMMODITY_EDIT, FormKind::CommodityEdit);
293 form.load(loaded);
294 form
295 }
296
297 pub fn close_tags(&mut self) -> bool {
299 self.fields.iter_mut().any(|field| match &mut field.widget {
300 Widget::Splits(sw) => sw.close_tags(),
301 _ => false,
302 })
303 }
304
305 pub fn close_select(&mut self) -> bool {
307 match self.focused_select_mut() {
308 Some(select) if select.open => {
309 select.close();
310 true
311 }
312 _ => false,
313 }
314 }
315
316 #[must_use]
318 pub fn focused_select(&self) -> Option<&SelectWidget> {
319 match &self.fields.get(self.focus)?.widget {
320 Widget::Select(select) => Some(select),
321 Widget::Splits(sw) => sw.focused_select(),
322 Widget::Rows(rw) => rw.focused_select(),
323 _ => None,
324 }
325 }
326
327 pub fn focused_select_mut(&mut self) -> Option<&mut SelectWidget> {
328 match &mut self.fields.get_mut(self.focus)?.widget {
329 Widget::Select(select) => Some(select),
330 Widget::Splits(sw) => sw.focused_select_mut(),
331 Widget::Rows(rw) => rw.focused_select_mut(),
332 _ => None,
333 }
334 }
335
336 pub fn cycle(&mut self, forward: bool) {
338 self.close_select();
339 let len = self.fields.len() + self.buttons().len();
340 self.focus = if forward {
341 (self.focus + 1) % len
342 } else {
343 (self.focus + len - 1) % len
344 };
345 }
346
347 pub fn focus_field(&mut self, index: usize) {
349 if index < self.fields.len() {
350 self.close_select();
351 self.focus = index;
352 }
353 }
354
355 #[must_use]
357 pub fn focused_button(&self) -> Option<usize> {
358 self.focus.checked_sub(self.fields.len())
359 }
360
361 pub fn focused_field_mut(&mut self) -> Option<&mut Field> {
363 self.fields.get_mut(self.focus)
364 }
365
366 pub fn focused_editor_mut(&mut self) -> Option<&mut Editor> {
371 self.focused_field_mut()
372 .and_then(|f| f.widget.as_text_mut())
373 }
374
375 #[must_use]
377 pub fn focused_widget_kind(&self) -> Option<WidgetKind> {
378 self.fields.get(self.focus).map(|f| f.widget.kind())
379 }
380
381 #[must_use]
384 pub fn focused_cells_boundary(&self) -> Option<(bool, bool)> {
385 match self.fields.get(self.focus)?.widget {
386 Widget::Splits(ref sw) if sw.tags_open() => Some((false, false)),
387 Widget::Splits(ref sw) => Some((sw.at_first_cell(), sw.at_last_cell())),
388 Widget::Rows(ref rw) => Some((rw.at_first_cell(), rw.at_last_cell())),
389 _ => None,
390 }
391 }
392
393 #[must_use]
396 pub fn focused_vertical_boundary(&self) -> Option<(bool, bool)> {
397 match self.fields.get(self.focus)?.widget {
398 Widget::Splits(ref sw) if sw.tags_open() => sw
399 .tags()
400 .map(|tags| (tags.at_first_row(), tags.at_last_row())),
401 Widget::Splits(ref sw) => Some((sw.at_first_row(), sw.at_last_row())),
402 Widget::Rows(ref rw) => Some((rw.at_first_row(), rw.at_last_row())),
403 Widget::Text(ref ed) if ed.is_multiline() => {
404 Some((ed.at_first_line(), ed.at_last_line()))
405 }
406 _ => None,
407 }
408 }
409}