1
//! Application state for the TUI.
2
//!
3
//! The TUI is organised as a small state machine:
4
//!
5
//! - A top-level menu bar: View raises or opens a view, Window arranges the windows.
6
//! - The windows live in a list; each owns a view, a multi-pane area managed by the view itself.
7
//! - An overlay stack sits on top of the whole lot and intercepts input
8
//!   when non-empty.
9
//! - A bottom command line is always visible.
10
//!
11
//! All of this is pure state: no rendering happens in this file. The
12
//! draw layer reads from `App` and renders; the event layer mutates
13
//! `App` via named methods so tests can drive state transitions
14
//! without a real terminal.
15

            
16
mod account_balances;
17
mod arrange;
18
mod balance;
19
mod bulk_delete;
20
mod button_press;
21
mod close;
22
mod convert;
23
mod deliver;
24
mod detail;
25
mod drag;
26
mod edit;
27
mod eval;
28
mod filter;
29
mod form_options;
30
mod hold;
31
mod lookup;
32
mod menu;
33
mod merge;
34
mod mutation;
35
mod open;
36
mod paging;
37
mod pending;
38
mod refresh;
39
mod report_args;
40
mod report_chart;
41
mod report_csv;
42
mod report_rerun;
43
mod scripts;
44
mod ssh_keys;
45
mod tag_hints;
46
mod tags;
47
mod template_pick;
48
mod template_test;
49
mod templates;
50
mod ui_config;
51
mod viewer;
52
mod window_list;
53
mod windows;
54

            
55
#[cfg(test)]
56
pub(crate) use button_press::KEY_PRESS;
57
pub(crate) use button_press::button_of_key;
58
pub(crate) use hold::{Face, Owner};
59
pub use ui_config::{CliConfig, parse_colors};
60

            
61
use std::collections::HashMap;
62

            
63
pub use lookup::Lookup;
64
pub use menu::{MenuEntry, MenuId};
65
pub use pending::PendingForm;
66

            
67
use crate::hits::Hits;
68
use crate::keymap::{Keys, VimState};
69
use crate::modal::{ConfirmAction, Modal};
70
use crate::mouse::Click;
71
use crate::overlay::OverlayStack;
72
use crate::route::Route;
73
use crate::tabs::nms_eval::ConsoleEval;
74
use crate::tabs::reports::chart::TerminalState;
75
use crate::tabs::seq::Seq;
76
use crate::view::Tab;
77
use crate::widgets::Editor;
78
use crate::window::{Win, WindowId};
79
use sqlx::types::Uuid;
80
use tvui::{ColorDepth, Desktop, RowWindow, Theme};
81
use ui_config::UiConfig;
82

            
83
/// Command-line palette editor state.
84
pub struct CmdLine {
85
    pub editor: Editor,
86
    pub active: bool,
87
    /// Completion candidates for the current buffer, refreshed on each Tab press.
88
    pub completions: Vec<String>,
89
}
90

            
91
impl CmdLine {
92
2101
    fn new() -> Self {
93
2101
        Self {
94
2101
            editor: Editor::default(),
95
2101
            active: false,
96
2101
            completions: Vec::new(),
97
2101
        }
98
2101
    }
99
}
100

            
101
pub struct App {
102
    pub user_id: Uuid,
103
    pub theme: Theme,
104
    ui_config: UiConfig,
105
    pub overlays: OverlayStack,
106
    /// The submitted form awaiting its reply; owns input focus while set.
107
    pub pending_form: Option<PendingForm>,
108
    pub cmdline: CmdLine,
109
    /// The open menu and its selected entry; `None` while it is closed.
110
    pub menu: Option<(MenuId, usize)>,
111
    /// Which entries of the open menu are in view.
112
    pub menu_window: RowWindow,
113
    /// What the mouse, a key or the size/move mode holds: a drag, or a press awaiting its release or its time.
114
    pub(crate) hold: Option<hold::Hold>,
115
    /// The clickable areas of the last frame drawn.
116
    pub hits: Hits,
117
    /// The last left click on a list row, for the double click.
118
    pub(crate) last_click: Option<Click>,
119
    pub keys: Keys,
120
    pub status: String,
121
    /// The window whose failed reply wrote `status`; only its next success clears it.
122
    status_owner: Option<WindowId>,
123
    pub should_quit: bool,
124
    /// The async eval bridge. `None` until attached via [`App::attach_console`].
125
    console_eval: Option<ConsoleEval>,
126
    /// Routes pending eval reply ids to their destination view.
127
    pending_routes: HashMap<i64, Route>,
128
    /// Monotonic counter for form-options fetches; shared across all forms
129
    /// that populate Selects. A stale reply (wrong seq) is silently dropped.
130
    form_options_seq: Seq,
131
    /// Counts tag-list suggestion lookups; the form drops an answer of an older one.
132
    suggest_seq: Seq,
133
    /// Counts palette account-merge lookups; a reply of an older one is dropped.
134
    merge_seq: Seq,
135
    /// Counts palette account-balance lookups; a reply of an older one is dropped.
136
    balance_seq: Seq,
137
    /// Counts template tests; a reply of an older one is dropped.
138
    template_test_seq: Seq,
139
    /// Counts template picker requests; a reply of an older one is dropped.
140
    template_pick_seq: Seq,
141
    /// Whether the transport runs on the user's machine, so saving a file is allowed.
142
    pub local_files: bool,
143
    /// Raw terminal output queued for the transport's passthrough.
144
    outbound: Vec<Vec<u8>>,
145
    bulk: bulk_delete::BulkDelete,
146
    windows: Vec<Win>,
147
    /// The id the next window gets; ids are never reused.
148
    next_window: u32,
149
    /// Order, rect and zoom of the windows; the front one is the active view.
150
    desktop: Desktop<WindowId>,
151
    /// What the terminal is and the chart backend the user forced, for every Reports window.
152
    pub(crate) terminal: TerminalState,
153
}
154

            
155
impl App {
156
    /// The app with the theme and keymap the command line chose; `auto_depth` is what `auto` colours mean.
157
    ///
158
    /// # Errors
159
    /// The message of a command-line theme or a keymap preset that does not build.
160
2102
    pub fn new(user_id: Uuid, cli: CliConfig, auto_depth: ColorDepth) -> Result<Self, String> {
161
2102
        let ui_config = UiConfig::new(cli, auto_depth);
162
2102
        let (theme, _) = ui_config.build([None, None, None])?;
163
2101
        let (keys, _) = ui_config.keys(None, None)?;
164
2101
        Ok(Self {
165
2101
            user_id,
166
2101
            ui_config,
167
2101
            theme,
168
2101
            overlays: OverlayStack::new(),
169
2101
            pending_form: None,
170
2101
            cmdline: CmdLine::new(),
171
2101
            menu: None,
172
2101
            menu_window: RowWindow::default(),
173
2101
            hold: None,
174
2101
            hits: Hits::default(),
175
2101
            last_click: None,
176
2101
            keys,
177
2101
            status: String::new(),
178
2101
            status_owner: None,
179
2101
            should_quit: false,
180
2101
            console_eval: None,
181
2101
            pending_routes: HashMap::new(),
182
2101
            form_options_seq: Seq::default(),
183
2101
            suggest_seq: Seq::default(),
184
2101
            merge_seq: Seq::default(),
185
2101
            balance_seq: Seq::default(),
186
2101
            template_test_seq: Seq::default(),
187
2101
            template_pick_seq: Seq::default(),
188
2101
            local_files: false,
189
2101
            outbound: Vec::new(),
190
2101
            bulk: bulk_delete::BulkDelete::default(),
191
2101
            windows: Vec::new(),
192
2101
            next_window: 0,
193
2101
            desktop: Desktop::default(),
194
2101
            terminal: TerminalState::default(),
195
2101
        })
196
2102
    }
197

            
198
778
    pub fn switch_tab(&mut self, tab: Tab) {
199
778
        let window = self.window_or_open(tab).window;
200
778
        self.activate_window(window);
201
778
    }
202

            
203
214
    pub fn open_command_line(&mut self) {
204
214
        self.cmdline.editor = Editor::default();
205
214
        self.keys.set_vim(VimState::Insert);
206
214
        self.cmdline.active = true;
207
214
        self.cmdline.completions.clear();
208
214
    }
209

            
210
144
    pub fn close_command_line(&mut self) {
211
144
        self.cmdline.active = false;
212
144
        self.cmdline.completions.clear();
213
144
    }
214

            
215
2417
    pub fn set_status(&mut self, msg: impl Into<String>) {
216
2417
        self.status = msg.into();
217
2417
        self.status_owner = None;
218
2417
    }
219

            
220
2175
    pub fn clear_status(&mut self) {
221
2175
        self.set_status("");
222
2175
    }
223

            
224
26
    pub fn confirm_quit(&mut self) {
225
26
        self.overlays
226
26
            .push(Modal::confirm("Quit?", ConfirmAction::Quit));
227
26
    }
228

            
229
29
    pub fn request_quit(&mut self) {
230
29
        self.should_quit = true;
231
29
    }
232

            
233
    /// Attaches the console and reads the UI config.
234
91
    pub fn attach_console(&mut self, eval: ConsoleEval) {
235
91
        self.console_eval = Some(eval);
236
91
        self.read_ui_config();
237
91
    }
238
}
239

            
240
#[cfg(test)]
241
pub(crate) mod tests;