purple-ssh 3.22.0

Open-source terminal SSH manager that keeps ~/.ssh/config in sync with your cloud infra. Spin up a VM on AWS, GCP, Azure, Hetzner or 12 other cloud providers and it appears in your host list. Destroy it and the entry dims. Search hundreds of hosts, transfer files, manage Docker and Podman over SSH, sign Vault SSH certs. Rust TUI, MIT licensed.
Documentation
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Key handler for the Snippets tab (`TopPage::Snippets`, `Screen::HostList`).
//!
//! Navigation, search, CRUD (delegated to the shared snippet form/delete flow,
//! returning to the tab) and the snippet -> hosts run flow (opens the host
//! picker). Mirrors the other tab handlers' Tab/Shift+Tab and search rhythm.

use crossterm::event::{KeyCode, KeyEvent};

use crate::app::{App, Screen};

pub(super) fn handle_key(app: &mut App, key: KeyEvent) {
    // A pending delete confirmation owns input until resolved (shared with the
    // host-list picker so the dialog behaves identically).
    if app.snippets.pending_delete().is_some() {
        super::snippet::confirm_pending_snippet_delete(app, key);
        return;
    }
    if app.search.query().is_some() {
        handle_search_key(app, key);
        return;
    }
    let count = crate::snippet::filtered_indices(app.snippets.store(), app.search.query()).len();
    match key.code {
        KeyCode::Tab => {
            app.cycle_top_page_next();
            app.search.set_query(None);
        }
        KeyCode::BackTab => {
            app.cycle_top_page_prev();
            app.search.set_query(None);
        }
        KeyCode::Char('j') | KeyCode::Down => move_cursor(app, count, 1),
        KeyCode::Char('k') | KeyCode::Up => move_cursor(app, count, -1),
        KeyCode::PageDown => move_cursor(app, count, 10),
        KeyCode::PageUp => move_cursor(app, count, -10),
        KeyCode::Home | KeyCode::Char('g') if count > 0 => {
            app.snippets.list_state_mut().select(Some(0));
        }
        KeyCode::End | KeyCode::Char('G') if count > 0 => {
            app.snippets.list_state_mut().select(Some(count - 1));
        }
        KeyCode::Char('/') => {
            app.search.set_query(Some(String::new()));
            if count > 0 {
                app.snippets.list_state_mut().select(Some(0));
            }
        }
        KeyCode::Char('v') => {
            app.snippets.toggle_view_mode();
            app.ui.set_detail_toggle_pending(true);
        }
        KeyCode::Char('?') => {
            // return_screen = HostList because TopPage::Snippets shares the
            // HostList screen variant; the help dispatcher reads app.top_page
            // to pick the tab-specific column content.
            app.set_screen(Screen::Help {
                return_screen: Box::new(Screen::HostList),
            });
        }
        KeyCode::Char('a') => super::snippet::open_add_form_for_tab(app),
        KeyCode::Char('e') => open_edit(app),
        KeyCode::Char('d') => request_delete(app),
        KeyCode::Enter | KeyCode::Char('r') => open_host_picker(app, false),
        KeyCode::Char('!') => open_host_picker(app, true),
        // Standard cross-tab keys, identical to the other overview tabs.
        KeyCode::Char(':') => {
            log::debug!("[purple] jump: opened from snippets overview");
            // Open in Snippets mode so the empty-state biases snippet actions to
            // the front, matching how the other tabs bias their own domain.
            app.open_jump(crate::app::JumpMode::Snippets);
        }
        KeyCode::Char('n') => {
            super::whats_new::dismiss_whats_new_toast(app);
            app.set_screen(Screen::WhatsNew(crate::app::WhatsNewState::default()));
        }
        KeyCode::Char('q') => {
            app.running = false;
        }
        KeyCode::Esc
            if !app.ui.esc_quit_hint_shown()
                && !app.status_center.toast().is_some_and(|t| t.sticky) =>
        {
            app.notify(crate::messages::ESC_QUIT_HINT);
            app.ui.set_esc_quit_hint_shown(true);
        }
        _ => {}
    }
}

fn handle_search_key(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc | KeyCode::Enter => {
            // Esc clears the filter; Enter confirms it and stays on the list.
            // Either way input returns to the main keymap.
            if matches!(key.code, KeyCode::Esc) {
                app.search.set_query(None);
            }
        }
        KeyCode::Backspace => {
            if let Some(q) = app.search.query() {
                let mut q = q.to_string();
                q.pop();
                app.search.set_query(Some(q));
            }
        }
        KeyCode::Char(c) => {
            if let Some(q) = app.search.query() {
                let mut q = q.to_string();
                q.push(c);
                app.search.set_query(Some(q));
            }
        }
        _ => {}
    }
    // Keep the cursor in range as the filtered list shrinks/grows.
    let count = crate::snippet::filtered_indices(app.snippets.store(), app.search.query()).len();
    if count == 0 {
        app.snippets.list_state_mut().select(None);
    } else {
        app.snippets.list_state_mut().select(Some(0));
    }
}

fn move_cursor(app: &mut App, count: usize, delta: i64) {
    if count == 0 {
        return;
    }
    let cur = app.snippets.list_state().selected().unwrap_or(0) as i64;
    let next = (cur + delta).clamp(0, count as i64 - 1) as usize;
    app.snippets.list_state_mut().select(Some(next));
}

/// Resolve the snippet under the cursor, translating the filtered index back to
/// a store index, into `(store_idx, snippet)`.
fn selected_snippet(app: &App) -> Option<(usize, crate::snippet::Snippet)> {
    let indices = crate::snippet::filtered_indices(app.snippets.store(), app.search.query());
    let cursor = app.snippets.list_state().selected()?;
    let store_idx = *indices.get(cursor)?;
    let snippet = app.snippets.store().snippets.get(store_idx)?.clone();
    Some((store_idx, snippet))
}

fn open_edit(app: &mut App) {
    if let Some((store_idx, _)) = selected_snippet(app) {
        super::snippet::open_edit_form_for_tab(app, store_idx);
    }
}

fn request_delete(app: &mut App) {
    if let Some((store_idx, _)) = selected_snippet(app) {
        app.snippets.request_delete(store_idx);
    }
}

fn open_host_picker(app: &mut App, terminal: bool) {
    if app.hosts_state.list().is_empty() {
        app.notify_warning(crate::messages::PICKER_NO_HOSTS);
        return;
    }
    let Some((_, snippet)) = selected_snippet(app) else {
        return;
    };
    app.snippets.set_flow_terminal(terminal);
    app.snippets.reset_host_pick();
    // Pre-select the snippet's saved default targets that still exist, so a
    // repeat run is just Enter -> confirm.
    let existing: std::collections::HashSet<String> = app
        .hosts_state
        .list()
        .iter()
        .map(|h| h.alias.clone())
        .collect();
    let saved: Vec<String> = app
        .snippets
        .store()
        .targets_for(&snippet.name)
        .iter()
        .filter(|a| existing.contains(*a))
        .cloned()
        .collect();
    for alias in saved {
        app.snippets.host_pick_mut().selected.insert(alias);
    }
    app.snippets.set_flow_snippet(Some(snippet));
    app.set_screen(Screen::SnippetHostPicker);
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::app::{App, TopPage};
    use crate::ssh_config::model::SshConfigFile;
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    fn make_app(content: &str) -> App {
        let scratch = tempfile::tempdir().expect("tempdir").keep();
        let config = SshConfigFile {
            elements: SshConfigFile::parse_content(content),
            path: scratch.join("cfg"),
            crlf: false,
            bom: false,
        };
        let mut app = App::new(config);
        app.top_page = TopPage::Snippets;
        app.snippets.store_mut().snippets = vec![
            crate::snippet::Snippet {
                name: "deploy".into(),
                command: "make".into(),
                description: String::new(),
            },
            crate::snippet::Snippet {
                name: "uptime".into(),
                command: "uptime".into(),
                description: String::new(),
            },
        ];
        app.snippets.list_state_mut().select(Some(0));
        app
    }

    fn k(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::NONE)
    }

    #[test]
    fn tab_cycles_to_keys() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        handle_key(&mut app, k(KeyCode::Tab));
        assert_eq!(app.top_page, TopPage::Keys);
    }

    #[test]
    fn backtab_cycles_to_containers() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        handle_key(&mut app, k(KeyCode::BackTab));
        assert_eq!(app.top_page, TopPage::Containers);
    }

    #[test]
    fn j_k_move_within_bounds() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        handle_key(&mut app, k(KeyCode::Char('j')));
        assert_eq!(app.snippets.list_state().selected(), Some(1));
        handle_key(&mut app, k(KeyCode::Char('j')));
        assert_eq!(app.snippets.list_state().selected(), Some(1));
        handle_key(&mut app, k(KeyCode::Char('k')));
        assert_eq!(app.snippets.list_state().selected(), Some(0));
    }

    #[test]
    fn v_toggles_view_mode_and_marks_anim_pending() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        let before = app.snippets.view_mode();
        handle_key(&mut app, k(KeyCode::Char('v')));
        assert_ne!(app.snippets.view_mode(), before);
        assert!(app.ui.detail_toggle_pending());
    }

    #[test]
    fn enter_opens_host_picker_with_chosen_snippet() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        handle_key(&mut app, k(KeyCode::Enter));
        assert!(matches!(app.screen, Screen::SnippetHostPicker));
        assert_eq!(
            app.snippets.flow_snippet().map(|s| s.name.as_str()),
            Some("deploy")
        );
        assert!(!app.snippets.flow_terminal());
    }

    #[test]
    fn enter_preselects_saved_default_targets() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\nHost h2\n  HostName 2.2.2.2\n");
        // deploy is store index 0 in make_app; give it a saved default host.
        app.snippets
            .store_mut()
            .set_targets("deploy", vec!["h1".into()]);
        app.snippets.list_state_mut().select(Some(0));
        handle_key(&mut app, k(KeyCode::Enter));
        assert!(matches!(app.screen, Screen::SnippetHostPicker));
        assert!(app.snippets.host_pick().selected.contains("h1"));
        assert!(!app.snippets.host_pick().selected.contains("h2"));
    }

    #[test]
    fn enter_with_no_hosts_notifies_and_stays() {
        let mut app = make_app("");
        handle_key(&mut app, k(KeyCode::Enter));
        assert!(matches!(app.screen, Screen::HostList));
        assert!(app.status_center.toast().is_some());
    }

    #[test]
    fn e_opens_edit_form_for_selected_snippet_returning_to_tab() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        app.snippets.list_state_mut().select(Some(1)); // uptime (store idx 1)
        handle_key(&mut app, k(KeyCode::Char('e')));
        assert!(matches!(app.screen, Screen::SnippetForm));
        assert!(app.snippets.form_return_to_tab());
        assert_eq!(app.snippets.form_editing(), Some(1));
        assert_eq!(app.snippets.form().name, "uptime");
    }

    #[test]
    fn bang_opens_host_picker_in_terminal_mode() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        handle_key(&mut app, k(KeyCode::Char('!')));
        assert!(matches!(app.screen, Screen::SnippetHostPicker));
        assert!(app.snippets.flow_terminal());
    }

    #[test]
    fn r_opens_host_picker_like_enter() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        handle_key(&mut app, k(KeyCode::Char('r')));
        assert!(matches!(app.screen, Screen::SnippetHostPicker));
        assert!(!app.snippets.flow_terminal());
    }

    #[test]
    fn deleting_last_snippet_from_tab_clamps_cursor() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        // Cursor on the last of two snippets; delete it.
        app.snippets.list_state_mut().select(Some(1));
        handle_key(&mut app, k(KeyCode::Char('d')));
        assert_eq!(app.snippets.pending_delete(), Some(1));
        handle_key(&mut app, k(KeyCode::Char('y')));
        assert_eq!(app.snippets.store().snippets.len(), 1);
        assert_eq!(app.snippets.store().snippets[0].name, "deploy");
        // Tab cursor clamped back into range, not dangling at the old index 1.
        assert_eq!(app.snippets.list_state().selected(), Some(0));
    }

    #[test]
    fn slash_enters_search_mode() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        handle_key(&mut app, k(KeyCode::Char('/')));
        assert!(app.search.query().is_some());
    }

    #[test]
    fn search_filters_and_selected_snippet_resolves_store_index() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        handle_key(&mut app, k(KeyCode::Char('/')));
        for c in "uptime".chars() {
            handle_key(&mut app, k(KeyCode::Char(c)));
        }
        // Filtered list now holds only "uptime" (store index 1) at cursor 0.
        let (idx, snip) = selected_snippet(&app).expect("a snippet");
        assert_eq!(idx, 1);
        assert_eq!(snip.name, "uptime");
    }

    #[test]
    fn d_requests_delete_and_confirm_y_removes() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        handle_key(&mut app, k(KeyCode::Char('d')));
        assert_eq!(app.snippets.pending_delete(), Some(0));
        handle_key(&mut app, k(KeyCode::Char('y')));
        assert!(app.snippets.pending_delete().is_none());
        assert_eq!(app.snippets.store().snippets.len(), 1);
        assert_eq!(app.snippets.store().snippets[0].name, "uptime");
    }

    #[test]
    fn deleting_snippet_drops_its_run_ledger_entry() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        // Give "deploy" (store idx 0) a recorded run.
        app.snippets.runs_mut().record(
            "deploy",
            crate::snippet_runs::RunRecord {
                ts: 1,
                hosts: 2,
                ok: 2,
                failed: 0,
            },
        );
        assert_eq!(app.snippets.runs().run_count("deploy"), 1);
        app.snippets.list_state_mut().select(Some(0));
        handle_key(&mut app, k(KeyCode::Char('d')));
        handle_key(&mut app, k(KeyCode::Char('y')));
        // Snippet gone AND its ledger entry gone, so a recreated "deploy" starts
        // with a clean TRACK RECORD.
        assert!(app.snippets.store().get("deploy").is_none());
        assert_eq!(app.snippets.runs().run_count("deploy"), 0);
    }

    #[test]
    fn a_opens_add_form_returning_to_tab() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        handle_key(&mut app, k(KeyCode::Char('a')));
        assert!(matches!(app.screen, Screen::SnippetForm));
        assert!(app.snippets.form_return_to_tab());
    }

    #[test]
    fn question_opens_help_returning_to_host_list() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        handle_key(&mut app, k(KeyCode::Char('?')));
        match &app.screen {
            Screen::Help { return_screen } => {
                assert!(matches!(**return_screen, Screen::HostList));
            }
            other => panic!("expected Help, got {other:?}"),
        }
    }

    #[test]
    fn q_quits() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        assert!(app.running);
        handle_key(&mut app, k(KeyCode::Char('q')));
        assert!(!app.running);
    }

    #[test]
    fn colon_opens_jump_palette_in_snippets_mode() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        handle_key(&mut app, k(KeyCode::Char(':')));
        let jump = app.jump.as_ref().expect("jump open");
        assert_eq!(jump.mode(), crate::app::JumpMode::Snippets);
    }

    #[test]
    fn n_opens_whats_new() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        handle_key(&mut app, k(KeyCode::Char('n')));
        assert!(matches!(app.screen, Screen::WhatsNew(_)));
    }

    #[test]
    fn esc_on_idle_shows_quit_hint() {
        let mut app = make_app("Host h1\n  HostName 1.1.1.1\n");
        handle_key(&mut app, k(KeyCode::Esc));
        assert!(app.ui.esc_quit_hint_shown());
        assert!(app.status_center.toast().is_some());
    }
}