Skip to main content

purple_ssh/app/
selection.rs

1//! Selection and navigation helpers: keys, tags, tunnels, snippets, and the
2//! background tunnel polling that updates status when active tunnels exit.
3
4use std::path::Path;
5
6use ratatui::widgets::ListState;
7
8use super::{
9    BulkTagAction, BulkTagApplyResult, BulkTagEditorState, BulkTagRow, HostListItem,
10    ProxyJumpCandidate, Screen,
11};
12use crate::app::App;
13use crate::ssh_config::model::{HostEntry, PatternEntry};
14use crate::ssh_keys;
15
16impl App {
17    /// Transition to a new screen. Logs the transition at debug level for
18    /// support-bundle traceability. Callers should prefer this over direct
19    /// `app.screen = ...` assignment.
20    pub fn set_screen(&mut self, screen: Screen) {
21        if self.screen != screen {
22            log::debug!(
23                "screen: {} → {}",
24                self.screen.variant_name(),
25                screen.variant_name()
26            );
27        }
28        self.screen = screen;
29    }
30
31    /// Cycle to the next top page. Logs the transition so Tab-cycle
32    /// confusion ("I keep landing on the wrong page after Tab") leaves a
33    /// breadcrumb in `~/.purple/purple.log`. Callers should prefer this
34    /// over direct `app.top_page = app.top_page.next()` assignment.
35    pub fn cycle_top_page_next(&mut self) {
36        let old = self.top_page;
37        self.top_page = self.top_page.next();
38        log::debug!("[purple] top_page: {:?} → {:?} (Tab)", old, self.top_page);
39    }
40
41    /// Cycle to the previous top page. See `cycle_top_page_next`.
42    pub fn cycle_top_page_prev(&mut self) {
43        let old = self.top_page;
44        self.top_page = self.top_page.prev();
45        log::debug!(
46            "[purple] top_page: {:?} → {:?} (Shift+Tab)",
47            old,
48            self.top_page
49        );
50    }
51
52    /// Get the host index from the currently selected display list item.
53    pub fn selected_host_index(&self) -> Option<usize> {
54        if self.search.query.is_some() {
55            // In search mode, list_state indexes into filtered_indices
56            let sel = self.ui.list_state.selected()?;
57            self.search.filtered_indices.get(sel).copied()
58        } else {
59            // In normal mode, list_state indexes into display_list
60            let sel = self.ui.list_state.selected()?;
61            match self.hosts_state.display_list.get(sel) {
62                Some(HostListItem::Host { index }) => Some(*index),
63                _ => None,
64            }
65        }
66    }
67
68    /// Get the currently selected host entry.
69    pub fn selected_host(&self) -> Option<&HostEntry> {
70        self.selected_host_index()
71            .and_then(|i| self.hosts_state.list.get(i))
72    }
73
74    /// Get the currently selected pattern entry (if a pattern is selected).
75    pub fn selected_pattern(&self) -> Option<&PatternEntry> {
76        if self.search.query.is_some() {
77            let sel = self.ui.list_state.selected()?;
78            let host_count = self.search.filtered_indices.len();
79            if sel >= host_count {
80                let pattern_idx = sel - host_count;
81                return self
82                    .search
83                    .filtered_pattern_indices
84                    .get(pattern_idx)
85                    .and_then(|&i| self.hosts_state.patterns.get(i));
86            }
87            return None;
88        }
89        let sel = self.ui.list_state.selected()?;
90        match self.hosts_state.display_list.get(sel) {
91            Some(HostListItem::Pattern { index }) => self.hosts_state.patterns.get(*index),
92            _ => None,
93        }
94    }
95
96    /// Check if the currently selected item is a pattern.
97    pub fn is_pattern_selected(&self) -> bool {
98        if self.search.query.is_some() {
99            let Some(sel) = self.ui.list_state.selected() else {
100                return false;
101            };
102            let total =
103                self.search.filtered_indices.len() + self.search.filtered_pattern_indices.len();
104            return sel >= self.search.filtered_indices.len() && sel < total;
105        }
106        let Some(sel) = self.ui.list_state.selected() else {
107            return false;
108        };
109        matches!(
110            self.hosts_state.display_list.get(sel),
111            Some(HostListItem::Pattern { .. })
112        )
113    }
114
115    /// Move selection up, skipping group headers.
116    pub fn select_prev(&mut self) {
117        self.ui.detail_scroll = 0;
118        if self.search.query.is_some() {
119            let total =
120                self.search.filtered_indices.len() + self.search.filtered_pattern_indices.len();
121            super::cycle_selection(&mut self.ui.list_state, total, false);
122        } else {
123            self.select_prev_in_display_list();
124        }
125    }
126
127    /// Move selection down, skipping group headers.
128    pub fn select_next(&mut self) {
129        self.ui.detail_scroll = 0;
130        if self.search.query.is_some() {
131            let total =
132                self.search.filtered_indices.len() + self.search.filtered_pattern_indices.len();
133            super::cycle_selection(&mut self.ui.list_state, total, true);
134        } else {
135            self.select_next_in_display_list();
136        }
137    }
138
139    fn select_next_in_display_list(&mut self) {
140        if self.hosts_state.display_list.is_empty() {
141            return;
142        }
143        let len = self.hosts_state.display_list.len();
144        let current = self.ui.list_state.selected().unwrap_or(0);
145        // Find next selectable item after current (always skip headers)
146        for offset in 1..=len {
147            let idx = (current + offset) % len;
148            if matches!(
149                &self.hosts_state.display_list[idx],
150                HostListItem::Host { .. } | HostListItem::Pattern { .. }
151            ) {
152                self.ui.list_state.select(Some(idx));
153                return;
154            }
155        }
156    }
157
158    fn select_prev_in_display_list(&mut self) {
159        if self.hosts_state.display_list.is_empty() {
160            return;
161        }
162        let len = self.hosts_state.display_list.len();
163        let current = self.ui.list_state.selected().unwrap_or(0);
164        // Find prev selectable item before current (always skip headers)
165        for offset in 1..=len {
166            let idx = (current + len - offset) % len;
167            if matches!(
168                &self.hosts_state.display_list[idx],
169                HostListItem::Host { .. } | HostListItem::Pattern { .. }
170            ) {
171                self.ui.list_state.select(Some(idx));
172                return;
173            }
174        }
175    }
176
177    /// Page down in the host list, skipping group headers when ungrouped.
178    pub fn page_down_host(&mut self) {
179        self.ui.detail_scroll = 0;
180        const PAGE_SIZE: usize = 10;
181        if self.search.query.is_some() {
182            super::page_down(
183                &mut self.ui.list_state,
184                self.search.filtered_indices.len(),
185                PAGE_SIZE,
186            );
187        } else {
188            let current = self.ui.list_state.selected().unwrap_or(0);
189            let mut target = current;
190            let mut items_skipped = 0;
191            let len = self.hosts_state.display_list.len();
192            for i in (current + 1)..len {
193                if matches!(
194                    self.hosts_state.display_list[i],
195                    HostListItem::Host { .. } | HostListItem::Pattern { .. }
196                ) {
197                    target = i;
198                    items_skipped += 1;
199                    if items_skipped >= PAGE_SIZE {
200                        break;
201                    }
202                }
203            }
204            if target != current {
205                self.ui.list_state.select(Some(target));
206            }
207        }
208    }
209
210    /// Page up in the host list, skipping group headers.
211    pub fn page_up_host(&mut self) {
212        self.ui.detail_scroll = 0;
213        const PAGE_SIZE: usize = 10;
214        if self.search.query.is_some() {
215            super::page_up(
216                &mut self.ui.list_state,
217                self.search.filtered_indices.len(),
218                PAGE_SIZE,
219            );
220        } else {
221            let current = self.ui.list_state.selected().unwrap_or(0);
222            let mut target = current;
223            let mut items_skipped = 0;
224            for i in (0..current).rev() {
225                if matches!(
226                    self.hosts_state.display_list[i],
227                    HostListItem::Host { .. } | HostListItem::Pattern { .. }
228                ) {
229                    target = i;
230                    items_skipped += 1;
231                    if items_skipped >= PAGE_SIZE {
232                        break;
233                    }
234                }
235            }
236            if target != current {
237                self.ui.list_state.select(Some(target));
238            }
239        }
240    }
241    pub fn scan_keys(&mut self) {
242        let ssh_dir = self.env().paths().map(crate::runtime::env::Paths::ssh_dir);
243        if let Some(ssh_dir) = ssh_dir {
244            self.keys.list = ssh_keys::discover_keys(Path::new(&ssh_dir), &self.hosts_state.list);
245            if !self.keys.list.is_empty() && self.keys.list_state.selected().is_none() {
246                self.keys.list_state.select(Some(0));
247            }
248            self.reload.keys_dir_mtime = crate::app::reload_state::get_mtime(&ssh_dir);
249            self.reload.key_file_mtimes =
250                crate::app::reload_state::snapshot_key_mtimes(&ssh_dir, &self.keys.list);
251        }
252    }
253
254    /// Move key list selection up.
255    pub fn select_prev_key(&mut self) {
256        super::cycle_selection(&mut self.keys.list_state, self.keys.list.len(), false);
257    }
258
259    /// Move key list selection down.
260    pub fn select_next_key(&mut self) {
261        super::cycle_selection(&mut self.keys.list_state, self.keys.list.len(), true);
262    }
263
264    /// Move key picker selection up.
265    pub fn select_prev_picker_key(&mut self) {
266        super::cycle_selection(&mut self.ui.key_picker.list, self.keys.list.len(), false);
267    }
268
269    /// Move key picker selection down.
270    pub fn select_next_picker_key(&mut self) {
271        super::cycle_selection(&mut self.ui.key_picker.list, self.keys.list.len(), true);
272    }
273
274    /// Move password picker selection up.
275    pub fn select_prev_password_source(&mut self) {
276        super::cycle_selection(
277            &mut self.ui.password_picker.list,
278            crate::askpass::PASSWORD_SOURCES.len(),
279            false,
280        );
281    }
282
283    /// Move password picker selection down.
284    pub fn select_next_password_source(&mut self) {
285        super::cycle_selection(
286            &mut self.ui.password_picker.list,
287            crate::askpass::PASSWORD_SOURCES.len(),
288            true,
289        );
290    }
291
292    /// Get hosts available as ProxyJump targets (excludes the host being
293    /// edited), ranked so likely jump hosts appear on top. Ranking combines
294    /// three signals: usage count as ProxyJump on other hosts, alias or
295    /// hostname matching a jump-host keyword (`jump`, `bastion`, `gateway`,
296    /// `proxy`, `gw`), and sharing the last two domain labels with the
297    /// hostname of the host being edited. Items with a non-zero score are
298    /// grouped in a "suggested" section above a visual `Separator`. The
299    /// remaining items are listed alphabetically below. If no item scores,
300    /// the full list is alphabetical with no separator.
301    pub fn proxyjump_candidates(&self) -> Vec<ProxyJumpCandidate> {
302        let editing_alias = match &self.screen {
303            Screen::EditHost { alias, .. } => Some(alias.as_str()),
304            _ => None,
305        };
306        let editing_hostname = match &self.screen {
307            Screen::EditHost { alias, .. } => self
308                .hosts_state
309                .list
310                .iter()
311                .find(|h| h.alias == *alias)
312                .map(|h| h.hostname.as_str()),
313            _ => None,
314        };
315        let editing_suffix = editing_hostname.and_then(domain_suffix);
316
317        let usage_counts = proxyjump_usage_counts(&self.hosts_state.list, editing_alias);
318        let mut scored = score_proxyjump_candidates(
319            &self.hosts_state.list,
320            editing_alias,
321            editing_suffix.as_deref(),
322            &usage_counts,
323        );
324
325        // Top-3 suggestions: score > 0, sorted by score desc then alias asc.
326        scored.sort_by(|(sa, a), (sb, b)| sb.cmp(sa).then_with(|| a.alias.cmp(&b.alias)));
327        let suggested: Vec<&HostEntry> = scored
328            .iter()
329            .filter(|(s, _)| *s > 0)
330            .take(3)
331            .map(|(_, h)| *h)
332            .collect();
333        let suggested_aliases: std::collections::HashSet<&str> =
334            suggested.iter().map(|h| h.alias.as_str()).collect();
335
336        // Rest: everything not suggested, alphabetical by alias.
337        scored.sort_by(|(_, a), (_, b)| a.alias.cmp(&b.alias));
338        let rest: Vec<&HostEntry> = scored
339            .into_iter()
340            .map(|(_, h)| h)
341            .filter(|h| !suggested_aliases.contains(h.alias.as_str()))
342            .collect();
343
344        build_proxyjump_items(&suggested, &rest)
345    }
346
347    /// Find the first selectable (non-separator) index in the ProxyJump
348    /// picker, or None if the list has no hosts.
349    pub fn proxyjump_first_host_index(&self) -> Option<usize> {
350        self.proxyjump_candidates()
351            .iter()
352            .position(|c| matches!(c, ProxyJumpCandidate::Host { .. }))
353    }
354
355    /// Move proxyjump picker selection up, skipping separators.
356    pub fn select_prev_proxyjump(&mut self) {
357        step_proxyjump_selection(self, false);
358    }
359
360    /// Move proxyjump picker selection down, skipping separators.
361    pub fn select_next_proxyjump(&mut self) {
362        step_proxyjump_selection(self, true);
363    }
364
365    /// Collect unique Vault SSH roles from all hosts and providers, sorted.
366    pub fn vault_role_candidates(&self) -> Vec<String> {
367        let mut seen = std::collections::HashSet::new();
368        let mut roles = Vec::new();
369        for host in &self.hosts_state.list {
370            if let Some(ref role) = host.vault_ssh {
371                if seen.insert(role.clone()) {
372                    roles.push(role.clone());
373                }
374            }
375        }
376        // Also collect from provider configs.
377        for section in &self.providers.config.sections {
378            let role = section.vault_role.trim();
379            if !role.is_empty() && seen.insert(role.to_string()) {
380                roles.push(role.to_string());
381            }
382        }
383        roles.sort();
384        roles
385    }
386
387    /// Move vault role picker selection up.
388    pub fn select_prev_vault_role(&mut self) {
389        let len = self.vault_role_candidates().len();
390        super::cycle_selection(&mut self.ui.vault_role_picker.list, len, false);
391    }
392
393    /// Move vault role picker selection down.
394    pub fn select_next_vault_role(&mut self) {
395        let len = self.vault_role_candidates().len();
396        super::cycle_selection(&mut self.ui.vault_role_picker.list, len, true);
397    }
398
399    /// Collect all unique tags from hosts, sorted alphabetically.
400    pub fn collect_unique_tags(&self) -> Vec<String> {
401        let mut seen = std::collections::HashSet::new();
402        let mut tags = Vec::new();
403        let mut has_stale = false;
404        let mut has_vault_ssh = false;
405        let mut has_vault_kv = false;
406        for host in &self.hosts_state.list {
407            for tag in host.provider_tags.iter().chain(host.tags.iter()) {
408                if seen.insert(tag.clone()) {
409                    tags.push(tag.clone());
410                }
411            }
412            if let Some(ref provider) = host.provider {
413                if seen.insert(provider.clone()) {
414                    tags.push(provider.clone());
415                }
416            }
417            if host.stale.is_some() {
418                has_stale = true;
419            }
420            if crate::vault_ssh::resolve_vault_role(
421                host.vault_ssh.as_deref(),
422                host.provider.as_deref(),
423                host.provider_label.as_deref(),
424                &self.providers.config,
425            )
426            .is_some()
427            {
428                has_vault_ssh = true;
429            }
430            if host
431                .askpass
432                .as_deref()
433                .map(|s| s.starts_with("vault:"))
434                .unwrap_or(false)
435            {
436                has_vault_kv = true;
437            }
438        }
439        for pattern in &self.hosts_state.patterns {
440            for tag in &pattern.tags {
441                if seen.insert(tag.clone()) {
442                    tags.push(tag.clone());
443                }
444            }
445        }
446        if has_stale && seen.insert("stale".to_string()) {
447            tags.push("stale".to_string());
448        }
449        if !has_vault_ssh {
450            for section in &self.providers.config.sections {
451                if !section.vault_role.is_empty() {
452                    has_vault_ssh = true;
453                    break;
454                }
455            }
456        }
457        if has_vault_ssh && seen.insert("vault-ssh".to_string()) {
458            tags.push("vault-ssh".to_string());
459        }
460        if has_vault_kv && seen.insert("vault-kv".to_string()) {
461            tags.push("vault-kv".to_string());
462        }
463        tags.sort_by_cached_key(|a| a.to_lowercase());
464        tags
465    }
466
467    /// Open the bulk tag editor for every host currently in `multi_select`.
468    /// Returns false (and leaves the screen untouched) when the selection
469    /// is empty or contains only pattern entries — callers can then fall
470    /// back to single-host tag editing or show a status message.
471    ///
472    /// Hosts that live in an Include file are still listed in `aliases` but
473    /// get surfaced via `skipped_included`. `bulk_tag_apply` honours that
474    /// split so included hosts are never mutated in place.
475    pub fn open_bulk_tag_editor(&mut self) -> bool {
476        let mut aliases: Vec<String> = Vec::new();
477        let mut skipped: Vec<String> = Vec::new();
478        let mut alias_set: std::collections::HashSet<String> = std::collections::HashSet::new();
479        for &idx in &self.hosts_state.multi_select {
480            if let Some(host) = self.hosts_state.list.get(idx) {
481                if !alias_set.insert(host.alias.clone()) {
482                    continue;
483                }
484                if host.source_file.is_some() {
485                    skipped.push(host.alias.clone());
486                }
487                aliases.push(host.alias.clone());
488            }
489        }
490        if aliases.is_empty() {
491            return false;
492        }
493        aliases.sort();
494        skipped.sort();
495
496        // Collect candidate tags: union of all user tags across the whole
497        // config. This lets users apply an existing tag that none of the
498        // selected hosts have yet (the common "tag a new batch with prod"
499        // case).
500        let mut candidate_tags: std::collections::BTreeSet<String> =
501            std::collections::BTreeSet::new();
502        for host in &self.hosts_state.list {
503            for tag in &host.tags {
504                candidate_tags.insert(tag.clone());
505            }
506        }
507        for pattern in &self.hosts_state.patterns {
508            for tag in &pattern.tags {
509                candidate_tags.insert(tag.clone());
510            }
511        }
512
513        let selected_set: std::collections::HashSet<&str> =
514            aliases.iter().map(|s| s.as_str()).collect();
515        let rows: Vec<BulkTagRow> = candidate_tags
516            .into_iter()
517            .map(|tag| {
518                let initial_count = self
519                    .hosts_state
520                    .list
521                    .iter()
522                    .filter(|h| selected_set.contains(h.alias.as_str()))
523                    .filter(|h| h.tags.iter().any(|t| t == &tag))
524                    .count();
525                BulkTagRow {
526                    tag,
527                    initial_count,
528                    action: BulkTagAction::Leave,
529                }
530            })
531            .collect();
532
533        // Snapshot baseline actions for the dirty-check on Esc. Every row
534        // starts at `Leave`; the snapshot is the same length as `rows` so
535        // `is_dirty` short-circuits before scanning when nothing has changed.
536        let initial_actions: Vec<BulkTagAction> = rows.iter().map(|r| r.action).collect();
537        self.forms.bulk_tag_editor = BulkTagEditorState {
538            rows,
539            aliases,
540            skipped_included: skipped,
541            new_tag_input: None,
542            new_tag_cursor: 0,
543            initial_actions,
544        };
545        self.ui.bulk_tag_editor_state = ListState::default();
546        if !self.forms.bulk_tag_editor.rows.is_empty() {
547            self.ui.bulk_tag_editor_state.select(Some(0));
548        }
549        self.set_screen(Screen::BulkTagEditor);
550        true
551    }
552
553    /// Move bulk tag editor selection down.
554    pub fn bulk_tag_editor_next(&mut self) {
555        super::cycle_selection(
556            &mut self.ui.bulk_tag_editor_state,
557            self.forms.bulk_tag_editor.rows.len(),
558            true,
559        );
560    }
561
562    /// Move bulk tag editor selection up.
563    pub fn bulk_tag_editor_prev(&mut self) {
564        super::cycle_selection(
565            &mut self.ui.bulk_tag_editor_state,
566            self.forms.bulk_tag_editor.rows.len(),
567            false,
568        );
569    }
570
571    /// Cycle the action on the currently selected row:
572    /// `Leave` → `AddToAll` → `RemoveFromAll` → `Leave`.
573    pub fn bulk_tag_editor_cycle_current(&mut self) {
574        super::bulk_tag_cycle_current(&self.ui, &mut self.forms);
575    }
576
577    /// Append a freshly typed tag to the row list. The new row is marked
578    /// `AddToAll` so the user's intent ("add this new tag to all selected
579    /// hosts") is preserved without a second keystroke. No-op for empty
580    /// input or duplicate tag names.
581    pub fn bulk_tag_editor_commit_new_tag(&mut self) {
582        super::bulk_tag_commit_new_tag(&mut self.ui, &mut self.forms);
583    }
584
585    /// Apply all pending actions from the bulk tag editor. Leaves the
586    /// config untouched (and returns an error) if the write fails so the
587    /// user can retry without losing state. On success, hosts are reloaded
588    /// (which clears `multi_select`).
589    pub fn bulk_tag_apply(&mut self) -> Result<BulkTagApplyResult, String> {
590        // Compute + config write run on the host and form slices alone; the
591        // returned result drives the caller's control flow inline. Only the
592        // post-write whole-App tails (mtime refresh, reload) need full App,
593        // and only when something actually changed on disk.
594        let result = super::apply_bulk_tags(&mut self.hosts_state, &mut self.forms)?;
595        if result.changed_hosts > 0 {
596            self.update_last_modified();
597            self.reload_hosts();
598        }
599        Ok(result)
600    }
601
602    /// Open the tag picker overlay.
603    pub fn open_tag_picker(&mut self) {
604        self.tags.list = self.collect_unique_tags();
605        self.ui.tag_picker_state = ListState::default();
606        if !self.tags.list.is_empty() {
607            self.ui.tag_picker_state.select(Some(0));
608        }
609        self.set_screen(Screen::TagPicker);
610    }
611
612    /// Move tag picker selection up.
613    pub fn select_prev_tag(&mut self) {
614        super::cycle_selection(&mut self.ui.tag_picker_state, self.tags.list.len(), false);
615    }
616
617    /// Move tag picker selection down.
618    pub fn select_next_tag(&mut self) {
619        super::cycle_selection(&mut self.ui.tag_picker_state, self.tags.list.len(), true);
620    }
621
622    /// Load tunnel directives for a host alias.
623    /// Uses find_tunnel_directives for Include-aware, multi-pattern host lookup.
624    pub fn refresh_tunnel_list(&mut self, alias: &str) {
625        self.tunnels
626            .load_directives(&self.hosts_state.ssh_config, alias);
627    }
628
629    /// Move tunnel list selection up.
630    pub fn select_prev_tunnel(&mut self) {
631        super::cycle_selection(
632            &mut self.ui.tunnel_list_state,
633            self.tunnels.list.len(),
634            false,
635        );
636    }
637
638    /// Move tunnel list selection down.
639    pub fn select_next_tunnel(&mut self) {
640        super::cycle_selection(
641            &mut self.ui.tunnel_list_state,
642            self.tunnels.list.len(),
643            true,
644        );
645    }
646
647    /// Move snippet picker selection up.
648    pub fn select_prev_snippet(&mut self) {
649        super::cycle_selection(
650            &mut self.ui.snippet_picker_state,
651            self.snippets.store.snippets.len(),
652            false,
653        );
654    }
655
656    /// Move snippet picker selection down.
657    pub fn select_next_snippet(&mut self) {
658        super::cycle_selection(
659            &mut self.ui.snippet_picker_state,
660            self.snippets.store.snippets.len(),
661            true,
662        );
663    }
664
665    /// Poll active tunnels for exit status. Returns messages for any that exited.
666    /// Move selection to the next non-header item.
667    pub fn select_next_skipping_headers(&mut self) {
668        let current = self.ui.list_state.selected().unwrap_or(0);
669        for i in (current + 1)..self.hosts_state.display_list.len() {
670            if !matches!(
671                self.hosts_state.display_list[i],
672                HostListItem::GroupHeader(_)
673            ) {
674                self.ui.list_state.select(Some(i));
675                return;
676            }
677        }
678    }
679
680    /// Move selection to the previous non-header item.
681    pub fn select_prev_skipping_headers(&mut self) {
682        let current = self.ui.list_state.selected().unwrap_or(0);
683        for i in (0..current).rev() {
684            if !matches!(
685                self.hosts_state.display_list[i],
686                HostListItem::GroupHeader(_)
687            ) {
688                self.ui.list_state.select(Some(i));
689                return;
690            }
691        }
692    }
693}
694
695const JUMP_KEYWORDS: &[&str] = &["jump", "bastion", "gateway", "proxy", "gw"];
696
697/// Count how often each alias appears as a ProxyJump hop across all hosts,
698/// excluding the host currently being edited.
699fn proxyjump_usage_counts(
700    hosts: &[HostEntry],
701    editing_alias: Option<&str>,
702) -> std::collections::HashMap<String, u32> {
703    let mut counts: std::collections::HashMap<String, u32> = std::collections::HashMap::new();
704    for h in hosts {
705        if h.proxy_jump.is_empty() || editing_alias == Some(h.alias.as_str()) {
706            continue;
707        }
708        for hop in parse_proxy_jump_hops(&h.proxy_jump) {
709            *counts.entry(hop).or_insert(0) += 1;
710        }
711    }
712    counts
713}
714
715/// Score each host as a ProxyJump candidate. Excludes the host being edited.
716/// Score = usage * 10 + keyword_hit * 5 + shared_domain_suffix * 3.
717fn score_proxyjump_candidates<'a>(
718    hosts: &'a [HostEntry],
719    editing_alias: Option<&str>,
720    editing_suffix: Option<&str>,
721    usage_counts: &std::collections::HashMap<String, u32>,
722) -> Vec<(u32, &'a HostEntry)> {
723    hosts
724        .iter()
725        .filter(|h| editing_alias.is_none_or(|a| h.alias != a))
726        .map(|h| {
727            let usage = usage_counts.get(&h.alias).copied().unwrap_or(0);
728            let kw = has_jump_keyword(&h.alias, &h.hostname);
729            let same = editing_suffix
730                .and_then(|suf| domain_suffix(&h.hostname).map(|s| s == suf))
731                .unwrap_or(false);
732            let score = usage * 10 + u32::from(kw) * 5 + u32::from(same) * 3;
733            (score, h)
734        })
735        .collect()
736}
737
738/// Assemble the final picker list from pre-sorted `suggested` and `rest`
739/// slices. Inserts a `Suggestions` section label and a `Separator` only when
740/// both sides are non-empty.
741fn build_proxyjump_items(suggested: &[&HostEntry], rest: &[&HostEntry]) -> Vec<ProxyJumpCandidate> {
742    let mut items = Vec::with_capacity(suggested.len() + rest.len() + 2);
743    if !suggested.is_empty() {
744        items.push(ProxyJumpCandidate::SectionLabel("Suggestions"));
745    }
746    for h in suggested {
747        items.push(ProxyJumpCandidate::Host {
748            alias: h.alias.clone(),
749            hostname: h.hostname.clone(),
750            suggested: true,
751        });
752    }
753    if !suggested.is_empty() && !rest.is_empty() {
754        items.push(ProxyJumpCandidate::Separator);
755    }
756    for h in rest {
757        items.push(ProxyJumpCandidate::Host {
758            alias: h.alias.clone(),
759            hostname: h.hostname.clone(),
760            suggested: false,
761        });
762    }
763    items
764}
765
766/// Parse a ProxyJump directive value into its list of alias hops, stripping
767/// optional `user@` prefix and `:port` suffix (including IPv6 brackets).
768/// Malformed hops (empty, missing closing bracket on an IPv6 literal) are
769/// dropped rather than passed through as garbage that could never match a
770/// real alias.
771pub(crate) fn parse_proxy_jump_hops(proxy_jump: &str) -> Vec<String> {
772    proxy_jump
773        .split(',')
774        .filter_map(|hop| {
775            let h = hop.trim();
776            if h.is_empty() {
777                return None;
778            }
779            let h = h.split_once('@').map_or(h, |(_, host)| host);
780            let h = if let Some(bracketed) = h.strip_prefix('[') {
781                let (inner, _) = bracketed.split_once(']')?;
782                inner
783            } else {
784                h.rsplit_once(':').map_or(h, |(host, _)| host)
785            };
786            if h.is_empty() {
787                None
788            } else {
789                Some(h.to_string())
790            }
791        })
792        .collect()
793}
794
795/// True when the alias or hostname mentions a common jump-host keyword
796/// (`jump`, `bastion`, `gateway`, `proxy`, `gw`) as a substring.
797pub(crate) fn has_jump_keyword(alias: &str, hostname: &str) -> bool {
798    let a = alias.to_ascii_lowercase();
799    let h = hostname.to_ascii_lowercase();
800    JUMP_KEYWORDS
801        .iter()
802        .any(|kw| a.contains(kw) || h.contains(kw))
803}
804
805/// Extract the last two dot-separated labels of a hostname for domain
806/// matching. Returns None for single-label hostnames, IPv4 literals, and
807/// bracketed IPv6 literals where domain matching would be meaningless.
808/// Also rejects any string that parses as a valid `IpAddr` (which catches
809/// 4-octet IPv4 shapes without relying on a naive all-digits-per-label
810/// check that would miss mixed strings like `192.168.1.foo`).
811pub(crate) fn domain_suffix(hostname: &str) -> Option<String> {
812    let h = hostname.trim();
813    if h.is_empty() || h.starts_with('[') {
814        return None;
815    }
816    if h.parse::<std::net::IpAddr>().is_ok() {
817        return None;
818    }
819    let labels: Vec<&str> = h.split('.').collect();
820    if labels.len() < 2 {
821        return None;
822    }
823    // Trailing empty labels (e.g. `example.com.` FQDN) would silently
824    // produce a bogus `.com` suffix; normalise by trimming them off.
825    let mut end = labels.len();
826    while end > 0 && labels[end - 1].is_empty() {
827        end -= 1;
828    }
829    if end < 2 {
830        return None;
831    }
832    let tail = &labels[end - 2..end];
833    Some(tail.join(".").to_ascii_lowercase())
834}
835
836/// Step the ProxyJump picker selection one position in the requested
837/// direction, wrapping around and skipping any `Separator` entries. When
838/// nothing is currently selected, the first step lands on the first
839/// selectable host (forward) or the last selectable host (backward)
840/// instead of advancing past index 0.
841fn step_proxyjump_selection(app: &mut App, forward: bool) {
842    let candidates = app.proxyjump_candidates();
843    let len = candidates.len();
844    if len == 0 {
845        app.ui.proxyjump_picker.list.select(None);
846        return;
847    }
848    // When no prior selection exists, seed `next` so the first modular
849    // step lands on index 0 (forward) or len-1 (backward). Without this
850    // seed a fresh picker with selected() == None would skip index 0 on
851    // a Down press.
852    let seed: usize = match app.ui.proxyjump_picker.list.selected() {
853        Some(idx) => idx,
854        None if forward => len - 1,
855        None => 0,
856    };
857    let mut next = seed;
858    for _ in 0..len {
859        next = if forward {
860            (next + 1) % len
861        } else {
862            (next + len - 1) % len
863        };
864        if matches!(candidates.get(next), Some(ProxyJumpCandidate::Host { .. })) {
865            app.ui.proxyjump_picker.list.select(Some(next));
866            return;
867        }
868    }
869}