1use core::str::FromStr;
7use std::process::Command;
8
9use rootcause::prelude::ResultExt;
10use rootcause::report;
11use ytil_editor::Editor;
12use ytil_hx::HxStatusLine;
13use ytil_sys::cli::Args;
14
15#[ytil_sys::main]
17fn main() -> rootcause::Result<()> {
18 let args = ytil_sys::cli::get();
19 if args.has_help() {
20 println!("{}", include_str!("../help.txt"));
21 return Ok(());
22 }
23
24 let hx_pane_id = ytil_wezterm::get_sibling_pane_with_titles(
25 &ytil_wezterm::get_all_panes(&[])?,
26 ytil_wezterm::get_current_pane_id()?,
27 Editor::Hx.pane_titles(),
28 )?
29 .pane_id;
30
31 let wezterm_pane_text = String::from_utf8(
32 Command::new("wezterm")
33 .args(["cli", "get-text", "--pane-id", &hx_pane_id.to_string()])
34 .output()?
35 .stdout,
36 )?;
37
38 let hx_status_line_str = wezterm_pane_text
39 .lines()
40 .nth_back(1)
41 .ok_or_else(|| report!("missing hx status line"))
42 .attach_with(|| format!("pane_id={hx_pane_id} text={wezterm_pane_text:#?}"))?;
43
44 let hx_status_line = HxStatusLine::from_str(hx_status_line_str)?;
45
46 ytil_sys::file::cp_to_system_clipboard(&mut format_hx_status_line(&hx_status_line)?.as_bytes())?;
47
48 Ok(())
49}
50
51fn format_hx_status_line(hx_status_line: &HxStatusLine) -> rootcause::Result<String> {
56 let file_path = hx_status_line
57 .file_path
58 .to_str()
59 .ok_or_else(|| report!("cannot convert path to str"))
60 .attach_with(|| format!("path={}", hx_status_line.file_path.display()))?;
61
62 Ok(format!("{file_path}:{}", hx_status_line.position.line))
63}
64
65#[cfg(test)]
66mod tests {
67 use std::path::PathBuf;
68
69 use rstest::rstest;
70
71 use super::*;
72
73 #[rstest]
74 #[case("src/main.rs", 42, 7, "src/main.rs:42")]
75 #[case("yog/ytil/cmd/src/lib.rs", 1, 1, "yog/ytil/cmd/src/lib.rs:1")]
76 #[case("file.txt", 100, 50, "file.txt:100")]
77 fn test_format_hx_status_line_returns_path_with_line_number(
78 #[case] path: &str,
79 #[case] line: usize,
80 #[case] column: usize,
81 #[case] expected: &str,
82 ) {
83 let status = HxStatusLine {
84 file_path: PathBuf::from(path),
85 position: ytil_hx::HxCursorPosition { line, column },
86 };
87 assert2::assert!(let Ok(result) = format_hx_status_line(&status));
88 pretty_assertions::assert_eq!(result, expected);
89 }
90}