1use 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 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 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 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 pub fn selected_host_index(&self) -> Option<usize> {
54 if self.search.query.is_some() {
55 let sel = self.ui.list_state.selected()?;
57 self.search.filtered_indices.get(sel).copied()
58 } else {
59 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 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 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 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 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 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 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 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 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 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 pub fn select_prev_key(&mut self) {
256 super::cycle_selection(&mut self.keys.list_state, self.keys.list.len(), false);
257 }
258
259 pub fn select_next_key(&mut self) {
261 super::cycle_selection(&mut self.keys.list_state, self.keys.list.len(), true);
262 }
263
264 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 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 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 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 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 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 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 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 pub fn select_prev_proxyjump(&mut self) {
357 step_proxyjump_selection(self, false);
358 }
359
360 pub fn select_next_proxyjump(&mut self) {
362 step_proxyjump_selection(self, true);
363 }
364
365 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 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 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 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 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 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 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 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 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 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 pub fn bulk_tag_editor_cycle_current(&mut self) {
574 super::bulk_tag_cycle_current(&self.ui, &mut self.forms);
575 }
576
577 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 pub fn bulk_tag_apply(&mut self) -> Result<BulkTagApplyResult, String> {
590 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 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 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 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 pub fn refresh_tunnel_list(&mut self, alias: &str) {
625 self.tunnels
626 .load_directives(&self.hosts_state.ssh_config, alias);
627 }
628
629 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 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 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 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 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 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
697fn 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
715fn 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
738fn 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
766pub(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
795pub(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
805pub(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 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
836fn 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 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}