Skip to main content

tui/app/
lookup.rs

1//! One lookup flow for palette args naming accounts or commodities by label:
2//! fetch the option lists the typed values need, resolve them against the
3//! form descriptor, then hand the resolved values to the feature that asked.
4
5use cli_core::eval::escape_str;
6use cli_core::forms::{Fetched, map_values, parse_form_options, sources_used};
7use forms::{Form, FormValues, Source};
8
9use super::App;
10use crate::route::RouteCtx;
11use crate::tabs::list::ListTab;
12use crate::tabs::reports::ReportKind;
13use crate::tabs::seq::Seq;
14use crate::window::{Origin, WindowId};
15
16/// The feature a lookup serves; each counts its own lookups to drop stale replies.
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum Goal {
19    /// Filters the Transactions window.
20    Filter(WindowId),
21    /// Opens the merge form over the Accounts window.
22    Merge(Origin),
23    /// Prints the balances in the Console window.
24    Balance(Origin),
25    Report {
26        kind: ReportKind,
27        interactive: bool,
28        origin: Origin,
29    },
30}
31
32/// Typed values waiting for the options of their select fields.
33pub struct Lookup {
34    seq: u64,
35    goal: Goal,
36    form: &'static Form,
37    values: FormValues,
38    pending: Vec<Source>,
39    fetched: Fetched,
40}
41
42impl App {
43    /// Resolve the select values of `values` over `form`, then complete `goal`.
44    pub(super) fn start_lookup(&mut self, goal: Goal, form: &'static Form, values: FormValues) {
45        let Some(seq) = self.lookup_seq(goal).map(Seq::advance) else {
46            return;
47        };
48        let pending = sources_used(form, &values);
49        self.advance_lookup(Lookup {
50            seq,
51            goal,
52            form,
53            values,
54            pending,
55            fetched: Fetched::new(),
56        });
57    }
58
59    fn lookup_seq(&mut self, goal: Goal) -> Option<&mut Seq> {
60        match goal {
61            Goal::Filter(window) => self
62                .win_mut::<ListTab>(window)
63                .map(|list| &mut list.filter_seq),
64            Goal::Merge(_) => Some(&mut self.merge_seq),
65            Goal::Balance(_) => Some(&mut self.balance_seq),
66            Goal::Report { origin, .. } => self.report_seq_of(origin.window),
67        }
68    }
69
70    fn lookup_awaited(&mut self, lookup: &Lookup) -> bool {
71        let awaits = |seq: &mut Seq| seq.awaits(lookup.seq);
72        self.lookup_seq(lookup.goal).is_some_and(awaits)
73    }
74
75    fn advance_lookup(&mut self, mut lookup: Lookup) {
76        let Some(source) = lookup.pending.pop() else {
77            return self.finish_lookup(lookup);
78        };
79        let call = format!("(form-options {})", escape_str(source.name()));
80        let ctx = RouteCtx::Lookup { lookup, source };
81        if self.dispatch_app_eval(ctx, call).is_none() {
82            self.set_status("console not connected");
83        }
84    }
85
86    pub(super) fn deliver_lookup(&mut self, mut lookup: Lookup, source: Source, wire: &str) {
87        if !self.lookup_awaited(&lookup) {
88            return;
89        }
90        if let Err(e) = cli_core::render::parse_wire(wire) {
91            self.set_status(format!("command error: {e}"));
92            return;
93        }
94        lookup.fetched.push((source, parse_form_options(wire)));
95        self.advance_lookup(lookup);
96    }
97
98    fn finish_lookup(&mut self, lookup: Lookup) {
99        let Lookup {
100            goal,
101            form,
102            values,
103            fetched,
104            ..
105        } = lookup;
106        match map_values(form, &values, &fetched, "") {
107            Ok(resolved) => self.complete_lookup(goal, resolved, &fetched),
108            Err(e) => self.set_status(format!("command error: {e}")),
109        }
110    }
111
112    fn complete_lookup(&mut self, goal: Goal, resolved: FormValues, fetched: &Fetched) {
113        match goal {
114            Goal::Filter(window) => self.apply_resolved_filter(window, &resolved, fetched),
115            Goal::Merge(origin) => self.open_resolved_merge(origin, &resolved, fetched),
116            Goal::Balance(origin) => self.print_resolved_balance(origin, &resolved),
117            Goal::Report {
118                kind,
119                interactive,
120                origin,
121            } => self.launch_report(origin, kind, &resolved, interactive),
122        }
123    }
124}