1use std::path::PathBuf;
7
8use crate::fs_util::atomic_write;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
13#[serde(rename_all = "snake_case")]
14pub enum SourceKind {
15 Host,
16 Tunnel,
17 Container,
18 Snippet,
19 Action,
20}
21
22impl SourceKind {
23 pub fn section_label(self) -> &'static str {
24 match self {
25 Self::Host => "HOSTS",
26 Self::Tunnel => "TUNNELS",
27 Self::Container => "CONTAINERS",
28 Self::Snippet => "SNIPPETS",
29 Self::Action => "ACTIONS",
30 }
31 }
32
33 pub fn render_order() -> [Self; 5] {
36 [
37 Self::Host,
38 Self::Tunnel,
39 Self::Container,
40 Self::Snippet,
41 Self::Action,
42 ]
43 }
44}
45
46#[derive(Debug, Clone, PartialEq, Eq)]
49pub enum JumpHit {
50 Action(JumpAction),
51 Host(HostHit),
52 Tunnel(TunnelHit),
53 Container(ContainerHit),
54 Snippet(SnippetHit),
55}
56
57impl JumpHit {
58 pub fn kind(&self) -> SourceKind {
59 match self {
60 Self::Action(_) => SourceKind::Action,
61 Self::Host(_) => SourceKind::Host,
62 Self::Tunnel(_) => SourceKind::Tunnel,
63 Self::Container(_) => SourceKind::Container,
64 Self::Snippet(_) => SourceKind::Snippet,
65 }
66 }
67
68 pub fn haystacks(&self) -> Vec<&str> {
74 match self {
75 Self::Action(a) => {
76 let mut v = Vec::with_capacity(2 + a.aliases.len());
77 v.push(a.label);
78 v.push(a.key_str);
79 for alias in a.aliases {
80 v.push(*alias);
81 }
82 v
83 }
84 Self::Host(h) => {
85 let mut v = Vec::with_capacity(7 + h.tags.len());
86 v.push(h.alias.as_str());
87 v.push(h.hostname.as_str());
88 if let Some(p) = &h.provider {
89 v.push(p.as_str());
90 }
91 for t in &h.tags {
92 v.push(t.as_str());
93 }
94 if !h.user.is_empty() {
95 v.push(h.user.as_str());
96 }
97 if !h.identity_file.is_empty() {
98 v.push(h.identity_file.as_str());
99 }
100 if !h.proxy_jump.is_empty() {
101 v.push(h.proxy_jump.as_str());
102 }
103 if let Some(role) = &h.vault_ssh {
104 v.push(role.as_str());
105 }
106 v
107 }
108 Self::Tunnel(t) => vec![t.alias.as_str(), t.destination.as_str(), &t.bind_port_str],
109 Self::Container(c) => vec![
110 c.container_name.as_str(),
111 c.alias.as_str(),
112 c.container_id.as_str(),
113 ],
114 Self::Snippet(s) => vec![s.name.as_str(), s.command_preview.as_str()],
115 }
116 }
117
118 pub fn identity(&self) -> RecentRef {
120 match self {
121 Self::Action(a) => RecentRef::new(SourceKind::Action, a.key.to_string()),
122 Self::Host(h) => RecentRef::new(SourceKind::Host, h.alias.clone()),
123 Self::Tunnel(t) => {
124 RecentRef::new(SourceKind::Tunnel, format!("{}:{}", t.alias, t.bind_port))
125 }
126 Self::Container(c) => RecentRef::new(
127 SourceKind::Container,
128 format!("{}/{}", c.alias, c.container_name),
129 ),
130 Self::Snippet(s) => RecentRef::new(SourceKind::Snippet, s.name.clone()),
131 }
132 }
133}
134
135#[derive(Debug, Clone, Copy, PartialEq, Eq)]
136pub struct JumpAction {
137 pub key: char,
138 pub key_str: &'static str,
142 pub label: &'static str,
143 pub aliases: &'static [&'static str],
144 pub target: JumpActionTarget,
149}
150
151#[derive(Debug, Clone, Copy, PartialEq, Eq)]
152pub enum JumpActionTarget {
153 Hosts,
154 Tunnels,
155 Containers,
156 Keys,
157}
158
159#[derive(Debug, Clone, PartialEq, Eq)]
160pub struct HostHit {
161 pub alias: String,
162 pub hostname: String,
163 pub tags: Vec<String>,
164 pub provider: Option<String>,
165 pub user: String,
166 pub identity_file: String,
167 pub proxy_jump: String,
168 pub vault_ssh: Option<String>,
169}
170
171#[derive(Debug, Clone, PartialEq, Eq)]
172pub struct TunnelHit {
173 pub alias: String,
174 pub bind_port: u16,
175 pub bind_port_str: String,
179 pub destination: String,
180 pub active: bool,
181}
182
183#[derive(Debug, Clone, PartialEq, Eq)]
184pub struct ContainerHit {
185 pub alias: String,
186 pub container_name: String,
187 pub container_id: String,
188 pub state: String,
189}
190
191#[derive(Debug, Clone, PartialEq, Eq)]
192pub struct SnippetHit {
193 pub name: String,
194 pub command_preview: String,
195}
196
197#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
200pub struct RecentRef {
201 pub kind: SourceKind,
202 pub key: String,
203}
204
205impl RecentRef {
206 pub fn new(kind: SourceKind, key: String) -> Self {
207 Self { kind, key }
208 }
209}
210
211#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
212pub struct RecentEntry {
213 #[serde(flatten)]
214 pub target: RecentRef,
215 pub last_used_unix: i64,
216}
217
218#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
221pub struct RecentsFile {
222 pub version: u32,
223 pub entries: Vec<RecentEntry>,
224}
225
226impl Default for RecentsFile {
227 fn default() -> Self {
228 Self {
229 version: 1,
230 entries: Vec::new(),
231 }
232 }
233}
234
235const RECENTS_VERSION: u32 = 1;
236const RECENTS_CAP: usize = 50;
237
238pub fn recents_path() -> Option<PathBuf> {
241 if let Some(p) = recents_path_override() {
242 return Some(p);
243 }
244 let home = dirs::home_dir()?;
245 Some(home.join(".purple").join("recents.json"))
246}
247
248#[cfg(test)]
254pub mod test_path {
255 use std::cell::RefCell;
256 use std::path::PathBuf;
257
258 thread_local! {
259 static OVERRIDE: RefCell<Option<PathBuf>> = const { RefCell::new(None) };
260 }
261
262 pub fn set(path: PathBuf) {
263 OVERRIDE.with(|cell| *cell.borrow_mut() = Some(path));
264 }
265
266 pub fn clear() {
267 OVERRIDE.with(|cell| *cell.borrow_mut() = None);
268 }
269
270 pub fn get() -> Option<PathBuf> {
271 OVERRIDE.with(|cell| cell.borrow().clone())
272 }
273}
274
275#[cfg(test)]
276fn recents_path_override() -> Option<PathBuf> {
277 test_path::get()
278}
279
280#[cfg(not(test))]
281fn recents_path_override() -> Option<PathBuf> {
282 None
283}
284
285pub fn load_recents() -> RecentsFile {
286 #[cfg(test)]
287 {
288 if test_path::get().is_none() {
291 return RecentsFile::default();
292 }
293 }
294 let Some(path) = recents_path() else {
295 return RecentsFile::default();
296 };
297 let bytes = match std::fs::read(&path) {
298 Ok(b) => b,
299 Err(_) => return RecentsFile::default(),
300 };
301 serde_json::from_slice(&bytes).unwrap_or_default()
302}
303
304pub fn save_recents(file: &RecentsFile) -> std::io::Result<()> {
305 #[cfg(test)]
311 {
312 if test_path::get().is_none() {
313 return Ok(());
314 }
315 }
316 let Some(path) = recents_path() else {
317 return Ok(());
318 };
319 if let Some(parent) = path.parent() {
320 std::fs::create_dir_all(parent)?;
321 }
322 let bytes = serde_json::to_vec_pretty(file).map_err(std::io::Error::other)?;
323 atomic_write(&path, &bytes)
324}
325
326pub fn rename_host_recent(file: &mut RecentsFile, old_alias: &str, new_alias: &str) -> bool {
333 if old_alias == new_alias {
334 return false;
335 }
336 let old_idx = file
337 .entries
338 .iter()
339 .position(|e| e.target.kind == SourceKind::Host && e.target.key == old_alias);
340 let Some(old_idx) = old_idx else {
341 return false;
342 };
343 let new_idx = file
344 .entries
345 .iter()
346 .position(|e| e.target.kind == SourceKind::Host && e.target.key == new_alias);
347 if let Some(new_idx) = new_idx {
348 let drop_idx =
349 if file.entries[old_idx].last_used_unix >= file.entries[new_idx].last_used_unix {
350 new_idx
351 } else {
352 old_idx
353 };
354 let keep_idx = if drop_idx == new_idx {
355 old_idx
356 } else {
357 new_idx
358 };
359 file.entries[keep_idx].target.key = new_alias.to_string();
360 file.entries.remove(drop_idx);
361 } else {
362 file.entries[old_idx].target.key = new_alias.to_string();
363 }
364 file.version = RECENTS_VERSION;
365 true
366}
367
368pub fn touch_recent(file: &mut RecentsFile, target: RecentRef) {
370 file.version = RECENTS_VERSION;
371 file.entries.retain(|e| e.target != target);
372 let now = current_unix_ts();
373 file.entries.insert(
374 0,
375 RecentEntry {
376 target,
377 last_used_unix: now,
378 },
379 );
380 if file.entries.len() > RECENTS_CAP {
381 file.entries.truncate(RECENTS_CAP);
382 }
383}
384
385fn current_unix_ts() -> i64 {
386 std::time::SystemTime::now()
387 .duration_since(std::time::UNIX_EPOCH)
388 .map(|d| d.as_secs() as i64)
389 .unwrap_or(0)
390}
391
392#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
396pub enum JumpMode {
397 #[default]
398 Hosts,
399 Tunnels,
400 Containers,
401 Keys,
402}
403
404pub const JUMP_EMPTY_STATE_ACTIONS_CAP: usize = 6;
409
410const EMPTY_STATE_TAB_BIAS: usize = 3;
416
417const CATEGORY_PRIORITY: &[&str] = &[
423 "Hosts",
424 "Tunnels",
425 "Containers",
426 "Files",
427 "Vault",
428 "Keys",
429 "Providers",
430 "Snippets",
431 "Clipboard",
432 "Settings",
433 "Help",
434];
435
436pub(crate) const PALETTE_ACTION_FLOOR: u32 = 30;
439
440fn round_robin_actions_by_category(actions: impl Iterator<Item = JumpAction>) -> Vec<JumpHit> {
448 let mut buckets: Vec<(String, Vec<JumpAction>)> = Vec::new();
449 for action in actions {
450 let category = action
451 .label
452 .split_once(':')
453 .map(|(c, _)| c.trim().to_string())
454 .unwrap_or_else(|| "Other".to_string());
455 if let Some(slot) = buckets.iter_mut().find(|(c, _)| c == &category) {
456 slot.1.push(action);
457 } else {
458 buckets.push((category, vec![action]));
459 }
460 }
461 let priority_index = |cat: &str| -> usize {
462 CATEGORY_PRIORITY
463 .iter()
464 .position(|p| *p == cat)
465 .unwrap_or(usize::MAX)
466 };
467 buckets.sort_by_key(|(c, _)| priority_index(c));
468 let mut out: Vec<JumpHit> = Vec::new();
469 let mut depth = 0usize;
470 let max_depth = buckets.iter().map(|(_, v)| v.len()).max().unwrap_or(0);
471 while depth < max_depth {
472 for (_, bucket) in &buckets {
473 if let Some(action) = bucket.get(depth) {
474 out.push(JumpHit::Action(*action));
475 }
476 }
477 depth += 1;
478 }
479 out
480}
481
482fn round_robin_actions_with_bias(
493 actions: impl Iterator<Item = JumpAction>,
494 preferred: JumpActionTarget,
495 bump: usize,
496) -> Vec<JumpHit> {
497 let collected: Vec<JumpAction> = actions.collect();
498 let biased: Vec<JumpAction> = collected
499 .iter()
500 .filter(|a| a.target == preferred)
501 .take(bump)
502 .copied()
503 .collect();
504 let biased_keys: std::collections::HashSet<char> = biased.iter().map(|a| a.key).collect();
505 let rest: Vec<JumpAction> = collected
506 .into_iter()
507 .filter(|a| !(biased_keys.contains(&a.key) && a.target == preferred))
508 .collect();
509 let mut out: Vec<JumpHit> = biased.into_iter().map(JumpHit::Action).collect();
510 out.extend(round_robin_actions_by_category(rest.into_iter()));
511 out
512}
513
514#[derive(Debug, Default)]
515pub struct JumpState {
516 pub query: String,
517 pub selected: usize,
518 pub mode: JumpMode,
519 pub hits: Vec<JumpHit>,
522 pub recents: Vec<JumpHit>,
524 pub cursor_revealed: bool,
530 pub matcher: Option<nucleo_matcher::Matcher>,
534}
535
536impl Clone for JumpState {
541 fn clone(&self) -> Self {
542 Self {
543 query: self.query.clone(),
544 selected: self.selected,
545 mode: self.mode,
546 hits: self.hits.clone(),
547 recents: self.recents.clone(),
548 cursor_revealed: self.cursor_revealed,
549 matcher: None,
550 }
551 }
552}
553
554impl JumpState {
555 pub fn for_mode(mode: JumpMode) -> Self {
556 Self {
557 mode,
558 ..Self::default()
559 }
560 }
561
562 pub fn push_query(&mut self, c: char) {
563 if self.query.len() < 64 {
564 self.query.push(c);
565 }
566 }
571
572 pub fn pop_query(&mut self) {
573 self.query.pop();
574 }
575
576 pub fn visible_hits(&self) -> Vec<JumpHit> {
585 if self.query.is_empty() {
586 let mut out: Vec<JumpHit> = self.recents.clone();
587 out.extend(self.empty_state_actions());
588 out
589 } else {
590 self.hits.clone()
591 }
592 }
593
594 fn filtered_actions_for_empty_state(&self) -> Vec<JumpAction> {
601 let recent_keys: std::collections::HashSet<RecentRef> =
602 self.recents.iter().map(|h| h.identity()).collect();
603 JumpAction::for_mode(self.mode)
604 .iter()
605 .filter(|a| {
606 let id = RecentRef::new(SourceKind::Action, a.key.to_string());
607 !recent_keys.contains(&id)
608 })
609 .copied()
610 .collect()
611 }
612
613 fn empty_state_actions(&self) -> Vec<JumpHit> {
619 let filtered = self.filtered_actions_for_empty_state();
620 let preferred_target = match self.mode {
621 JumpMode::Hosts => None,
622 JumpMode::Tunnels => Some(JumpActionTarget::Tunnels),
623 JumpMode::Containers => Some(JumpActionTarget::Containers),
624 JumpMode::Keys => Some(JumpActionTarget::Keys),
625 };
626 let actions = match preferred_target {
627 Some(t) => round_robin_actions_with_bias(filtered.into_iter(), t, EMPTY_STATE_TAB_BIAS),
628 None => round_robin_actions_by_category(filtered.into_iter()),
629 };
630 actions
631 .into_iter()
632 .take(JUMP_EMPTY_STATE_ACTIONS_CAP)
633 .collect()
634 }
635
636 pub fn empty_state_actions_total(&self) -> usize {
640 self.filtered_actions_for_empty_state().len()
641 }
642
643 pub fn grouped_hits(&self) -> Vec<(SourceKind, Vec<JumpHit>)> {
647 let visible = self.visible_hits();
648 let mut out = Vec::with_capacity(SourceKind::render_order().len());
649 for kind in SourceKind::render_order() {
650 let group: Vec<JumpHit> = visible
651 .iter()
652 .filter(|h| h.kind() == kind)
653 .cloned()
654 .collect();
655 if !group.is_empty() {
656 out.push((kind, group));
657 }
658 }
659 out
660 }
661
662 pub fn empty_state_groups(&self) -> Vec<(&'static str, Vec<JumpHit>)> {
667 let mut out: Vec<(&'static str, Vec<JumpHit>)> = Vec::new();
668 if !self.recents.is_empty() {
669 out.push(("RECENT", self.recents.clone()));
670 }
671 let actions = self.empty_state_actions();
674 if !actions.is_empty() {
675 out.push(("ACTIONS", actions));
676 }
677 out
678 }
679
680 pub fn selected_section(&self) -> Option<SourceKind> {
683 self.visible_hits().get(self.selected).map(|h| h.kind())
684 }
685
686 #[cfg(test)]
690 pub fn filtered_commands(&self) -> Vec<JumpAction> {
691 let all = JumpAction::for_mode(self.mode);
692 if self.query.is_empty() {
693 return all.to_vec();
694 }
695 let q = self.query.to_lowercase();
696 all.iter()
697 .filter(|cmd| {
698 cmd.label.to_lowercase().contains(&q)
699 || cmd.aliases.iter().any(|a| a.to_lowercase().contains(&q))
700 })
701 .copied()
702 .collect()
703 }
704
705 pub fn jump_next_section(&mut self) {
707 let visible = self.visible_hits();
708 if visible.is_empty() {
709 return;
710 }
711 if self.query.is_empty() {
712 let n_recent = self.recents.len();
720 if n_recent == 0 || n_recent >= visible.len() {
721 return;
722 }
723 if self.selected < n_recent {
724 self.selected = n_recent; } else {
726 self.selected = 0; }
728 return;
729 }
730 let groups = self.grouped_hits();
731 if groups.len() < 2 {
732 return;
733 }
734 let cur_kind = match self.selected_section() {
735 Some(k) => k,
736 None => {
737 self.selected = 0;
738 return;
739 }
740 };
741 let cur_idx = groups.iter().position(|(k, _)| *k == cur_kind).unwrap_or(0);
742 let next_idx = (cur_idx + 1) % groups.len();
743 let next_kind = groups[next_idx].0;
744 if let Some(pos) = visible.iter().position(|h| h.kind() == next_kind) {
745 self.selected = pos;
746 }
747 }
748}
749
750#[cfg(test)]
751pub mod tests {
752 use super::*;
753 use std::sync::Mutex;
754
755 pub(crate) static ENV_LOCK: Mutex<()> = Mutex::new(());
756
757 fn with_temp<F: FnOnce(&std::path::Path)>(f: F) {
758 let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
759 let dir = tempfile::tempdir().unwrap();
760 let path = dir.path().join("recents.json");
761 test_path::set(path.clone());
762 f(&path);
763 test_path::clear();
764 }
765
766 #[test]
767 fn section_labels_are_uppercase() {
768 for k in SourceKind::render_order() {
769 let label = k.section_label();
770 assert_eq!(label, label.to_uppercase(), "{:?} not uppercase", k);
771 }
772 }
773
774 #[test]
775 fn render_order_starts_with_hosts() {
776 assert_eq!(SourceKind::render_order()[0], SourceKind::Host);
777 assert_eq!(SourceKind::render_order()[4], SourceKind::Action);
778 }
779
780 #[test]
781 fn touch_moves_existing_to_front_and_caps() {
782 let mut f = RecentsFile::default();
783 for i in 0..(RECENTS_CAP + 5) {
784 touch_recent(&mut f, RecentRef::new(SourceKind::Host, format!("h{i}")));
785 }
786 assert_eq!(f.entries.len(), RECENTS_CAP);
787 let target = RecentRef::new(SourceKind::Host, format!("h{}", RECENTS_CAP + 2));
789 touch_recent(&mut f, target.clone());
790 assert_eq!(f.entries[0].target, target);
791 assert_eq!(f.entries.len(), RECENTS_CAP);
792 }
793
794 #[test]
795 fn save_then_load_roundtrip() {
796 with_temp(|_path| {
797 let mut f = RecentsFile::default();
798 touch_recent(&mut f, RecentRef::new(SourceKind::Action, "F".into()));
799 touch_recent(&mut f, RecentRef::new(SourceKind::Host, "web-01".into()));
800 save_recents(&f).expect("save");
801 let loaded = load_recents();
802 assert_eq!(loaded.version, RECENTS_VERSION);
803 assert_eq!(loaded.entries.len(), 2);
804 assert_eq!(loaded.entries[0].target.key, "web-01");
805 assert_eq!(loaded.entries[1].target.key, "F");
806 });
807 }
808
809 #[test]
810 fn missing_file_loads_empty() {
811 with_temp(|_path| {
812 let loaded = load_recents();
813 assert!(loaded.entries.is_empty());
814 });
815 }
816
817 #[test]
818 fn corrupt_file_loads_empty() {
819 with_temp(|path| {
820 std::fs::write(path, b"not json").unwrap();
821 let loaded = load_recents();
822 assert!(loaded.entries.is_empty());
823 });
824 }
825
826 fn host_entry(alias: &str, ts: i64) -> RecentEntry {
827 RecentEntry {
828 target: RecentRef::new(SourceKind::Host, alias.to_string()),
829 last_used_unix: ts,
830 }
831 }
832
833 #[test]
834 fn rename_host_recent_rewrites_key() {
835 let mut file = RecentsFile::default();
836 file.entries.push(host_entry("web-old", 100));
837 file.entries.push(RecentEntry {
838 target: RecentRef::new(SourceKind::Tunnel, "web-old:5432".to_string()),
839 last_used_unix: 90,
840 });
841
842 assert!(rename_host_recent(&mut file, "web-old", "web-new"));
843 assert_eq!(file.entries[0].target.kind, SourceKind::Host);
844 assert_eq!(file.entries[0].target.key, "web-new");
845 assert_eq!(file.entries[1].target.kind, SourceKind::Tunnel);
847 assert_eq!(file.entries[1].target.key, "web-old:5432");
848 }
849
850 #[test]
851 fn rename_host_recent_dedups_on_collision_keeping_most_recent() {
852 let mut file = RecentsFile::default();
853 file.entries.push(host_entry("a", 200));
856 file.entries.push(host_entry("b", 100));
857
858 assert!(rename_host_recent(&mut file, "a", "b"));
859 assert_eq!(file.entries.len(), 1);
860 assert_eq!(file.entries[0].target.key, "b");
861 assert_eq!(file.entries[0].last_used_unix, 200);
862 }
863
864 #[test]
865 fn rename_host_recent_dedups_when_new_key_is_newer() {
866 let mut file = RecentsFile::default();
867 file.entries.push(host_entry("a", 100));
868 file.entries.push(host_entry("b", 200));
869
870 assert!(rename_host_recent(&mut file, "a", "b"));
871 assert_eq!(file.entries.len(), 1);
872 assert_eq!(file.entries[0].target.key, "b");
873 assert_eq!(file.entries[0].last_used_unix, 200);
874 }
875
876 #[test]
877 fn rename_host_recent_noop_when_same() {
878 let mut file = RecentsFile::default();
879 file.entries.push(host_entry("a", 10));
880 assert!(!rename_host_recent(&mut file, "a", "a"));
881 assert_eq!(file.entries.len(), 1);
882 }
883
884 #[test]
885 fn rename_host_recent_noop_when_absent() {
886 let mut file = RecentsFile::default();
887 assert!(!rename_host_recent(&mut file, "ghost", "phantom"));
888 assert!(file.entries.is_empty());
889 }
890}