Skip to main content

purple_ssh/
app.rs

1use ratatui::widgets::ListState;
2
3use crate::history::ConnectionHistory;
4use crate::ssh_config::model::SshConfigFile;
5
6/// Case-insensitive substring check without allocation.
7/// Uses a byte-window approach for ASCII strings (the common case for SSH
8/// hostnames and aliases). Falls back to a char-based scan when either
9/// string contains non-ASCII bytes to avoid false matches across UTF-8
10/// character boundaries.
11pub(crate) fn contains_ci(haystack: &str, needle: &str) -> bool {
12    if needle.is_empty() {
13        return true;
14    }
15    if haystack.is_ascii() && needle.is_ascii() {
16        return haystack
17            .as_bytes()
18            .windows(needle.len())
19            .any(|window| window.eq_ignore_ascii_case(needle.as_bytes()));
20    }
21    // Non-ASCII fallback: compare char-by-char (case fold ASCII only)
22    let needle_lower: Vec<char> = needle.chars().map(|c| c.to_ascii_lowercase()).collect();
23    let haystack_chars: Vec<char> = haystack.chars().collect();
24    haystack_chars.windows(needle_lower.len()).any(|window| {
25        window
26            .iter()
27            .zip(needle_lower.iter())
28            .all(|(h, n)| h.to_ascii_lowercase() == *n)
29    })
30}
31
32/// Case-insensitive equality check without allocation.
33pub(super) fn eq_ci(a: &str, b: &str) -> bool {
34    a.eq_ignore_ascii_case(b)
35}
36
37mod baselines;
38mod container_state;
39mod containers_overview;
40mod display_list;
41mod file_browser_state;
42mod form_state;
43mod forms;
44mod groups;
45mod host_state;
46mod hosts;
47pub(crate) use hosts::migrate_renames_persistent_state;
48pub(crate) mod jump;
49mod key_push_state;
50mod keys_state;
51mod pickers;
52pub(crate) mod ping;
53mod provider_state;
54mod reload_state;
55mod screen;
56mod search;
57mod selection;
58mod snippet_state;
59mod status_state;
60mod tag_state;
61mod tunnel_state;
62mod ui_state;
63mod update;
64mod vault;
65
66pub use baselines::{FormBaseline, ProviderFormBaseline, SnippetFormBaseline, TunnelFormBaseline};
67pub use container_state::{ContainerSession, ContainerState};
68pub use containers_overview::{
69    ContainerActionRequest, ContainerExecRequest, ContainerLogsRequest, ContainersOverviewState,
70    ContainersSortMode, InspectCacheEntry, LIST_CACHE_TTL_SECS, LOGS_TAIL, LogsCacheEntry,
71    REFRESH_MAX_PARALLEL, RefreshBatch, RefreshQueueItem,
72};
73pub use file_browser_state::FileBrowserState;
74pub use form_state::FormState;
75pub(crate) use forms::char_to_byte_pos;
76pub use forms::{
77    FormField, HostForm, ProviderFormField, ProviderFormFields, SnippetForm, SnippetFormField,
78    SnippetHostOutput, SnippetOutputState, SnippetParamFormState, TunnelForm, TunnelFormField,
79};
80pub use host_state::{
81    DeletedHost, GroupBy, HostListItem, HostState, ProxyJumpCandidate, SortMode, ViewMode,
82    health_summary_spans, health_summary_spans_for,
83};
84pub use key_push_state::KeyPushState;
85pub use keys_state::KeysState;
86pub use ping::{
87    PingState, PingStatus, classify_ping, ping_sort_key, propagate_ping_to_dependents, status_glyph,
88};
89pub use provider_state::{
90    LabelMigrationField, PendingLabelMigration, ProviderRow, ProviderState, SyncRecord,
91};
92pub(crate) use reload_state::config_changed;
93pub use reload_state::{ConflictState, ReloadState};
94pub use screen::{ContainerLogsSearch, Screen, StackMember, TopPage, WhatsNewState};
95pub use search::SearchState;
96pub use snippet_state::SnippetState;
97pub use status_state::{MessageClass, StatusCenter, StatusMessage};
98pub use tag_state::{
99    BulkTagAction, BulkTagApplyResult, BulkTagEditorState, BulkTagRow, TagState,
100    select_display_tags,
101};
102pub use tunnel_state::{TunnelSortMode, TunnelState};
103pub use ui_state::UiSelection;
104pub use update::UpdateState;
105pub use vault::VaultState;
106
107/// Kill active tunnel processes when App is dropped (e.g. on panic).
108impl Drop for App {
109    fn drop(&mut self) {
110        for (alias, mut tunnel) in self.tunnels.active.drain() {
111            if let Err(e) = tunnel.child.kill() {
112                log::debug!("[external] Failed to kill tunnel for {alias} on shutdown: {e}");
113            }
114            let _ = tunnel.child.wait();
115        }
116        // Cancel and join any in-flight Vault SSH bulk-sign worker so it
117        // cannot keep writing to ~/.purple/certs/ after teardown (panic
118        // unwind, normal exit, etc.).
119        if let Some(handle) = self.vault.cancel_signing_run() {
120            let _ = handle.join();
121        }
122        // Same dance for key-push workers: signal cancel, join, so a
123        // panic or early exit cannot leave a thread writing to remote
124        // authorized_keys after the App is gone.
125        self.keys.push.shutdown();
126    }
127}
128
129/// Main application state.
130pub struct App {
131    // Core
132    /// Currently rendered screen identifier; navigation only, never carries state heaps.
133    pub screen: Screen,
134    /// Top-level page (Hosts, Tunnels, Containers). Selected by Tab/Shift+Tab
135    /// in the navigation bar. Independent of `screen`, which tracks overlays.
136    pub top_page: TopPage,
137    /// App lifecycle flag; flip to false to exit the event loop.
138    pub running: bool,
139    /// All host entries plus selection state.
140    pub(crate) hosts_state: HostState,
141
142    // Sub-structs
143    /// Toast queue, sticky messages, status routing.
144    pub(crate) status_center: StatusCenter,
145    /// Cursor reveal, detail-toggle, welcome timestamps and overlay meta.
146    pub(crate) ui: UiSelection,
147    /// Host-list incremental search query and matched hits.
148    pub(crate) search: SearchState,
149    /// Reload-from-disk state when ~/.ssh/config changes externally.
150    pub(crate) reload: ReloadState,
151    /// Conflict detection when an external edit clashes with our pending write.
152    pub(crate) conflict: ConflictState,
153
154    /// Keys-tab state: discovered keys, push runs, activity log.
155    pub(crate) keys: KeysState,
156
157    /// Tag library and per-host tag mappings.
158    pub(crate) tags: TagState,
159
160    /// Host form and bulk tag editor scratch state.
161    pub(crate) forms: FormState,
162
163    /// Connection history persisted to ~/.purple/history.
164    pub(crate) history: ConnectionHistory,
165
166    /// Provider configs, sync runs, host conflict resolution.
167    pub(crate) providers: ProviderState,
168
169    /// Ping/health-check state per host.
170    pub(crate) ping: PingState,
171
172    /// Vault SSH certificate cache and signing run state.
173    pub(crate) vault: VaultState,
174
175    /// Tunnel definitions per host and active tunnel processes.
176    pub(crate) tunnels: TunnelState,
177
178    /// Snippet library, parameter forms, output buffers.
179    pub(crate) snippets: SnippetState,
180
181    /// Self-update polling and badge state.
182    pub(crate) update: UpdateState,
183
184    /// askpass session token; not Keys-tab state.
185    pub bw_session: Option<String>,
186
187    // File browser
188    /// Persistent per-host last-visited paths; always present.
189    pub(crate) file_browser_state: FileBrowserState,
190    /// Per-host overlay session; Some when the file browser is open.
191    pub(crate) file_browser_session: Option<crate::file_browser::FileBrowserSession>,
192
193    // Containers
194    /// Cache and cross-host pending operations; always present.
195    pub(crate) container_state: ContainerState,
196    /// Per-host overlay session state; Some when the containers overlay is open.
197    pub(crate) container_session: Option<ContainerSession>,
198    /// Containers tab data: per-host docker ps cache, selection.
199    pub(crate) containers_overview: ContainersOverviewState,
200
201    /// Demo mode: all mutations are in-memory only, no disk writes.
202    pub demo_mode: bool,
203
204    /// Resolved process environment and filesystem paths, injected once at
205    /// construction. Every env/path read goes through here instead of ambient
206    /// `std::env` / `dirs::home_dir`. `Arc` so worker closures clone cheaply.
207    pub(crate) env: std::sync::Arc<crate::runtime::env::Env>,
208
209    /// Jump state. Some when the jump bar is open.
210    pub(crate) jump: Option<JumpState>,
211}
212
213impl App {
214    /// Construct with the process environment resolved here. In test builds the
215    /// environment is a self-cleaning sandbox so fixtures never touch the real
216    /// `~/.purple` or process env and need no lock.
217    pub fn new(config: SshConfigFile) -> Self {
218        #[cfg(test)]
219        let env = std::sync::Arc::new(crate::runtime::env::Env::sandboxed());
220        #[cfg(not(test))]
221        let env = std::sync::Arc::new(crate::runtime::env::Env::from_process());
222        Self::with_env(config, env)
223    }
224
225    /// Construct with an explicitly provided environment. The edge
226    /// (`launcher::run`) uses this to share one `Env` snapshot with the
227    /// pre-TUI CLI handlers.
228    pub fn with_env(config: SshConfigFile, env: std::sync::Arc<crate::runtime::env::Env>) -> Self {
229        let hosts = config.host_entries();
230        let patterns = config.pattern_entries();
231        let display_list = Self::build_display_list_from(&config, &hosts, &patterns);
232
233        let initial_selection = display_list.iter().position(|item| {
234            matches!(
235                item,
236                HostListItem::Host { .. } | HostListItem::Pattern { .. }
237            )
238        });
239
240        let reload = ReloadState::from_config(&config);
241        let hosts_state = HostState::from_config(config, hosts, patterns, display_list);
242
243        Self {
244            screen: Screen::HostList,
245            top_page: TopPage::default(),
246            running: true,
247            hosts_state,
248            status_center: StatusCenter::default(),
249            ui: UiSelection::new_with_initial_selection(initial_selection),
250            search: SearchState::default(),
251            reload,
252            conflict: ConflictState::default(),
253            keys: KeysState {
254                list: Vec::new(),
255                list_state: ratatui::widgets::ListState::default(),
256                activity: crate::key_activity::KeyActivityLog::load(),
257                push: KeyPushState::default(),
258            },
259            tags: TagState::default(),
260            forms: FormState::default(),
261            history: ConnectionHistory::load(),
262            providers: ProviderState::load(),
263            ping: PingState::from_preferences(env.paths()),
264            vault: VaultState::default(),
265            tunnels: TunnelState::default(),
266            snippets: SnippetState::with_store_loaded(),
267            update: UpdateState::with_current_hint(),
268            bw_session: None,
269            file_browser_state: FileBrowserState::default(),
270            file_browser_session: None,
271            container_state: ContainerState {
272                cache: crate::containers::load_container_cache(env.paths()),
273                ..ContainerState::default()
274            },
275            container_session: None,
276            containers_overview: ContainersOverviewState::default(),
277            demo_mode: false,
278            env,
279            jump: None,
280        }
281    }
282
283    /// The resolved process environment and filesystem paths for this run.
284    pub(crate) fn env(&self) -> &crate::runtime::env::Env {
285        &self.env
286    }
287
288    /// Record an SSH session against `alias` in the activity log. Appends
289    /// in memory and flushes to `~/.purple/key_activity.json`. Failures
290    /// during flush are logged at debug level only; an activity-log write
291    /// failure must never interrupt the user's connect flow. Caller
292    /// passes `now`; production call sites pass `key_activity::now_secs()`.
293    pub fn record_key_use(&mut self, alias: &str, now: u64) {
294        crate::key_activity::record_and_flush(&mut self.keys.activity, alias, now);
295    }
296
297    /// Snapshot the alias of every host currently loaded. Used as
298    /// the "before" set for `queue_new_aliases_since` after a
299    /// reload that may have added or removed hosts.
300    pub fn snapshot_alias_set(&self) -> std::collections::HashSet<String> {
301        self.hosts_state
302            .list
303            .iter()
304            .map(|h| h.alias.clone())
305            .collect()
306    }
307
308    /// Push aliases that are in the current host list but were NOT
309    /// in `before_aliases` to the auto-fetch queue. Sync handlers
310    /// and external-config-edit detection use this so only freshly
311    /// introduced hosts trigger an initial `docker ps`. pre-existing
312    /// cache-missing hosts are explicitly left alone.
313    pub fn queue_new_aliases_since(&mut self, before_aliases: &std::collections::HashSet<String>) {
314        let new_aliases: Vec<String> = self
315            .hosts_state
316            .list
317            .iter()
318            .filter(|h| !before_aliases.contains(&h.alias))
319            .map(|h| h.alias.clone())
320            .collect();
321        for alias in new_aliases {
322            self.container_state.queue_fetch(alias);
323        }
324    }
325
326    /// Reload hosts from config.
327    ///
328    /// Flushes any deferred vault config write, rebuilds the host list
329    /// from `ssh_config`, then orchestrates orphan pruning across every
330    /// sub-state that keys on host alias or container ID. Each sub-state
331    /// owns the prune mechanics via `prune_orphans` (or
332    /// `prune_by_container_ids` for the inspect/logs caches). This
333    /// function still owns sequencing (container_id derivation requires
334    /// the container_cache to be pruned first) and post-prune
335    /// persistence (`save_container_cache`,
336    /// `save_containers_collapsed_hosts`).
337    pub fn reload_hosts(&mut self) {
338        let had_pending_vault_write = self.vault.pending_config_write;
339        // Synchronously flush any deferred vault config write before reloading,
340        // so on-disk state matches in-memory state (no TOCTOU with auto-reload).
341        // Skip when a form is open (flush handler would bail anyway) and do not
342        // call flush_pending_vault_write() itself to avoid recursion.
343        //
344        // Before flushing, check whether the on-disk config changed since the
345        // in-memory model was loaded. If so, the deferred write would overwrite
346        // those external edits silently. Surface a notification and skip the
347        // flush; the user can re-trigger vault sign after reviewing their
348        // changes. The cert files themselves were already written by the bulk
349        // sign worker. Only the config-side `CertificateFile` directives are
350        // skipped, which the user can wire up via a fresh sign.
351        let mut flushed_vault_write = false;
352        if self.vault.pending_config_write && !self.is_form_open() {
353            if self.external_config_changed() {
354                self.notify_error(
355                    crate::messages::vault_config_skipped_external_change().to_string(),
356                );
357                log::warn!(
358                    "[config] reload_hosts: skipping deferred vault write. external config changed"
359                );
360            } else {
361                match self.hosts_state.ssh_config.write() {
362                    Ok(()) => flushed_vault_write = true,
363                    Err(e) => self.notify_error(crate::messages::vault_config_write_after_sign(&e)),
364                }
365            }
366        }
367        // Always clear the flag: either we flushed, we surfaced a conflict, or
368        // the form-submit path has already written the full config.
369        self.vault.pending_config_write = false;
370        log::debug!(
371            "[config] reload_hosts: pending_vault_write={had_pending_vault_write} flushed={flushed_vault_write}"
372        );
373        let had_search = self.search.query.take();
374        let selected_alias = self
375            .selected_host()
376            .map(|h| h.alias.clone())
377            .or_else(|| self.selected_pattern().map(|p| p.pattern.clone()));
378
379        self.tunnels.summaries_cache.clear();
380        self.hosts_state.render_cache.invalidate();
381        self.hosts_state.list = self.hosts_state.ssh_config.host_entries();
382        self.hosts_state.patterns = self.hosts_state.ssh_config.pattern_entries();
383
384        // Orphan-prune every per-host sub-state in one scoped block.
385        // `valid_aliases` borrows from `self.hosts_state.list`, so the
386        // scope ends before any `&mut self` call (apply_sort, set_screen)
387        // can run. Within the scope only field-method calls are allowed.
388        {
389            let valid_aliases: std::collections::HashSet<&str> = self
390                .hosts_state
391                .list
392                .iter()
393                .map(|h| h.alias.as_str())
394                .collect();
395
396            self.vault.prune_orphans(&valid_aliases);
397
398            // Per-host container cache; persist the trimmed cache when
399            // anything dropped so `~/.purple/container_cache.jsonl` does
400            // not keep serving orphan entries on the next purple start.
401            // Demo mode skips disk writes via `save_container_cache` itself.
402            if self.container_state.prune_orphans(&valid_aliases) {
403                crate::containers::save_container_cache(
404                    self.env().paths(),
405                    self.container_state.cache(),
406                );
407            }
408
409            // Inspect / logs caches key on container ID. Build the
410            // valid-id set from the (just-pruned) container_cache and
411            // prune both caches in one pass.
412            let valid_container_ids: std::collections::HashSet<String> = self
413                .container_state
414                .cache()
415                .values()
416                .flat_map(|e| e.containers.iter().map(|c| c.id.clone()))
417                .collect();
418            self.containers_overview
419                .prune_by_container_ids(&valid_container_ids);
420
421            // Per-alias overview state (auto-list in-flight, refresh
422            // batch, collapsed-hosts). Persist collapsed_hosts when it
423            // shrank.
424            if self.containers_overview.prune_orphans(&valid_aliases) {
425                if let Err(e) = crate::preferences::save_containers_collapsed_hosts(
426                    self.env().paths(),
427                    self.containers_overview.collapsed_hosts(),
428                ) {
429                    log::warn!("[config] failed to save collapsed_hosts after prune: {e}");
430                }
431            }
432
433            self.file_browser_state.prune_orphans(&valid_aliases);
434            self.tunnels.prune_orphans(&valid_aliases);
435            self.ping.prune_orphans(&valid_aliases);
436        }
437
438        if self.hosts_state.sort_mode == SortMode::Original
439            && matches!(self.hosts_state.group_by, GroupBy::None)
440        {
441            self.hosts_state.display_list = Self::build_display_list_from(
442                &self.hosts_state.ssh_config,
443                &self.hosts_state.list,
444                &self.hosts_state.patterns,
445            );
446        } else {
447            self.apply_sort();
448        }
449
450        // Close tag pickers if open. tags.list is stale after reload
451        if matches!(self.screen, Screen::TagPicker | Screen::BulkTagEditor) {
452            self.set_screen(Screen::HostList);
453            self.forms.bulk_tag_editor = BulkTagEditorState::default();
454        }
455
456        // Multi-select stores indices into hosts; clear to avoid stale refs
457        self.hosts_state.multi_select.clear();
458
459        // Restore search if it was active, otherwise reset
460        if let Some(query) = had_search {
461            self.search.query = Some(query);
462            self.apply_filter();
463        } else {
464            self.search.query = None;
465            self.search.filtered_indices.clear();
466            self.search.filtered_pattern_indices.clear();
467            // Fix selection for display list mode
468            if self.hosts_state.list.is_empty() && self.hosts_state.patterns.is_empty() {
469                self.ui.list_state.select(None);
470            } else if let Some(pos) = self.hosts_state.display_list.iter().position(|item| {
471                matches!(
472                    item,
473                    HostListItem::Host { .. } | HostListItem::Pattern { .. }
474                )
475            }) {
476                let current = self.ui.list_state.selected().unwrap_or(0);
477                if current >= self.hosts_state.display_list.len()
478                    || !matches!(
479                        self.hosts_state.display_list.get(current),
480                        Some(HostListItem::Host { .. } | HostListItem::Pattern { .. })
481                    )
482                {
483                    self.ui.list_state.select(Some(pos));
484                }
485            } else {
486                self.ui.list_state.select(None);
487            }
488        }
489
490        // Restore selection by alias (e.g. after SSH connect changed sort order)
491        if let Some(alias) = selected_alias {
492            self.select_host_by_alias(&alias);
493        }
494
495        log::debug!(
496            "[config] reload_hosts: hosts={} patterns={} display_items={}",
497            self.hosts_state.list.len(),
498            self.hosts_state.patterns.len(),
499            self.hosts_state.display_list.len(),
500        );
501    }
502
503    /// Synchronously re-check a host's Vault SSH certificate and update
504    /// `vault.cert_cache` with fresh status + on-disk mtime.
505    ///
506    /// Every sign path (V-key bulk sign, host form submit, connect-time
507    /// `ensure_vault_ssh_if_needed`, CLI) funnels through this helper so the
508    /// detail panel never lies about cert state after a successful sign.
509    ///
510    /// No-op in demo mode. If the host is missing, has no resolvable vault
511    /// role, or the cert path cannot be resolved, any stale entry for the
512    /// alias is removed to avoid showing ghost status.
513    pub fn refresh_cert_cache(&mut self, alias: &str) {
514        if crate::demo_flag::is_demo() {
515            return;
516        }
517        let Some(host) = self.hosts_state.list.iter().find(|h| h.alias == alias) else {
518            self.vault.cert_cache.remove(alias);
519            return;
520        };
521        let role_some = crate::vault_ssh::resolve_vault_role(
522            host.vault_ssh.as_deref(),
523            host.provider.as_deref(),
524            host.provider_label.as_deref(),
525            &self.providers.config,
526        )
527        .is_some();
528        if !role_some {
529            self.vault.cert_cache.remove(alias);
530            return;
531        }
532        let cert_path = match crate::vault_ssh::resolve_cert_path(
533            self.env().paths(),
534            alias,
535            &host.certificate_file,
536        ) {
537            Ok(p) => p,
538            Err(_) => {
539                self.vault.cert_cache.remove(alias);
540                return;
541            }
542        };
543        let status = crate::vault_ssh::check_cert_validity(self.env(), &cert_path);
544        let mtime = std::fs::metadata(&cert_path)
545            .ok()
546            .and_then(|m| m.modified().ok());
547        self.vault.cert_cache.insert(
548            alias.to_string(),
549            (std::time::Instant::now(), status, mtime),
550        );
551    }
552
553    // --- Search methods ---
554
555    /// Shim. Routes to `ProviderState::sorted_names`.
556    /// Test-only: production code uses `provider_list_rows()` for the
557    /// tree-style list, so this wrapper exists to keep older test fixtures
558    /// concise.
559    #[cfg(test)]
560    pub fn sorted_provider_names(&self) -> Vec<String> {
561        self.providers.sorted_names()
562    }
563
564    /// Check whether a form screen is currently open (host or provider forms).
565    pub fn is_form_open(&self) -> bool {
566        matches!(
567            self.screen,
568            Screen::AddHost | Screen::EditHost { .. } | Screen::ProviderForm { .. }
569        )
570    }
571
572    /// Open the unified jump in the given mode. Loads recents
573    /// from disk and seeds the empty-query view. Recomputes hits.
574    pub fn open_jump(&mut self, mode: JumpMode) {
575        log::debug!("jump: open mode={:?}", mode);
576        let mut state = JumpState::for_mode(mode);
577        let recents_file = jump::load_recents();
578        state.recents = self.resolve_recents(&recents_file);
579        self.jump = Some(state);
580        self.recompute_jump_hits();
581    }
582
583    /// Close the unified jump overlay. Idempotent: a no-op when no jump
584    /// is open. Pairs with `open_jump`; the three handler arms (Esc,
585    /// Enter-after-dispatch, Backspace-on-empty) all route through here.
586    pub(crate) fn close_jump(&mut self) {
587        self.jump = None;
588    }
589
590    /// Translate the on-disk recents log into live `JumpHit`s, dropping
591    /// dangling references silently.
592    fn resolve_recents(&self, file: &RecentsFile) -> Vec<JumpHit> {
593        let mode = self
594            .jump
595            .as_ref()
596            .map(|p| p.mode)
597            .unwrap_or(JumpMode::Hosts);
598        let mut out = Vec::with_capacity(file.entries.len());
599        for entry in &file.entries {
600            if let Some(hit) = self.resolve_recent_ref(&entry.target, mode) {
601                out.push(hit);
602            }
603        }
604        out
605    }
606
607    /// Test seam: exposes `resolve_recent_ref` as `pub(crate)` so the unit
608    /// tests in `app::tests` can drive each `SourceKind` branch without
609    /// going through `open_jump`.
610    #[cfg(test)]
611    pub(crate) fn resolve_recent_ref_for_test(
612        &self,
613        r: &RecentRef,
614        mode: JumpMode,
615    ) -> Option<JumpHit> {
616        self.resolve_recent_ref(r, mode)
617    }
618
619    fn resolve_recent_ref(&self, r: &RecentRef, mode: JumpMode) -> Option<JumpHit> {
620        match r.kind {
621            SourceKind::Action => {
622                let key_char = r.key.chars().next()?;
623                let actions = JumpAction::for_mode(mode);
624                actions
625                    .iter()
626                    .find(|a| a.key == key_char)
627                    .copied()
628                    .map(JumpHit::Action)
629            }
630            SourceKind::Host => {
631                let host = self.hosts_state.list.iter().find(|h| h.alias == r.key)?;
632                Some(JumpHit::Host(HostHit {
633                    alias: host.alias.clone(),
634                    hostname: host.hostname.clone(),
635                    tags: host.tags.clone(),
636                    provider: host.provider.clone(),
637                    user: host.user.clone(),
638                    identity_file: host.identity_file.clone(),
639                    proxy_jump: host.proxy_jump.clone(),
640                    vault_ssh: host.vault_ssh.clone(),
641                }))
642            }
643            SourceKind::Tunnel => {
644                let (alias, port_str) = r.key.split_once(':')?;
645                let port: u16 = port_str.parse().ok()?;
646                let rules = self.hosts_state.ssh_config.find_tunnel_directives(alias);
647                let rule = rules.iter().find(|r| r.bind_port == port)?;
648                Some(JumpHit::Tunnel(TunnelHit {
649                    alias: alias.to_string(),
650                    bind_port: rule.bind_port,
651                    bind_port_str: rule.bind_port.to_string(),
652                    destination: rule.display(),
653                    active: self.tunnels.active.contains_key(alias),
654                }))
655            }
656            SourceKind::Container => {
657                let (alias, name) = r.key.split_once('/')?;
658                let entry = self.container_state.cache.get(alias)?;
659                let info = entry.containers.iter().find(|c| c.names == name)?;
660                Some(JumpHit::Container(ContainerHit {
661                    alias: alias.to_string(),
662                    container_name: info.names.clone(),
663                    container_id: info.id.clone(),
664                    state: info.state.clone(),
665                }))
666            }
667            SourceKind::Snippet => {
668                let snippet = self.snippets.store.get(&r.key)?;
669                Some(JumpHit::Snippet(SnippetHit {
670                    name: snippet.name.clone(),
671                    command_preview: preview(&snippet.command, 40),
672                }))
673            }
674        }
675    }
676
677    /// Recompute the jump bar hit list against the current query. Pulls
678    /// candidates from every live source and ranks them with nucleo-matcher.
679    /// Preserves the previously-selected hit's identity across the
680    /// recompute so mid-typing arrow-key navigation does not jump back to
681    /// row 0.
682    pub fn recompute_jump_hits(&mut self) {
683        let Some(mut state) = self.jump.take() else {
684            return;
685        };
686        // Identity of the row the user was on before the recompute. We
687        // re-resolve it after rebuilding `hits` to keep selection stable
688        // when the user types and the matched row is still in the list.
689        let prior_identity = state
690            .visible_hits()
691            .get(state.selected)
692            .map(|h| h.identity());
693
694        let candidates = self.collect_jump_candidates(state.mode);
695        if state.query.is_empty() {
696            state.hits = candidates;
697            state.selected = restore_selection(&state.visible_hits(), prior_identity.as_ref(), 0);
698            self.jump = Some(state);
699            return;
700        }
701
702        // Field-prefix syntax: `user:eric` scopes to one field. Empty
703        // remainder after the prefix is treated as no query (empty
704        // scope-search). Mode is held in `query_scope` for the row
705        // renderer to surface a "via <field>" hint.
706        let (scope, effective_query) = parse_query_scope(&state.query);
707
708        use nucleo_matcher::pattern::{CaseMatching, Normalization, Pattern};
709        use nucleo_matcher::{Config, Matcher, Utf32Str};
710        let matcher_state = state
711            .matcher
712            .get_or_insert_with(|| Matcher::new(Config::DEFAULT));
713        let pattern = Pattern::parse(effective_query, CaseMatching::Smart, Normalization::Smart);
714        let mut buf: Vec<char> = Vec::new();
715        let mut scored: Vec<(JumpHit, u32)> = Vec::with_capacity(candidates.len());
716        for hit in candidates {
717            let mut best: u32 = 0;
718            // Score over the right haystack set: scoped queries narrow to
719            // a single field; unscoped queries score over everything the
720            // hit advertises.
721            let scoped_haystacks = scoped_haystacks_for(&hit, scope);
722            let haystacks: Vec<&str> = if let Some(hs) = scoped_haystacks {
723                hs
724            } else {
725                hit.haystacks()
726            };
727            for haystack in haystacks {
728                buf.clear();
729                let chars = Utf32Str::new(haystack, &mut buf);
730                if let Some(score) = pattern.score(chars, matcher_state) {
731                    best = best.max(score);
732                }
733            }
734            // Boost: a single-char query that exactly matches an action's
735            // hotkey letter (case-insensitive) lands the action at the top.
736            // When two actions share the same hotkey (e.g. 'a' for `Hosts:
737            // Add host` and `Tunnels: Add tunnel`), the one whose target
738            // matches the current mode wins, so muscle memory survives.
739            if let JumpHit::Action(a) = &hit {
740                let single = effective_query.chars().next();
741                if effective_query.chars().count() == 1
742                    && single
743                        .map(|c| c.eq_ignore_ascii_case(&a.key))
744                        .unwrap_or(false)
745                {
746                    let mode_match = matches!(
747                        (state.mode, a.target),
748                        (JumpMode::Hosts, JumpActionTarget::Hosts)
749                            | (JumpMode::Tunnels, JumpActionTarget::Tunnels)
750                            | (JumpMode::Containers, JumpActionTarget::Containers)
751                            | (JumpMode::Keys, JumpActionTarget::Keys)
752                    );
753                    let bump = if mode_match { 20_000 } else { 10_000 };
754                    best = best.saturating_add(bump);
755                }
756            }
757            // Score floor: actions need to clear a higher bar than data
758            // rows. Stops query 'eric' from dragging in 'Containers: List
759            // containers' on stray e/r/i/c char overlap.
760            let floor = match &hit {
761                JumpHit::Action(_) => jump::PALETTE_ACTION_FLOOR,
762                _ => 1,
763            };
764            if best >= floor {
765                scored.push((hit, best));
766            }
767        }
768        // Stable sort: higher score first, ties broken by render-order kind so
769        // hosts come before actions when scores tie.
770        scored.sort_by(|a, b| {
771            b.1.cmp(&a.1)
772                .then_with(|| kind_rank(a.0.kind()).cmp(&kind_rank(b.0.kind())))
773        });
774        // Cap per-section using a fixed-size array so a broad query (one
775        // char that matches everything) cannot blow the visible list.
776        let mut per_kind: [usize; 5] = [0; 5];
777        let mut filtered: Vec<JumpHit> = Vec::with_capacity(scored.len().min(160));
778        for (hit, _) in scored {
779            let slot = kind_rank(hit.kind()) as usize;
780            if per_kind[slot] < PALETTE_PER_SECTION_CAP {
781                per_kind[slot] += 1;
782                filtered.push(hit);
783            }
784        }
785        state.hits = filtered;
786        // `state.hits` is score-sorted but `visible_hits()` reorders into
787        // fixed render-section order. Default the cursor to the top-scored
788        // hit's position in that display order so the boosted best match
789        // stays pre-selected and the highlight lands on the row that Enter
790        // will dispatch, even when a lower-scored host renders above it. The
791        // top-scored hit is the first of its kind in display order, so its
792        // position is the first row of that section. Resolving by kind
793        // sidesteps the non-unique action/tunnel/container identities.
794        let display = state.visible_hits();
795        let top_display = state
796            .hits
797            .first()
798            .map(|h| h.kind())
799            .and_then(|k| display.iter().position(|h| h.kind() == k))
800            .unwrap_or(0);
801        state.selected = restore_selection(&display, prior_identity.as_ref(), top_display);
802        log::debug!(
803            "jump: recompute selected={} of {} hits (top_display={})",
804            state.selected,
805            state.hits.len(),
806            top_display
807        );
808        self.jump = Some(state);
809    }
810
811    fn collect_jump_candidates(&self, mode: JumpMode) -> Vec<JumpHit> {
812        let mut out: Vec<JumpHit> = Vec::new();
813        // Hosts
814        for h in &self.hosts_state.list {
815            out.push(JumpHit::Host(HostHit {
816                alias: h.alias.clone(),
817                hostname: h.hostname.clone(),
818                tags: h.tags.clone(),
819                provider: h.provider.clone(),
820                user: h.user.clone(),
821                identity_file: h.identity_file.clone(),
822                proxy_jump: h.proxy_jump.clone(),
823                vault_ssh: h.vault_ssh.clone(),
824            }));
825        }
826        // Tunnels: every configured rule from every host with a directive.
827        for h in &self.hosts_state.list {
828            let rules = self.hosts_state.ssh_config.find_tunnel_directives(&h.alias);
829            for rule in rules {
830                out.push(JumpHit::Tunnel(TunnelHit {
831                    alias: h.alias.clone(),
832                    bind_port: rule.bind_port,
833                    bind_port_str: rule.bind_port.to_string(),
834                    destination: rule.display(),
835                    active: self.tunnels.active.contains_key(&h.alias),
836                }));
837            }
838        }
839        // Containers: cached only. Triggering an SSH fetch on jump bar open
840        // would be unbounded latency.
841        for (alias, entry) in &self.container_state.cache {
842            for info in &entry.containers {
843                out.push(JumpHit::Container(ContainerHit {
844                    alias: alias.clone(),
845                    container_name: info.names.clone(),
846                    container_id: info.id.clone(),
847                    state: info.state.clone(),
848                }));
849            }
850        }
851        // Snippets
852        for snippet in &self.snippets.store.snippets {
853            out.push(JumpHit::Snippet(SnippetHit {
854                name: snippet.name.clone(),
855                command_preview: preview(&snippet.command, 40),
856            }));
857        }
858        // Actions last
859        for a in JumpAction::for_mode(mode) {
860            out.push(JumpHit::Action(*a));
861        }
862        out
863    }
864
865    /// Persist a jump dispatch to the on-disk MRU log. Best-effort; a
866    /// write error logs and is otherwise swallowed so user navigation is
867    /// never blocked by a recents-file failure. Takes `&mut self` so the
868    /// type system reflects that this performs I/O and mutates persistent
869    /// state, even though `jump::save_recents` only needs `&File`.
870    pub fn record_jump_hit(&mut self, hit: &JumpHit) {
871        if self.demo_mode {
872            log::debug!("jump: record skipped (demo mode)");
873            return;
874        }
875        let mut file = jump::load_recents();
876        jump::touch_recent(&mut file, hit.identity());
877        if let Err(e) = jump::save_recents(&file) {
878            log::warn!("[purple] failed to save recents: {e}");
879        }
880    }
881
882    /// Open the file-browser overlay with the given session. Stores the
883    /// session and switches to `Screen::FileBrowser` for the session's
884    /// alias. Any previously-open session is replaced.
885    pub(crate) fn open_file_browser(&mut self, session: crate::file_browser::FileBrowserSession) {
886        let alias = session.alias.clone();
887        self.file_browser_session = Some(session);
888        self.set_screen(Screen::FileBrowser { alias });
889    }
890
891    /// Close the file-browser overlay. Persists the current pane paths to
892    /// `file_browser_state.host_paths` so the next open re-seeds them,
893    /// drops the session, and returns to the host list.
894    pub(crate) fn close_file_browser(&mut self) {
895        if let Some(fb) = self.file_browser_session.take() {
896            self.file_browser_state
897                .host_paths
898                .insert(fb.alias, (fb.local_path, fb.remote_path));
899        }
900        self.set_screen(Screen::HostList);
901    }
902
903    /// Flush a deferred vault config write if one is pending and no form is open.
904    /// Returns true if a write was performed.
905    pub fn flush_pending_vault_write(&mut self) -> bool {
906        if !self.vault.pending_config_write || self.is_form_open() {
907            return false;
908        }
909        // reload_hosts() performs the write and clears the flag.
910        self.reload_hosts();
911        true
912    }
913
914    /// Run once after App::new: queue the upgrade toast if the user just
915    /// upgraded past their last-seen version, otherwise seed the preference
916    /// so the next launch is silent.
917    pub fn post_init(&mut self) {
918        let outcome = crate::onboarding::evaluate(self.env().paths());
919        if let Some(text) = outcome.upgrade_toast {
920            self.enqueue_sticky_toast(text);
921        }
922        // Seed the Keys tab so the first Tab navigation lands on a
923        // populated list. Subsequent reloads run via R or after a host
924        // form save / provider sync.
925        self.scan_keys();
926    }
927
928    fn enqueue_sticky_toast(&mut self, text: String) {
929        log::debug!("[purple] enqueue sticky toast: {}", text);
930        let msg = StatusMessage {
931            text,
932            class: MessageClass::Success,
933            tick_count: 0,
934            sticky: true,
935            created_at: std::time::Instant::now(),
936        };
937        self.status_center.toast = Some(msg);
938    }
939
940    /// User action feedback. Success toast, length-proportional timeout.
941    pub fn notify(&mut self, text: impl Into<String>) {
942        self.status_center.set_status(text, false);
943    }
944
945    /// User action error. Error toast, sticky by default, queued.
946    pub fn notify_error(&mut self, text: impl Into<String>) {
947        self.status_center.set_status(text, true);
948    }
949
950    /// Background event. Info footer, suppressed if sticky active.
951    pub fn notify_background(&mut self, text: impl Into<String>) {
952        self.status_center.set_background_status(text, false);
953    }
954
955    /// Background error. Sticky toast, bypasses sticky suppression.
956    pub fn notify_background_error(&mut self, text: impl Into<String>) {
957        self.status_center.set_background_status(text, true);
958    }
959
960    /// Caution / degraded state → Warning toast (length-proportional
961    /// timeout, queued). For: precondition violations ("Nothing to undo."),
962    /// validation hints ("Project ID can't be empty."), empty-state
963    /// notices ("No stale hosts."), stale-host warnings, deprecated
964    /// config detected, partial sync results. Warnings are NOT sticky;
965    /// the user acknowledges them by continuing to interact.
966    ///
967    /// Use `notify_error` only for system-level failures (I/O, network,
968    /// subprocess) that require explicit acknowledgement. Use
969    /// `notify_warning` for everything that is "this can't happen given
970    /// current state" or "you forgot something".
971    pub fn notify_warning(&mut self, text: impl Into<String>) {
972        let msg = StatusMessage {
973            text: text.into(),
974            class: MessageClass::Warning,
975            tick_count: 0,
976            sticky: false,
977            created_at: std::time::Instant::now(),
978        };
979        log::debug!("toast <- Warning: {}", msg.text);
980        self.status_center.push_toast(msg);
981    }
982
983    /// Long-running progress. Footer sticky, never expires automatically.
984    pub fn notify_progress(&mut self, text: impl Into<String>) {
985        self.status_center.set_sticky_status(text, false);
986    }
987
988    /// Sticky error. Footer sticky, never expires automatically.
989    pub fn notify_sticky_error(&mut self, text: impl Into<String>) {
990        self.status_center.set_sticky_status(text, true);
991    }
992
993    /// Explicit info. Footer, 4s timeout, not suppressed by sticky.
994    pub fn notify_info(&mut self, text: impl Into<String>) {
995        self.status_center.set_info_status(text);
996    }
997
998    /// Drop the footer status unconditionally. Use when a new user action
999    /// makes the prior status stale. Symmetric with the `notify_*` family
1000    /// so handlers stay on the App surface instead of reaching into
1001    /// `status_center` directly.
1002    pub(crate) fn clear_status(&mut self) {
1003        self.status_center.clear_status();
1004    }
1005
1006    /// Tick the footer status message timer. Uses wall-clock time.
1007    /// Sticky/Progress messages never expire automatically.
1008    ///
1009    /// Stays on `App` (not moved to `StatusCenter`) because expiry is
1010    /// suppressed while any provider sync is in flight, which requires
1011    /// reading `self.providers.syncing`.
1012    pub fn tick_status(&mut self) {
1013        // Don't expire status while providers are still syncing
1014        if !self.providers.syncing.is_empty() {
1015            return;
1016        }
1017        if let Some(ref status) = self.status_center.status {
1018            if status.sticky {
1019                return;
1020            }
1021            let timeout_ms = status.timeout_ms();
1022            if timeout_ms != u64::MAX && status.created_at.elapsed().as_millis() as u64 > timeout_ms
1023            {
1024                log::debug!("footer status expired: {}", status.text);
1025                self.status_center.status = None;
1026            }
1027        }
1028    }
1029
1030    /// Shim. Routes to `StatusCenter::tick_toast`.
1031    pub fn tick_toast(&mut self) {
1032        self.status_center.tick_toast();
1033    }
1034
1035    /// Check if config or any Include file has changed externally and reload if so.
1036    /// Skips reload when the user is in a form (AddHost/EditHost) to avoid
1037    /// overwriting in-memory config while the user is editing.
1038    pub fn check_config_changed(&mut self) {
1039        if matches!(
1040            self.screen,
1041            Screen::AddHost
1042                | Screen::EditHost { .. }
1043                | Screen::ProviderForm { .. }
1044                | Screen::TunnelList { .. }
1045                | Screen::TunnelForm { .. }
1046                | Screen::HostDetail { .. }
1047                | Screen::SnippetPicker { .. }
1048                | Screen::SnippetForm { .. }
1049                | Screen::SnippetOutput { .. }
1050                | Screen::SnippetParamForm { .. }
1051                | Screen::FileBrowser { .. }
1052                | Screen::Containers { .. }
1053                | Screen::ConfirmDelete { .. }
1054                | Screen::ConfirmHostKeyReset { .. }
1055                | Screen::ConfirmPurgeStale { .. }
1056                | Screen::ConfirmImport { .. }
1057                | Screen::ConfirmVaultSign { .. }
1058                | Screen::TagPicker
1059                | Screen::BulkTagEditor
1060                | Screen::ThemePicker
1061                | Screen::WhatsNew(_)
1062        ) || self.tags.input.is_some()
1063        {
1064            return;
1065        }
1066        let current_mtime = reload_state::get_mtime(&self.reload.config_path);
1067        let changed = current_mtime != self.reload.last_modified
1068            || self
1069                .reload
1070                .include_mtimes
1071                .iter()
1072                .any(|(path, old_mtime)| reload_state::get_mtime(path) != *old_mtime)
1073            || self
1074                .reload
1075                .include_dir_mtimes
1076                .iter()
1077                .any(|(path, old_mtime)| reload_state::get_mtime(path) != *old_mtime);
1078        if changed {
1079            log::debug!(
1080                "[config] check_config_changed: mtime drift detected on {} -> reloading",
1081                self.reload.config_path.display()
1082            );
1083            if let Ok(new_config) = SshConfigFile::parse(&self.reload.config_path) {
1084                let before_aliases = self.snapshot_alias_set();
1085                self.hosts_state.ssh_config = new_config;
1086                // Invalidate undo state. config structure may have changed externally
1087                self.hosts_state.undo_stack.clear();
1088                // Clear stale ping status. hosts may have changed
1089                log::debug!(
1090                    "[config] external config change: clearing {} ping result(s) + timestamps",
1091                    self.ping.status.len()
1092                );
1093                self.ping.status.clear();
1094                self.ping.last_checked.clear();
1095                self.ping.filter_down_only = false;
1096                self.ping.checked_at = None;
1097                self.reload_hosts();
1098                self.reload.last_modified = current_mtime;
1099                self.reload.include_mtimes =
1100                    reload_state::snapshot_include_mtimes(&self.hosts_state.ssh_config);
1101                self.reload.include_dir_mtimes =
1102                    reload_state::snapshot_include_dir_mtimes(&self.hosts_state.ssh_config);
1103                let count = self.hosts_state.list.len();
1104                self.notify_background(crate::messages::config_reloaded(count));
1105                self.queue_new_aliases_since(&before_aliases);
1106            }
1107        }
1108    }
1109
1110    /// Detect external changes to `~/.ssh/` keys and refresh `self.keys.list`
1111    /// when something has moved. Mirrors `check_config_changed` for the
1112    /// keys tab so users see new key files (or deletions, or rotations)
1113    /// without pressing R. Cheap: a single dir stat plus one stat per
1114    /// tracked key. Called from the 4-second throttle in `handle_tick`.
1115    ///
1116    /// Skips during demo mode (the demo seeds a fixed key list and never
1117    /// reads from disk) and when a form is open that could be mutating
1118    /// the same data.
1119    pub fn check_keys_changed(&mut self) {
1120        if self.demo_mode {
1121            return;
1122        }
1123        if matches!(
1124            self.screen,
1125            Screen::AddHost | Screen::EditHost { .. } | Screen::ProviderForm { .. }
1126        ) {
1127            return;
1128        }
1129        let Some(ssh_dir) = self.env().paths().map(crate::runtime::env::Paths::ssh_dir) else {
1130            return;
1131        };
1132        let current_dir_mtime = reload_state::get_mtime(&ssh_dir);
1133        let dir_changed = current_dir_mtime != self.reload.keys_dir_mtime;
1134        let files_changed = self
1135            .reload
1136            .key_file_mtimes
1137            .iter()
1138            .any(|(path, old)| reload_state::get_mtime(path) != *old);
1139        if !dir_changed && !files_changed {
1140            return;
1141        }
1142        log::debug!(
1143            "[purple] check_keys_changed: drift detected on {} (dir={} files={}) -> rescan",
1144            ssh_dir.display(),
1145            dir_changed,
1146            files_changed,
1147        );
1148        let previous = self.keys.list.len();
1149        self.scan_keys();
1150        let after = self.keys.list.len();
1151        // Keep the selection valid after a rescan: clamp to the new list
1152        // length, or land on the first row when the list grew from empty.
1153        if let Some(sel) = self.keys.list_state.selected() {
1154            if sel >= after {
1155                let next = after.checked_sub(1);
1156                self.keys.list_state.select(next);
1157            }
1158        } else if after > 0 {
1159            self.keys.list_state.select(Some(0));
1160        }
1161        if previous != after {
1162            log::debug!(
1163                "[purple] check_keys_changed: rescan {} -> {} keys",
1164                previous,
1165                after
1166            );
1167        }
1168    }
1169
1170    /// Non-mutating check: has the on-disk config (or any tracked Include)
1171    /// been modified since `self.reload.last_modified` was captured? Used by
1172    /// async write paths (e.g. the Vault SSH bulk-sign completion handler)
1173    /// to refuse writing when an external editor changed the file underneath
1174    /// us. overwriting those edits would silently discard user work. The
1175    /// backup-on-write mechanism in `SshConfigFile::write()` would still
1176    /// recover them, but detecting the conflict BEFORE writing is strictly
1177    /// better than after.
1178    pub fn external_config_changed(&self) -> bool {
1179        let current_mtime = reload_state::get_mtime(&self.reload.config_path);
1180        current_mtime != self.reload.last_modified
1181            || self
1182                .reload
1183                .include_mtimes
1184                .iter()
1185                .any(|(path, old_mtime)| reload_state::get_mtime(path) != *old_mtime)
1186            || self
1187                .reload
1188                .include_dir_mtimes
1189                .iter()
1190                .any(|(path, old_mtime)| reload_state::get_mtime(path) != *old_mtime)
1191    }
1192
1193    /// Update the last_modified timestamp (call after writing config).
1194    pub fn update_last_modified(&mut self) {
1195        self.reload.last_modified = reload_state::get_mtime(&self.reload.config_path);
1196        self.reload.include_mtimes =
1197            reload_state::snapshot_include_mtimes(&self.hosts_state.ssh_config);
1198        self.reload.include_dir_mtimes =
1199            reload_state::snapshot_include_dir_mtimes(&self.hosts_state.ssh_config);
1200    }
1201
1202    /// Returns true if any host or provider has a vault role configured.
1203    pub fn has_any_vault_role(&self) -> bool {
1204        for host in &self.hosts_state.list {
1205            if host.vault_ssh.is_some() {
1206                return true;
1207            }
1208        }
1209        for section in &self.providers.config.sections {
1210            if !section.vault_role.is_empty() {
1211                return true;
1212            }
1213        }
1214        false
1215    }
1216
1217    /// Poll active tunnels for exit. Returns (alias, message, is_error) tuples.
1218    pub fn poll_tunnels(&mut self) -> Vec<(String, String, bool)> {
1219        self.tunnels.poll()
1220    }
1221
1222    /// Recompute the lsof poller's bind-port list from the current
1223    /// `active` map plus each host's directives in the SSH config.
1224    /// Called after every tunnel start/stop. The poller picks up the
1225    /// new list on its next iteration.
1226    pub fn refresh_tunnel_bind_ports(&mut self) {
1227        let mut ports: Vec<(String, u16, u32)> = Vec::new();
1228        for (alias, tunnel) in &self.tunnels.active {
1229            let pid = tunnel.child.id();
1230            for rule in self.hosts_state.ssh_config.find_tunnel_directives(alias) {
1231                ports.push((alias.clone(), rule.bind_port, pid));
1232            }
1233        }
1234        self.tunnels.set_lsof_ports(ports);
1235    }
1236}
1237
1238/// Cycle list selection forward or backward with wraparound.
1239pub(crate) fn cycle_selection(state: &mut ListState, len: usize, forward: bool) {
1240    if len == 0 {
1241        return;
1242    }
1243    let i = match state.selected() {
1244        Some(i) => {
1245            if forward {
1246                if i >= len - 1 { 0 } else { i + 1 }
1247            } else if i == 0 {
1248                len - 1
1249            } else {
1250                i - 1
1251            }
1252        }
1253        None => 0,
1254    };
1255    state.select(Some(i));
1256}
1257
1258/// Jump forward by page_size items, clamping at the end (no wrap).
1259pub(crate) fn page_down(state: &mut ListState, len: usize, page_size: usize) {
1260    if len == 0 {
1261        return;
1262    }
1263    let current = state.selected().unwrap_or(0);
1264    let next = (current + page_size).min(len - 1);
1265    state.select(Some(next));
1266}
1267
1268/// Jump backward by page_size items, clamping at 0 (no wrap).
1269pub(crate) fn page_up(state: &mut ListState, len: usize, page_size: usize) {
1270    if len == 0 {
1271        return;
1272    }
1273    let current = state.selected().unwrap_or(0);
1274    let prev = current.saturating_sub(page_size);
1275    state.select(Some(prev));
1276}
1277
1278// Re-export the jump bar types so call sites keep referring to them via
1279// `crate::app::JumpHit` / `crate::app::JumpAction` without caring
1280// which submodule they live in.
1281pub use jump::{
1282    ContainerHit, HostHit, JumpAction, JumpActionTarget, JumpHit, JumpMode, JumpState, RecentRef,
1283    RecentsFile, SnippetHit, SourceKind, TunnelHit,
1284};
1285
1286/// Backwards-compatible alias for the old `PaletteCommand` (now `JumpAction`) name. The
1287/// renamed type is `JumpAction`. Test-only. there is no production
1288/// caller.
1289#[cfg(test)]
1290pub type PaletteCommand = JumpAction;
1291
1292/// Unified action set. Every action declares its `target` so dispatch
1293/// switches `top_page` first, then synthesises the hotkey for the right
1294/// handler. The jump bar shows this same list regardless of which
1295/// top-page was active when it opened. so the overlay size is
1296/// consistent and `Tunnels: Add tunnel` is reachable from the Hosts
1297/// tab and vice versa.
1298static ALL_JUMP_ACTIONS: &[JumpAction] = &[
1299    JumpAction {
1300        key: 'a',
1301        key_str: "a",
1302        label: "Hosts: Add host",
1303        aliases: &["new", "create"],
1304        target: JumpActionTarget::Hosts,
1305    },
1306    JumpAction {
1307        key: 'A',
1308        key_str: "A",
1309        label: "Hosts: Add pattern",
1310        aliases: &["new pattern", "wildcard"],
1311        target: JumpActionTarget::Hosts,
1312    },
1313    JumpAction {
1314        key: 'e',
1315        key_str: "e",
1316        label: "Hosts: Edit host",
1317        aliases: &["modify", "change"],
1318        target: JumpActionTarget::Hosts,
1319    },
1320    JumpAction {
1321        key: 'd',
1322        key_str: "d",
1323        label: "Hosts: Delete host",
1324        aliases: &["remove", "rm"],
1325        target: JumpActionTarget::Hosts,
1326    },
1327    JumpAction {
1328        key: 'c',
1329        key_str: "c",
1330        label: "Hosts: Clone host",
1331        aliases: &["duplicate", "copy"],
1332        target: JumpActionTarget::Hosts,
1333    },
1334    JumpAction {
1335        key: 'u',
1336        key_str: "u",
1337        label: "Hosts: Undo delete",
1338        aliases: &["restore"],
1339        target: JumpActionTarget::Hosts,
1340    },
1341    JumpAction {
1342        key: 't',
1343        key_str: "t",
1344        label: "Hosts: Tag host",
1345        aliases: &["label", "category"],
1346        target: JumpActionTarget::Hosts,
1347    },
1348    JumpAction {
1349        key: 'i',
1350        key_str: "i",
1351        label: "Hosts: Show all directives",
1352        aliases: &["raw", "config", "settings"],
1353        target: JumpActionTarget::Hosts,
1354    },
1355    JumpAction {
1356        key: 'y',
1357        key_str: "y",
1358        label: "Clipboard: Copy SSH command",
1359        aliases: &["yank"],
1360        target: JumpActionTarget::Hosts,
1361    },
1362    JumpAction {
1363        key: 'x',
1364        key_str: "x",
1365        label: "Clipboard: Copy config block",
1366        aliases: &["yank config"],
1367        target: JumpActionTarget::Hosts,
1368    },
1369    JumpAction {
1370        key: 'X',
1371        key_str: "X",
1372        label: "Hosts: Purge stale hosts",
1373        aliases: &["clean", "cleanup"],
1374        target: JumpActionTarget::Hosts,
1375    },
1376    JumpAction {
1377        key: 'F',
1378        key_str: "F",
1379        label: "Files: Browse remote files",
1380        aliases: &[
1381            "browse",
1382            "filesystem",
1383            "scp",
1384            "sftp",
1385            "transfer",
1386            "explorer",
1387            "open",
1388        ],
1389        target: JumpActionTarget::Hosts,
1390    },
1391    JumpAction {
1392        key: 'C',
1393        key_str: "C",
1394        label: "Containers: List containers",
1395        aliases: &["docker", "podman", "ps", "open"],
1396        target: JumpActionTarget::Hosts,
1397    },
1398    JumpAction {
1399        key: 'K',
1400        key_str: "K",
1401        label: "Keys: Manage SSH keys",
1402        aliases: &["identity", "id_rsa", "id_ed25519", "private key", "open"],
1403        target: JumpActionTarget::Hosts,
1404    },
1405    JumpAction {
1406        key: 'S',
1407        key_str: "S",
1408        label: "Providers: Manage cloud sync",
1409        aliases: &["cloud", "aws", "gcp", "azure", "hetzner", "sync", "open"],
1410        target: JumpActionTarget::Hosts,
1411    },
1412    JumpAction {
1413        key: 'V',
1414        key_str: "V",
1415        label: "Vault: Sign certificate",
1416        aliases: &["hashicorp", "ssh cert", "vault ssh"],
1417        target: JumpActionTarget::Hosts,
1418    },
1419    JumpAction {
1420        key: 'I',
1421        key_str: "I",
1422        label: "Hosts: Import from known_hosts",
1423        aliases: &["known", "import"],
1424        target: JumpActionTarget::Hosts,
1425    },
1426    JumpAction {
1427        key: 'm',
1428        key_str: "m",
1429        label: "Settings: Switch theme",
1430        aliases: &["color", "appearance", "dark", "light"],
1431        target: JumpActionTarget::Hosts,
1432    },
1433    JumpAction {
1434        key: 'n',
1435        key_str: "n",
1436        label: "Help: What's new",
1437        aliases: &["changelog", "news", "release notes"],
1438        target: JumpActionTarget::Hosts,
1439    },
1440    JumpAction {
1441        key: 'r',
1442        key_str: "r",
1443        label: "Snippets: Run snippet",
1444        aliases: &["execute", "command"],
1445        target: JumpActionTarget::Hosts,
1446    },
1447    JumpAction {
1448        key: 'R',
1449        key_str: "R",
1450        label: "Snippets: Run on all visible",
1451        aliases: &["batch", "execute all"],
1452        target: JumpActionTarget::Hosts,
1453    },
1454    JumpAction {
1455        key: 'p',
1456        key_str: "p",
1457        label: "Hosts: Ping host",
1458        aliases: &["health", "check"],
1459        target: JumpActionTarget::Hosts,
1460    },
1461    JumpAction {
1462        key: 'P',
1463        key_str: "P",
1464        label: "Hosts: Ping all hosts",
1465        aliases: &["health all"],
1466        target: JumpActionTarget::Hosts,
1467    },
1468    JumpAction {
1469        key: '!',
1470        key_str: "!",
1471        label: "Hosts: Show down only",
1472        aliases: &["filter offline", "down only"],
1473        target: JumpActionTarget::Hosts,
1474    },
1475    // Tunnel-tab actions. Disambiguated by label so they coexist with
1476    // hosts-tab hotkey letters in the same list. Dispatch switches to
1477    // Tunnels top-page before synthesising the keypress.
1478    JumpAction {
1479        key: 'T',
1480        key_str: "T",
1481        label: "Tunnels: Manage tunnels",
1482        aliases: &["forward", "port forward", "ssh -L", "ssh -R", "open"],
1483        target: JumpActionTarget::Hosts,
1484    },
1485    JumpAction {
1486        key: 'a',
1487        key_str: "a",
1488        label: "Tunnels: Add tunnel",
1489        aliases: &["new tunnel", "create tunnel", "forward"],
1490        target: JumpActionTarget::Tunnels,
1491    },
1492    JumpAction {
1493        key: 'e',
1494        key_str: "e",
1495        label: "Tunnels: Edit tunnel",
1496        aliases: &["modify tunnel"],
1497        target: JumpActionTarget::Tunnels,
1498    },
1499    JumpAction {
1500        key: 'd',
1501        key_str: "d",
1502        label: "Tunnels: Delete tunnel",
1503        aliases: &["remove tunnel"],
1504        target: JumpActionTarget::Tunnels,
1505    },
1506    JumpAction {
1507        key: 's',
1508        key_str: "s",
1509        label: "Tunnels: Sort",
1510        aliases: &["order tunnels"],
1511        target: JumpActionTarget::Tunnels,
1512    },
1513    JumpAction {
1514        key: 'R',
1515        key_str: "R",
1516        label: "Containers: Refresh all hosts",
1517        aliases: &["reload containers", "fetch", "rescan"],
1518        target: JumpActionTarget::Containers,
1519    },
1520    JumpAction {
1521        key: 's',
1522        key_str: "s",
1523        label: "Containers: Cycle sort",
1524        aliases: &["order containers", "sort by host", "sort by name"],
1525        target: JumpActionTarget::Containers,
1526    },
1527    JumpAction {
1528        key: 'v',
1529        key_str: "v",
1530        label: "Containers: Toggle detail panel",
1531        aliases: &["show details", "hide details", "compact view"],
1532        target: JumpActionTarget::Containers,
1533    },
1534    // Keys tab. Mirror the footer + handler bindings on the Keys tab so
1535    // typing `:` followed by part of a verb (e.g. `push`, `sign`, `copy`)
1536    // surfaces the same actions the keyboard shortcuts already trigger.
1537    JumpAction {
1538        key: 'c',
1539        key_str: "c",
1540        label: "Keys: Copy public key",
1541        aliases: &["yank", "clipboard", "pubkey"],
1542        target: JumpActionTarget::Keys,
1543    },
1544    JumpAction {
1545        key: 'p',
1546        key_str: "p",
1547        label: "Keys: Push to host",
1548        aliases: &["install", "ssh-copy-id", "deploy", "upload"],
1549        target: JumpActionTarget::Keys,
1550    },
1551    JumpAction {
1552        key: 'V',
1553        key_str: "V",
1554        label: "Keys: Sign Vault SSH certificate",
1555        aliases: &["vault", "renew cert", "sign"],
1556        target: JumpActionTarget::Keys,
1557    },
1558];
1559
1560/// Cap on hits rendered per section. Broad queries (e.g. one character)
1561/// match thousands of candidates; capping keeps the jump bar legible without
1562/// virtualizing the render. The selected hit always falls within the cap
1563/// because results are sorted by score before truncation.
1564pub const PALETTE_PER_SECTION_CAP: usize = 32;
1565
1566/// Field-prefix parser: `user:eric` → (`Some(QueryScope::User)`, "eric").
1567/// Returns `(None, query)` for queries without a recognised scope.
1568pub fn parse_query_scope(query: &str) -> (Option<QueryScope>, &str) {
1569    if let Some((prefix, rest)) = query.split_once(':') {
1570        let scope = match prefix.trim() {
1571            "user" => Some(QueryScope::User),
1572            "host" => Some(QueryScope::Hostname),
1573            "proxy" => Some(QueryScope::ProxyJump),
1574            "vault" => Some(QueryScope::VaultSsh),
1575            "tag" => Some(QueryScope::Tag),
1576            _ => None,
1577        };
1578        if scope.is_some() {
1579            return (scope, rest.trim_start());
1580        }
1581    }
1582    (None, query)
1583}
1584
1585#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1586pub enum QueryScope {
1587    User,
1588    Hostname,
1589    ProxyJump,
1590    VaultSsh,
1591    Tag,
1592}
1593
1594/// Truncate a string to `max` characters, appending "..." if cut.
1595fn preview(s: &str, max: usize) -> String {
1596    let s = s.replace('\n', " ");
1597    let chars: Vec<char> = s.chars().collect();
1598    if chars.len() <= max {
1599        s
1600    } else {
1601        let mut out: String = chars.iter().take(max.saturating_sub(3)).collect();
1602        out.push_str("...");
1603        out
1604    }
1605}
1606
1607/// Restrict scoring to a single field when the user prefixes the query
1608/// with `user:` / `host:` / `proxy:` / `vault:` / `tag:`. Returns `None`
1609/// when no scope is set OR when the scope does not apply to the hit
1610/// (e.g. `vault:` on a snippet). caller falls back to the full set.
1611fn scoped_haystacks_for(hit: &JumpHit, scope: Option<QueryScope>) -> Option<Vec<&str>> {
1612    let scope = scope?;
1613    match (hit, scope) {
1614        (JumpHit::Host(h), QueryScope::User) if !h.user.is_empty() => Some(vec![&h.user]),
1615        (JumpHit::Host(h), QueryScope::Hostname) if !h.hostname.is_empty() => {
1616            Some(vec![&h.hostname])
1617        }
1618        (JumpHit::Host(h), QueryScope::ProxyJump) if !h.proxy_jump.is_empty() => {
1619            Some(vec![&h.proxy_jump])
1620        }
1621        (JumpHit::Host(h), QueryScope::VaultSsh) => h.vault_ssh.as_deref().map(|s| vec![s]),
1622        (JumpHit::Host(h), QueryScope::Tag) => Some(h.tags.iter().map(|t| t.as_str()).collect()),
1623        // Scoped queries do not match other kinds.
1624        _ => None,
1625    }
1626}
1627
1628/// Determine which field caused the host hit to match. The renderer uses
1629/// this to append a `via user`, `via proxy`, `vault: <role>` hint to the
1630/// row when the matched field is not part of the visible columns. Returns
1631/// `None` if the alias/hostname (already visible) matched.
1632pub fn match_source_for_host(host: &HostHit, query: &str) -> Option<MatchSource> {
1633    if query.is_empty() {
1634        return None;
1635    }
1636    let q = query.to_lowercase();
1637    let alias_hit = host.alias.to_lowercase().contains(&q);
1638    let hostname_hit = host.hostname.to_lowercase().contains(&q);
1639    if alias_hit || hostname_hit {
1640        return None;
1641    }
1642    if !host.user.is_empty() && host.user.to_lowercase().contains(&q) {
1643        return Some(MatchSource::User);
1644    }
1645    if !host.proxy_jump.is_empty() && host.proxy_jump.to_lowercase().contains(&q) {
1646        return Some(MatchSource::ProxyJump);
1647    }
1648    if let Some(role) = &host.vault_ssh {
1649        if role.to_lowercase().contains(&q) {
1650            return Some(MatchSource::VaultSsh);
1651        }
1652    }
1653    if !host.identity_file.is_empty() && host.identity_file.to_lowercase().contains(&q) {
1654        return Some(MatchSource::IdentityFile);
1655    }
1656    None
1657}
1658
1659#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1660pub enum MatchSource {
1661    User,
1662    ProxyJump,
1663    VaultSsh,
1664    IdentityFile,
1665}
1666
1667fn kind_rank(k: SourceKind) -> u8 {
1668    match k {
1669        SourceKind::Host => 0,
1670        SourceKind::Tunnel => 1,
1671        SourceKind::Container => 2,
1672        SourceKind::Snippet => 3,
1673        SourceKind::Action => 4,
1674    }
1675}
1676
1677/// Find `prior` in `hits` and return its index, or `fallback` if the prior
1678/// hit is gone (e.g. the typed query no longer matches it). Used by
1679/// `recompute_jump_hits` so mid-typing arrow navigation does not lose
1680/// the user's place.
1681fn restore_selection(hits: &[JumpHit], prior: Option<&RecentRef>, fallback: usize) -> usize {
1682    if let Some(target) = prior {
1683        if let Some(idx) = hits.iter().position(|h| &h.identity() == target) {
1684            return idx;
1685        }
1686    }
1687    fallback.min(hits.len().saturating_sub(1))
1688}
1689
1690impl JumpAction {
1691    #[cfg(test)]
1692    pub fn all() -> &'static [JumpAction] {
1693        ALL_JUMP_ACTIONS
1694    }
1695
1696    /// The jump bar surfaces the same action set regardless of mode now.
1697    /// `mode` is preserved on the API so the dispatcher and test helpers
1698    /// can still pass through, but it no longer narrows the visible list.
1699    pub fn for_mode(_mode: JumpMode) -> &'static [JumpAction] {
1700        ALL_JUMP_ACTIONS
1701    }
1702}
1703
1704#[cfg(test)]
1705mod tests;