1
//! Running a command-palette line: parse, resolve, then open the form or tab it names.
2

            
3
use super::modal_open::{
4
    open_account_create_modal, open_account_edit_for_selected, open_commodity_convert_modal,
5
    open_commodity_create_modal, open_commodity_edit_for_selected, open_config_set_modal,
6
    open_ssh_key_add_modal, open_ssh_key_remove, open_transaction_create_modal,
7
};
8
use crate::app::App;
9
use crate::command::{
10
    build_account_balance, build_account_merge, build_form, build_report_request,
11
    build_transaction_filter,
12
};
13
use crate::palette;
14
use crate::view::Tab;
15
use cli_core::command_tree;
16

            
17
/// Parse a command-palette input and act on the resolved command.
18
155
pub(super) fn submit_palette(app: &mut App, input: &str) {
19
155
    let query = palette::parse(input);
20
155
    if query.path.is_empty() {
21
        return;
22
155
    }
23
155
    let tree = command_tree();
24
155
    match palette::resolve(&tree, &query) {
25
148
        Some(_) => apply_resolved_command(app, &query.path, &query.args),
26
7
        None => app.set_status(format!("unknown command: {}", query.path.join(" "))),
27
    }
28
155
}
29

            
30
148
fn apply_resolved_command(app: &mut App, path: &[String], args: &[(String, String)]) {
31
148
    let path_str = path.join(" ");
32
148
    if path_str.starts_with("reports ") {
33
52
        apply_report_command(app, path, args);
34
52
        return;
35
96
    }
36
96
    if path_str == "account balance" {
37
9
        return apply_account_balance(app, args);
38
87
    }
39
87
    match build_form(path, args) {
40
15
        Err(e) => app.set_status(format!("command error: {e}")),
41
        Ok(None) => {
42
66
            let segments: Vec<&str> = path.iter().map(String::as_str).collect();
43
66
            match segments.as_slice() {
44
3
                ["config", "set"] => open_config_set_modal(app, args),
45
2
                ["ssh-key", "add"] => open_ssh_key_add_modal(app, args),
46
2
                ["ssh-key", "list"] => app.switch_tab(Tab::SshKeys),
47
3
                ["ssh-key", "remove"] => open_ssh_key_remove(app, fingerprint_arg(args)),
48
1
                ["account", "create"] => open_account_create_modal(app),
49
17
                ["account", "merge"] => open_account_merge_from_palette(app, args),
50
2
                ["account", "list"] => app.switch_tab(Tab::Accounts),
51
                ["account", "edit"] => {
52
3
                    edit_selected(app, Tab::Accounts, open_account_edit_for_selected)
53
                }
54
2
                ["commodity", "list"] => app.switch_tab(Tab::Commodities),
55
                ["commodity", "edit"] => {
56
5
                    edit_selected(app, Tab::Commodities, open_commodity_edit_for_selected);
57
                }
58
1
                ["commodity", "create"] => open_commodity_create_modal(app),
59
1
                ["commodity", "convert"] => open_commodity_convert_modal(app),
60
4
                ["transaction", "create"] => open_transaction_create_modal(app),
61
20
                ["transaction", "list"] => apply_transaction_list(app, args),
62
                _ => app.set_status(format!("unknown command: {path_str}")),
63
            }
64
        }
65
        Ok(Some(form)) => {
66
6
            let console = app.submit_to_console(form);
67
6
            app.activate_window(console);
68
        }
69
    }
70
148
}
71

            
72
/// Raises the window of `kind` and edits its selection.
73
8
fn edit_selected(app: &mut App, kind: Tab, open: fn(&mut App)) {
74
8
    if app.raise_kind(kind) {
75
7
        open(app);
76
1
    }
77
8
}
78

            
79
3
fn fingerprint_arg(args: &[(String, String)]) -> Option<String> {
80
3
    args.iter()
81
1
        .find(|(key, value)| key == "fingerprint" && !value.is_empty())
82
1
        .map(|(_, value)| value.clone())
83
3
}
84

            
85
/// Named accounts are resolved by label or id; without a `from` the selected
86
/// account (Accounts tab) is it.
87
17
fn open_account_merge_from_palette(app: &mut App, args: &[(String, String)]) {
88
17
    let mut values = match build_account_merge(args) {
89
13
        Ok(values) => values,
90
4
        Err(e) => return app.set_status(format!("command error: {e}")),
91
    };
92
13
    if values.text("from").is_none()
93
4
        && let Some(id) = app.selected_id(Tab::Accounts)
94
    {
95
2
        values.set("from", &id);
96
11
    }
97
13
    app.open_account_merge(values);
98
17
}
99

            
100
20
fn apply_transaction_list(app: &mut App, args: &[(String, String)]) {
101
20
    match build_transaction_filter(args) {
102
3
        Err(e) => app.set_status(format!("command error: {e}")),
103
17
        Ok(_) if args.iter().all(|(_, value)| value.is_empty()) => app.show_all_transactions(),
104
13
        Ok(filter) => app.filter_transactions(filter),
105
    }
106
20
}
107

            
108
52
fn apply_report_command(app: &mut App, path: &[String], args: &[(String, String)]) {
109
52
    match build_report_request(path, args) {
110
5
        Err(e) => app.set_status(format!("command error: {e}")),
111
        Ok(None) => app.set_status("unknown report command".to_string()),
112
47
        Ok(Some(req)) => app.run_report_request(req),
113
    }
114
52
}
115

            
116
9
fn apply_account_balance(app: &mut App, args: &[(String, String)]) {
117
9
    match build_account_balance(args) {
118
5
        Err(e) => app.set_status(format!("command error: {e}")),
119
4
        Ok(account) => app.show_account_balance(&account),
120
    }
121
9
}