agentmux_srv_core/backend/notify/
pane_label.rs1use crate::backend::obj::{meta_get_string, MetaMapType};
8use crate::backend::tower::agentmux::page_host;
9
10use super::router::is_bidi_control;
11
12const TITLE_MAX: usize = 48;
14
15pub fn pane_label(meta: &MetaMapType) -> String {
20 let get = |key: &str| meta_get_string(meta, key, "");
21 let view = get("view");
22 let kind = match view.as_str() {
23 "browser" => "browser pane".to_string(),
24 "editor" => "editor".to_string(),
25 "term" => "terminal".to_string(),
26 "files" => "file browser".to_string(),
27 "sysinfo" => "system info pane".to_string(),
28 "help" => "help pane".to_string(),
29 "tower" => "Tower pane".to_string(),
30 "" => "pane".to_string(),
31 other => format!("{} pane", one_line(other, 24)),
32 };
33 let title = [get("frame:title")].into_iter().find(|t| !t.trim().is_empty()).or_else(|| match view.as_str() {
34 "browser" => page_host(&get("url")),
35 "editor" => last_segment(&get("file")),
36 "files" => last_segment(&get("files:path")),
37 _ => None,
38 });
39 match title.map(|t| one_line(&t, TITLE_MAX)).filter(|t| !t.is_empty()) {
40 Some(t) => format!("{kind}: {t}"),
41 None => kind,
42 }
43}
44
45fn last_segment(path: &str) -> Option<String> {
47 path.trim_end_matches(['/', '\\']).rsplit(['/', '\\']).next().filter(|s| !s.is_empty()).map(str::to_string)
48}
49
50fn one_line(raw: &str, max: usize) -> String {
53 let cleaned: String = raw.chars().filter(|c| !is_bidi_control(*c)).map(|c| if c.is_control() { ' ' } else { c }).collect();
54 let line = cleaned.split_whitespace().collect::<Vec<_>>().join(" ");
55 let mut out: String = line.chars().take(max).collect();
56 if line.chars().count() > max {
57 out.push('…');
58 }
59 out
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 fn meta(pairs: &[(&str, &str)]) -> MetaMapType {
67 pairs.iter().map(|(k, v)| (k.to_string(), serde_json::json!(v))).collect()
68 }
69
70 #[test]
71 fn a_browser_pane_is_named_by_its_title() {
72 assert_eq!(
73 pane_label(&meta(&[("view", "browser"), ("url", "https://portal.azure.com/x"), ("frame:title", "Azure: subscriptions")])),
74 "browser pane: Azure: subscriptions"
75 );
76 }
77
78 #[test]
79 fn an_untitled_browser_pane_gives_its_host_and_nothing_more_of_the_url() {
80 assert_eq!(
81 pane_label(&meta(&[("view", "browser"), ("url", "https://me:secret@example.com:8443/a/b?token=x")])),
82 "browser pane: example.com"
83 );
84 assert_eq!(pane_label(&meta(&[("view", "browser"), ("url", "not a url")])), "browser pane");
85 }
86
87 #[test]
88 fn an_editor_gives_the_file_name_not_the_path() {
89 assert_eq!(pane_label(&meta(&[("view", "editor"), ("file", "C:\\work\\private\\notes.md")])), "editor: notes.md");
90 assert_eq!(pane_label(&meta(&[("view", "editor"), ("file", "/home/me/a/b.rs"), ("frame:title", "plan")])), "editor: plan");
91 }
92
93 #[test]
94 fn other_panes_are_named_by_their_kind_and_never_as_an_agent() {
95 assert_eq!(pane_label(&meta(&[("view", "files"), ("files:path", "/home/me/proj/")])), "file browser: proj");
96 assert_eq!(pane_label(&meta(&[("view", "term")])), "terminal");
97 assert_eq!(pane_label(&meta(&[("view", "tower")])), "Tower pane");
98 assert_eq!(pane_label(&meta(&[("view", "widgetx")])), "widgetx pane");
99 assert_eq!(pane_label(&meta(&[])), "pane");
100 }
101
102 #[test]
103 fn a_title_is_one_short_clean_line() {
104 let long = format!("{}\u{202E}tail", "x".repeat(80));
105 let label = pane_label(&meta(&[("view", "browser"), ("frame:title", &format!(" a\nb\t{long}"))]));
106 assert!(label.starts_with("browser pane: a b xxx"), "{label}");
107 assert!(!label.contains('\u{202E}') && !label.contains('\n'), "{label}");
108 assert_eq!(label.chars().count(), "browser pane: ".chars().count() + TITLE_MAX + 1, "{label}");
109 }
110}