1use std::path::PathBuf;
7use std::process::Command;
8
9use rootcause::prelude::ResultExt;
10use rootcause::report;
11use serde::Deserialize;
12
13const BIN: &str = "wezterm";
14
15pub fn send_text_to_pane_cmd(text: &str, pane_id: i64) -> String {
17 format!("{BIN} cli send-text {text} --pane-id '{pane_id}' --no-paste")
18}
19
20pub fn submit_pane_cmd(pane_id: i64) -> String {
22 format!(r#"printf "\r" | {BIN} cli send-text --pane-id '{pane_id}' --no-paste"#)
23}
24
25pub fn activate_pane_cmd(pane_id: i64) -> String {
27 format!("{BIN} cli activate-pane --pane-id '{pane_id}'")
28}
29
30pub fn get_current_pane_id() -> rootcause::Result<i64> {
37 let value = std::env::var("WEZTERM_PANE").context("error missing WEZTERM_PANE environment variable")?;
38 Ok(value
39 .parse()
40 .context("error parsing WEZTERM_PANE value as i64")
41 .attach_with(|| format!("value={value:?}"))?)
42}
43
44pub fn get_all_panes(envs: &[(&str, &str)]) -> rootcause::Result<Vec<WeztermPane>> {
53 let mut cmd = Command::new(BIN);
54 cmd.args(["cli", "list", "--format", "json"]);
55 cmd.envs(envs.iter().copied());
56 Ok(
57 serde_json::from_slice(&cmd.output().context("error running wezterm cli list")?.stdout)
58 .context("error parsing wezterm cli list output")
59 .attach("format=JSON")?,
60 )
61}
62
63pub fn get_sibling_pane_with_titles(
69 panes: &[WeztermPane],
70 current_pane_id: i64,
71 pane_titles: &[&str],
72) -> rootcause::Result<WeztermPane> {
73 let current_pane_tab_id = panes
74 .iter()
75 .find(|w| w.pane_id == current_pane_id)
76 .ok_or_else(|| report!("error finding current pane"))
77 .attach_with(|| format!("pane_id={current_pane_id} panes={panes:#?}"))?
78 .tab_id;
79
80 Ok(panes
81 .iter()
82 .find(|w| w.tab_id == current_pane_tab_id && pane_titles.contains(&w.title.as_str()))
83 .ok_or_else(|| {
84 report!("error finding pane title in tab")
85 .attach(format!("pane_titles={pane_titles:#?} tab_id={current_pane_tab_id}"))
86 })?
87 .clone())
88}
89
90#[derive(Clone, Debug, Deserialize)]
92#[cfg_attr(any(test, feature = "fake"), derive(fake::Dummy))]
93pub struct WeztermPane {
94 pub cursor_shape: String,
96 pub cursor_visibility: String,
98 pub cursor_x: i64,
100 pub cursor_y: i64,
102 pub cwd: PathBuf,
104 pub is_active: bool,
106 pub is_zoomed: bool,
108 pub left_col: i64,
110 pub pane_id: i64,
112 pub size: WeztermPaneSize,
114 pub tab_id: i64,
116 pub tab_title: String,
118 pub title: String,
120 pub top_row: i64,
122 pub tty_name: String,
124 pub window_id: i64,
126 pub window_title: String,
128 pub workspace: String,
130}
131
132impl WeztermPane {
133 pub fn is_sibling_terminal_pane_of(&self, other: &Self) -> bool {
136 self.pane_id != other.pane_id && self.tab_id == other.tab_id && self.cwd.starts_with(&other.cwd)
137 }
138
139 pub fn absolute_cwd(&self) -> PathBuf {
141 let mut path_parts = self.cwd.components();
142 path_parts.next(); path_parts.next(); PathBuf::from("/").join(path_parts.collect::<PathBuf>())
145 }
146}
147
148#[derive(Clone, Debug, Deserialize)]
150#[cfg_attr(any(test, feature = "fake"), derive(fake::Dummy))]
151pub struct WeztermPaneSize {
152 pub cols: i64,
154 pub dpi: i64,
156 pub pixel_height: i64,
158 pub pixel_width: i64,
160 pub rows: i64,
162}
163
164#[cfg(test)]
165mod tests {
166 use super::*;
167
168 #[test]
169 fn test_send_text_to_pane_cmd_returns_the_expected_bash_cmd_string() {
170 assert_eq!(
171 send_text_to_pane_cmd("echo hi", 7),
172 "wezterm cli send-text echo hi --pane-id '7' --no-paste"
173 );
174 }
175
176 #[test]
177 fn test_submit_pane_cmd_returns_the_expected_bash_cmd_string() {
178 assert_eq!(
179 submit_pane_cmd(3),
180 "printf \"\\r\" | wezterm cli send-text --pane-id '3' --no-paste"
181 );
182 }
183
184 #[test]
185 fn test_activate_pane_cmd_returns_the_expected_bash_cmd_string() {
186 assert_eq!(activate_pane_cmd(9), "wezterm cli activate-pane --pane-id '9'");
187 }
188
189 #[test]
190 fn test_get_sibling_pane_with_titles_returns_the_expected_match_in_same_tab() {
191 let panes = vec![
192 pane_with(1, 10, "file:///host/home/user/project", "hx"),
193 pane_with(2, 10, "file:///host/home/user/project", "shell"),
194 pane_with(3, 11, "file:///host/home/user/other", "hx"),
195 ];
196 assert2::assert!(let Ok(sibling) = get_sibling_pane_with_titles(&panes, 2, &["hx"]));
197 assert_eq!(sibling.pane_id, 1);
198 }
199
200 #[test]
201 fn test_get_sibling_pane_with_titles_errors_when_no_title_match() {
202 let panes = vec![pane_with(1, 10, "file:///host/home/user/project", "shell")];
203 assert2::assert!(let Err(err) = get_sibling_pane_with_titles(&panes, 1, &["hx"]));
204 assert!(err.to_string().contains("error finding pane title in tab"));
205 }
206
207 #[test]
208 fn test_absolute_cwd_strips_file_uri_prefix() {
209 let pane = pane_with(1, 10, "file:///localhost/Users/alice/src", "hx");
210 let abs = pane.absolute_cwd();
211 assert!(abs.starts_with("/Users/alice/src"));
212 }
213
214 #[test]
215 fn test_is_sibling_terminal_pane_of_works_as_expected() {
216 let root = pane_with(1, 10, "file:///localhost/Users/alice/src", "hx");
217 let child = pane_with(2, 10, "file:///localhost/Users/alice/src/project", "shell");
218 let other_tab = pane_with(3, 11, "file:///localhost/Users/alice/src/project", "shell");
219 assert!(child.is_sibling_terminal_pane_of(&root));
220 assert!(!root.is_sibling_terminal_pane_of(&root));
221 assert!(!other_tab.is_sibling_terminal_pane_of(&root));
222 }
223
224 fn pane_with(id: i64, tab: i64, cwd: &str, title: &str) -> WeztermPane {
225 WeztermPane {
226 cursor_shape: "Block".into(),
227 cursor_visibility: "Visible".into(),
228 cursor_x: 0,
229 cursor_y: 0,
230 cwd: PathBuf::from(cwd),
231 is_active: false,
232 is_zoomed: false,
233 left_col: 0,
234 pane_id: id,
235 size: WeztermPaneSize {
236 cols: 80,
237 dpi: 96,
238 pixel_height: 800,
239 pixel_width: 600,
240 rows: 24,
241 },
242 tab_id: tab,
243 tab_title: "tab".into(),
244 title: title.into(),
245 top_row: 0,
246 tty_name: "tty".into(),
247 window_id: 1,
248 window_title: "win".into(),
249 workspace: "default".into(),
250 }
251 }
252}