1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//! Key dispatch: the app-wide keys, then whichever view owns the rest.
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use super::confirm::ConfirmAction;
use super::widgets::shell_join;
use super::*;
impl App {
pub(super) fn on_key(&mut self, key: KeyEvent) -> Option<PendingRun> {
if self.show_help {
if matches!(
key.code,
KeyCode::Char('?') | KeyCode::Esc | KeyCode::Char('q')
) {
self.show_help = false;
}
return None;
}
if self.confirm.is_some() {
return self.confirm_key(key);
}
if self.picker.is_some() {
return self.picker_key(key);
}
if self.prompt.is_some() {
return self.prompt_key(key);
}
if self.searching {
return self.search_key(key);
}
self.nav_key(key)
}
/// Typing a `/` filter. Everything printable goes into the query, so this
/// has to run before the per-view letters; the list keeps updating under it
/// and the arrows still move, which is what makes "type then Enter" work.
pub(super) fn search_key(&mut self, key: KeyEvent) -> Option<PendingRun> {
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
if ctrl && key.code == KeyCode::Char('c') {
self.should_quit = true;
return None;
}
match key.code {
// Esc drops the filter entirely; Enter keeps it and hands the keys
// back to the list, so you can search then act on what you found.
KeyCode::Esc => {
self.query.clear();
self.searching = false;
self.requery();
}
KeyCode::Enter => self.searching = false,
KeyCode::Backspace => {
self.query.pop();
self.requery();
}
KeyCode::Down => self.move_sel(1),
KeyCode::Up => self.move_sel(-1),
KeyCode::Char(c) if !ctrl => {
self.query.push(c);
self.requery();
}
_ => {}
}
None
}
pub(super) fn nav_key(&mut self, key: KeyEvent) -> Option<PendingRun> {
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
// Ctrl-C always quits, even while a per-view letter (like `c` create) is bound.
if ctrl && key.code == KeyCode::Char('c') {
self.should_quit = true;
return None;
}
// Movement - arrows and vim keys are interchangeable, and the Ctrl-chord
// variants navigate too (so the same fingers work everywhere). Views are
// the horizontal axis (←→ / h l / Tab); the list is the vertical (↑↓ / k j).
match key.code {
KeyCode::Char('q') if !ctrl => {
self.should_quit = true;
return None;
}
KeyCode::Char('?') if !ctrl => {
self.show_help = true;
return None;
}
// `/` is the filter, the same key it is in vim, less and man.
KeyCode::Char('/') if !ctrl => {
self.query.clear();
self.searching = true;
self.requery();
return None;
}
// Outside a search, Esc's only job is to undo one: clearing the
// filter is the way back to the whole list.
KeyCode::Esc if !self.query.is_empty() => {
self.query.clear();
self.requery();
self.set_status("filter cleared");
return None;
}
KeyCode::Tab | KeyCode::Right | KeyCode::Char('l') => {
self.cycle_view(1);
return None;
}
KeyCode::BackTab | KeyCode::Left | KeyCode::Char('h') => {
self.cycle_view(-1);
return None;
}
KeyCode::Down | KeyCode::Char('j') => {
self.move_sel(1);
return None;
}
KeyCode::Up | KeyCode::Char('k') => {
self.move_sel(-1);
return None;
}
_ => {}
}
// Any leftover Ctrl-chord is a navigation intent, never an action trigger.
if ctrl {
return None;
}
match self.view {
View::Connections => self.conns_key(key),
View::Passwords => self.passwords_key(key),
View::Tunnels => self.tunnels_key(key),
View::Settings => self.settings_key(key),
}
}
pub(super) fn conns_key(&mut self, key: KeyEvent) -> Option<PendingRun> {
match key.code {
KeyCode::Enter => {
let conn = self.selected_conn()?.clone();
// Handle it here rather than letting the spawn fail with an
// errno: "no such file or directory" does not tell anybody to
// install psql, let alone how.
if !self.installed[conn.engine.idx()] {
self.offer_install(conn.engine);
return None;
}
// Bring back the forward this connection depends on before
// handing over the terminal, or it opens onto a local port that
// nothing is listening on any more.
match crate::vias::ensure(&conn.key()) {
Some(Ok(msg)) => {
self.refresh_tunnels();
self.set_status(msg);
}
Some(Err(e)) => {
self.set_status(format!("could not reopen the tunnel: {e}"));
return None;
}
None => {}
}
let argv = conn.connect_argv(&self.settings);
Some(PendingRun {
label: shell_join(&argv),
argv,
connect: Some(conn),
})
}
// `c` = create, the same key in every view (the tmux convention).
KeyCode::Char('c') => {
self.pick_engine();
None
}
KeyCode::Char('e') => {
let conn = self.selected_conn()?.clone();
self.prompt = Some(Prompt::edit_conn(&conn));
None
}
KeyCode::Char('d') => {
let conn = self.selected_conn()?.clone();
self.confirm = Some(Confirm::new(
"delete connection",
format!(
"Delete '{}' from {}? The database itself is untouched; this only forgets how to reach it.",
conn.name,
crate::ini::collapse_tilde(&conn.engine.store().to_string_lossy())
),
ConfirmAction::DeleteConn {
engine: conn.engine,
name: conn.name,
},
));
None
}
// `p` sets the password for the row you are on, which is the one
// place you already know which connection you mean.
KeyCode::Char('p') => {
let conn = self.selected_conn()?.clone();
if !conn.engine.stores_password() {
self.set_status(match conn.engine {
Engine::Sqlite => "a sqlite file has no password".to_string(),
_ => format!(
"{} has no password file to write: it asks you when it opens",
conn.engine.default_client()
),
});
return None;
}
self.prompt = Some(Prompt::password(&conn));
None
}
KeyCode::Char('t') => {
let conn = self.selected_conn()?.clone();
if !conn.engine.networked() {
self.set_status("a sqlite file is already local; there is nothing to tunnel");
return None;
}
self.prompt = Some(Prompt::forward(&conn, &self.settings));
None
}
// Yank the command, since the reason to leave the toolbox for a
// connection is almost always to paste it into a script.
KeyCode::Char('y') => {
let cmd = shell_join(&self.selected_conn()?.connect_argv(&self.settings));
self.set_status(match crate::clip::copy(&cmd) {
Ok(tool) => format!("copied '{cmd}' to the clipboard ({tool})"),
Err(e) => format!("clipboard: {e}"),
});
None
}
// `Y` is the same yank one level louder: the URL a GUI, an ORM
// config or a colleague's chat window wants. Never the password.
KeyCode::Char('Y') => {
let url = self.selected_conn()?.url();
self.set_status(match crate::clip::copy(&url) {
Ok(tool) => format!("copied '{url}' to the clipboard ({tool})"),
Err(e) => format!("clipboard: {e}"),
});
None
}
KeyCode::Char('r') => {
self.refresh_conns();
self.refresh_creds();
self.start_probes();
self.set_status("reloaded every connection file, re-checking ports");
None
}
_ => None,
}
}
pub(super) fn passwords_key(&mut self, key: KeyEvent) -> Option<PendingRun> {
match key.code {
KeyCode::Char('c') => {
self.pick_password_target();
None
}
// Deleting a stored password is not undoable (we never held the
// secret to put back), so it is gated.
KeyCode::Char('d') | KeyCode::Char('x') => {
let cred = self.selected_cred()?.clone();
self.confirm = Some(Confirm::new(
"forget password",
format!(
"Remove the password for {} from {}? easysql never read it and cannot put it back.",
cred.user,
cred.where_stored()
),
ConfirmAction::ForgetPassword(cred),
));
None
}
KeyCode::Char('r') => {
self.refresh_creds();
self.set_status("reloaded ~/.pgpass and ~/.my.cnf");
None
}
_ => None,
}
}
pub(super) fn tunnels_key(&mut self, key: KeyEvent) -> Option<PendingRun> {
match key.code {
KeyCode::Char('d') | KeyCode::Char('x') => {
let pid = self.selected_tunnel()?.pid;
let _ = tunnels::kill(pid);
self.refresh_tunnels();
self.set_status(format!("killed tunnel {pid}"));
None
}
KeyCode::Char('r') => {
self.refresh_tunnels();
self.set_status("refreshed tunnels");
None
}
_ => None,
}
}
/// The Settings tab. A cycled setting changes in place on Enter; a typed one
/// opens a one-field wizard. Every change is written to the file at once,
/// so there is no unsaved state to lose or a save key to remember.
pub(super) fn settings_key(&mut self, key: KeyEvent) -> Option<PendingRun> {
match key.code {
KeyCode::Enter | KeyCode::Char('e') => {
let row = self.selected_setting()?;
match row.choices {
Some(_) => {
self.settings.cycle(row.key, 1);
let shown = self.selected_setting().map(|r| r.value).unwrap_or_default();
let msg = match self.settings.save() {
Ok(_) => format!("{} = {shown}", row.label),
Err(e) => format!("could not save settings: {e}"),
};
self.apply_settings();
self.set_status(msg);
}
None => self.prompt = Some(Prompt::edit_setting(&row)),
}
None
}
// `d` is "get rid of my answer", the same shape as delete elsewhere.
// Trivially redone, so no confirm gate.
KeyCode::Char('d') => {
let row = self.selected_setting()?;
self.settings.reset(row.key);
let _ = self.settings.save();
self.apply_settings();
self.set_status(format!("{} back to {}", row.label, row.default));
None
}
KeyCode::Char('r') => {
self.settings = crate::settings::load();
self.apply_settings();
self.set_status(format!("reloaded {}", crate::settings::path().display()));
None
}
_ => None,
}
}
}