purple-ssh 3.15.18

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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! UI selection substate: list cursors, picker overlays, scroll offsets.

use ratatui::widgets::ListState;

use crate::ui::theme::ThemeDef;

/// A picker overlay: open flag plus its list cursor.
#[derive(Debug, Default)]
pub struct PickerState {
    pub open: bool,
    pub list: ListState,
}

#[allow(dead_code)]
impl PickerState {
    /// Open the picker with the cursor positioned at `index`.
    pub fn open_at(&mut self, index: usize) {
        self.open = true;
        self.list.select(Some(index));
    }

    /// Close the picker and reset the cursor.
    pub fn close(&mut self) {
        self.open = false;
        self.list.select(None);
    }
}

/// Theme picker carries extra catalogue + preview state beyond a simple list.
#[derive(Debug, Default)]
pub struct ThemePickerState {
    pub list: ListState,
    pub builtins: Vec<ThemeDef>,
    pub custom: Vec<ThemeDef>,
    pub saved_name: String,
    pub original: Option<ThemeDef>,
}

/// Region picker uses a cursor index rather than a ListState because region
/// rows are a synthetic flat array (provider × region pairs) rather than a
/// ratatui-managed list.
#[derive(Debug, Default)]
pub struct RegionPickerState {
    pub open: bool,
    pub cursor: usize,
}

#[derive(Debug, Default)]
pub struct UiSelection {
    pub(in crate::app) list_state: ListState,
    pub(in crate::app) key_picker: PickerState,
    pub(in crate::app) password_picker: PickerState,
    pub(in crate::app) proxyjump_picker: PickerState,
    pub(in crate::app) vault_role_picker: PickerState,
    pub(in crate::app) tag_picker_state: ListState,
    pub(in crate::app) bulk_tag_editor_state: ListState,
    pub(in crate::app) theme_picker: ThemePickerState,
    pub(in crate::app) provider_list_state: ListState,
    pub(in crate::app) tunnel_list_state: ListState,
    pub(in crate::app) tunnels_overview_state: ListState,
    pub(in crate::app) containers_overview_state: ListState,
    /// Cursor for the host picker reached from the tunnels overview when
    /// adding a new tunnel. Indexes into the editable-hosts slice built at
    /// render time (hosts from included files are excluded).
    pub(in crate::app) tunnel_host_picker_state: ListState,
    /// Live fuzzy-search query for the tunnel host picker. Always-on input
    /// mode: every printable keystroke appends to the query and shrinks the
    /// candidate set. Empty string means "show all".
    pub(in crate::app) tunnel_host_picker_query: String,
    /// Cursor + live query for the containers-tab `a` host picker.
    /// Mirrors the tunnel host picker pair; kept separate so the two
    /// pickers can be open back-to-back without state bleed.
    pub(in crate::app) container_host_picker_state: ListState,
    pub(in crate::app) container_host_picker_query: String,
    pub(in crate::app) snippet_picker_state: ListState,
    pub(in crate::app) snippet_search: Option<String>,
    pub(in crate::app) region_picker: RegionPickerState,
    pub(in crate::app) help_scroll: u16,
    pub(in crate::app) detail_scroll: u16,
    /// Set by handler, consumed by AnimationState to trigger detail panel transition.
    pub(in crate::app) detail_toggle_pending: bool,
    /// Tracks when the welcome screen was opened to auto-dismiss it.
    pub(in crate::app) welcome_opened: Option<std::time::Instant>,
    /// Set once the first time Esc-on-empty-list hint is shown per process.
    pub(in crate::app) esc_quit_hint_shown: bool,
    /// Welcome-screen heuristic: number of known hosts at last render.
    pub(in crate::app) known_hosts_count: usize,
    /// Pending SSH dispatch queued by connect actions; consumed by the event loop.
    pub(in crate::app) pending_connect: Option<(String, Option<String>)>,
}

impl UiSelection {
    /// Construct with all picker/list state defaulted and the host list
    /// selection pre-positioned at `initial` (the first selectable host or
    /// pattern in the display list).
    pub fn new_with_initial_selection(initial: Option<usize>) -> Self {
        let mut s = Self::default();
        if let Some(pos) = initial {
            s.list_state.select(Some(pos));
        }
        s
    }

    /// Queue an SSH connect for the event loop to pick up via the next
    /// `pending_connect.take()`. `askpass` carries the resolved per-host
    /// password source so the event loop can prepare a SSH_ASKPASS env
    /// before spawning the child.
    pub fn queue_connect(&mut self, alias: String, askpass: Option<String>) {
        self.pending_connect = Some((alias, askpass));
    }

    /// Enter snippet picker search mode with an empty query.
    pub fn open_snippet_search(&mut self) {
        self.snippet_search = Some(String::new());
    }

    /// Exit snippet picker search mode. Idempotent.
    pub fn close_snippet_search(&mut self) {
        self.snippet_search = None;
    }

    pub fn list_state(&self) -> &ListState {
        &self.list_state
    }

    pub fn list_state_mut(&mut self) -> &mut ListState {
        &mut self.list_state
    }

    pub fn key_picker(&self) -> &PickerState {
        &self.key_picker
    }

    pub fn key_picker_mut(&mut self) -> &mut PickerState {
        &mut self.key_picker
    }

    pub fn password_picker(&self) -> &PickerState {
        &self.password_picker
    }

    pub fn password_picker_mut(&mut self) -> &mut PickerState {
        &mut self.password_picker
    }

    pub fn proxyjump_picker(&self) -> &PickerState {
        &self.proxyjump_picker
    }

    pub fn proxyjump_picker_mut(&mut self) -> &mut PickerState {
        &mut self.proxyjump_picker
    }

    pub fn vault_role_picker(&self) -> &PickerState {
        &self.vault_role_picker
    }

    pub fn vault_role_picker_mut(&mut self) -> &mut PickerState {
        &mut self.vault_role_picker
    }

    pub fn tag_picker_state(&self) -> &ListState {
        &self.tag_picker_state
    }

    pub fn tag_picker_state_mut(&mut self) -> &mut ListState {
        &mut self.tag_picker_state
    }

    pub fn bulk_tag_editor_state(&self) -> &ListState {
        &self.bulk_tag_editor_state
    }

    pub fn bulk_tag_editor_state_mut(&mut self) -> &mut ListState {
        &mut self.bulk_tag_editor_state
    }

    pub fn theme_picker(&self) -> &ThemePickerState {
        &self.theme_picker
    }

    pub fn theme_picker_mut(&mut self) -> &mut ThemePickerState {
        &mut self.theme_picker
    }

    pub fn provider_list_state(&self) -> &ListState {
        &self.provider_list_state
    }

    pub fn provider_list_state_mut(&mut self) -> &mut ListState {
        &mut self.provider_list_state
    }

    pub fn tunnel_list_state(&self) -> &ListState {
        &self.tunnel_list_state
    }

    pub fn tunnel_list_state_mut(&mut self) -> &mut ListState {
        &mut self.tunnel_list_state
    }

    pub fn tunnels_overview_state(&self) -> &ListState {
        &self.tunnels_overview_state
    }

    pub fn tunnels_overview_state_mut(&mut self) -> &mut ListState {
        &mut self.tunnels_overview_state
    }

    pub fn containers_overview_state(&self) -> &ListState {
        &self.containers_overview_state
    }

    pub fn containers_overview_state_mut(&mut self) -> &mut ListState {
        &mut self.containers_overview_state
    }

    pub fn tunnel_host_picker_state(&self) -> &ListState {
        &self.tunnel_host_picker_state
    }

    pub fn tunnel_host_picker_state_mut(&mut self) -> &mut ListState {
        &mut self.tunnel_host_picker_state
    }

    pub fn tunnel_host_picker_query(&self) -> &String {
        &self.tunnel_host_picker_query
    }

    pub fn tunnel_host_picker_query_mut(&mut self) -> &mut String {
        &mut self.tunnel_host_picker_query
    }

    pub fn set_tunnel_host_picker_query(&mut self, query: String) {
        self.tunnel_host_picker_query = query;
    }

    pub fn container_host_picker_state(&self) -> &ListState {
        &self.container_host_picker_state
    }

    pub fn container_host_picker_state_mut(&mut self) -> &mut ListState {
        &mut self.container_host_picker_state
    }

    pub fn container_host_picker_query(&self) -> &String {
        &self.container_host_picker_query
    }

    pub fn container_host_picker_query_mut(&mut self) -> &mut String {
        &mut self.container_host_picker_query
    }

    pub fn set_container_host_picker_query(&mut self, query: String) {
        self.container_host_picker_query = query;
    }

    pub fn snippet_picker_state(&self) -> &ListState {
        &self.snippet_picker_state
    }

    pub fn snippet_picker_state_mut(&mut self) -> &mut ListState {
        &mut self.snippet_picker_state
    }

    pub fn snippet_search(&self) -> Option<&String> {
        self.snippet_search.as_ref()
    }

    pub fn snippet_search_mut(&mut self) -> Option<&mut String> {
        self.snippet_search.as_mut()
    }

    pub fn region_picker(&self) -> &RegionPickerState {
        &self.region_picker
    }

    pub fn region_picker_mut(&mut self) -> &mut RegionPickerState {
        &mut self.region_picker
    }

    pub fn help_scroll(&self) -> u16 {
        self.help_scroll
    }

    pub fn set_help_scroll(&mut self, scroll: u16) {
        self.help_scroll = scroll;
    }

    pub fn detail_scroll(&self) -> u16 {
        self.detail_scroll
    }

    pub fn set_detail_scroll(&mut self, scroll: u16) {
        self.detail_scroll = scroll;
    }

    pub fn detail_toggle_pending(&self) -> bool {
        self.detail_toggle_pending
    }

    pub fn set_detail_toggle_pending(&mut self, pending: bool) {
        self.detail_toggle_pending = pending;
    }

    pub fn welcome_opened(&self) -> Option<std::time::Instant> {
        self.welcome_opened
    }

    pub fn set_welcome_opened(&mut self, when: Option<std::time::Instant>) {
        self.welcome_opened = when;
    }

    pub fn esc_quit_hint_shown(&self) -> bool {
        self.esc_quit_hint_shown
    }

    pub fn set_esc_quit_hint_shown(&mut self, shown: bool) {
        self.esc_quit_hint_shown = shown;
    }

    pub fn known_hosts_count(&self) -> usize {
        self.known_hosts_count
    }

    pub fn set_known_hosts_count(&mut self, count: usize) {
        self.known_hosts_count = count;
    }

    pub fn pending_connect(&self) -> Option<&(String, Option<String>)> {
        self.pending_connect.as_ref()
    }

    /// Take the queued connect, leaving `None`. Consumed by the event loop.
    pub fn take_pending_connect(&mut self) -> Option<(String, Option<String>)> {
        self.pending_connect.take()
    }
}

impl ThemePickerState {
    /// Clear the catalogue lists and the saved-name input. Used by both
    /// picker-close paths. The `list` cursor and `original` are
    /// intentionally NOT touched here. The two callers handle `original`
    /// differently: the Esc/q path consumes it via `.take()` before
    /// calling reset (to restore the prior live theme); the Enter-save
    /// path leaves `original` intact through reset and clears it
    /// explicitly afterwards. Keeping `reset` orthogonal to `original`
    /// lets both flows share the same body.
    pub fn reset(&mut self) {
        self.builtins = Vec::new();
        self.custom = Vec::new();
        self.saved_name = String::new();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn queue_connect_sets_pending_connect_to_some() {
        let mut s = UiSelection::default();
        s.queue_connect("web1".into(), Some("vault:foo".into()));
        assert_eq!(
            s.pending_connect,
            Some(("web1".to_string(), Some("vault:foo".to_string())))
        );
    }

    #[test]
    fn queue_connect_with_no_askpass_stores_none() {
        let mut s = UiSelection::default();
        s.queue_connect("web1".into(), None);
        assert_eq!(s.pending_connect, Some(("web1".to_string(), None)));
    }

    #[test]
    fn queue_connect_overwrites_existing_pending() {
        let mut s = UiSelection::default();
        s.queue_connect("first".into(), None);
        s.queue_connect("second".into(), Some("p".into()));
        assert_eq!(
            s.pending_connect,
            Some(("second".to_string(), Some("p".to_string())))
        );
    }

    #[test]
    fn open_snippet_search_sets_empty_query() {
        let mut s = UiSelection::default();
        s.open_snippet_search();
        assert_eq!(s.snippet_search.as_deref(), Some(""));
    }

    #[test]
    fn open_snippet_search_overwrites_existing_query_with_empty() {
        // The handler currently calls open only when search was inactive,
        // but the invariant should still hold: open is unconditional and
        // resets to an empty query. Pin the reset semantic so a future
        // caller cannot rely on a preserved query.
        let mut s = UiSelection {
            snippet_search: Some("old".to_string()),
            ..Default::default()
        };
        s.open_snippet_search();
        assert_eq!(s.snippet_search.as_deref(), Some(""));
    }

    #[test]
    fn close_snippet_search_clears_query() {
        let mut s = UiSelection {
            snippet_search: Some("query".to_string()),
            ..Default::default()
        };
        s.close_snippet_search();
        assert!(s.snippet_search.is_none());
    }

    #[test]
    fn close_snippet_search_is_idempotent() {
        let mut s = UiSelection::default();
        s.close_snippet_search();
        s.close_snippet_search();
        assert!(s.snippet_search.is_none());
    }

    #[test]
    fn theme_picker_reset_clears_lists_and_saved_name() {
        let mut t = ThemePickerState {
            builtins: vec![ThemeDef::purple_purple()],
            custom: vec![ThemeDef::purple_purple(), ThemeDef::purple_purple()],
            saved_name: "Solarized".to_string(),
            ..Default::default()
        };
        t.reset();
        assert!(t.builtins.is_empty());
        assert!(t.custom.is_empty());
        assert!(t.saved_name.is_empty());
    }

    #[test]
    fn theme_picker_reset_preserves_original_and_list_cursor() {
        let mut t = ThemePickerState {
            builtins: vec![ThemeDef::purple_purple()],
            original: Some(ThemeDef::purple_purple()),
            ..Default::default()
        };
        t.list.select(Some(2));
        t.reset();
        assert!(t.original.is_some(), "original must survive reset()");
        assert_eq!(t.list.selected(), Some(2));
    }
}