1use core::str::FromStr;
7use std::process::Command;
8
9use rootcause::prelude::ResultExt as _;
10use rootcause::report;
11use ytil_editor::Editor;
12use ytil_hx::HxStatusLine;
13use ytil_sys::cli::Args;
14
15fn format_hx_status_line(hx_status_line: &HxStatusLine) -> rootcause::Result<String> {
20 let file_path = hx_status_line
21 .file_path
22 .to_str()
23 .ok_or_else(|| report!("cannot convert path to str"))
24 .attach_with(|| format!("path={}", hx_status_line.file_path.display()))?;
25
26 Ok(format!("{file_path}:{}", hx_status_line.position.line))
27}
28
29#[ytil_sys::main]
31fn main() -> rootcause::Result<()> {
32 let args = ytil_sys::cli::get();
33 if args.has_help() {
34 println!("{}", include_str!("../help.txt"));
35 return Ok(());
36 }
37
38 let hx_pane_id = ytil_wezterm::get_sibling_pane_with_titles(
39 &ytil_wezterm::get_all_panes(&[])?,
40 ytil_wezterm::get_current_pane_id()?,
41 Editor::Hx.pane_titles(),
42 )?
43 .pane_id;
44
45 let wezterm_pane_text = String::from_utf8(
46 Command::new("wezterm")
47 .args(["cli", "get-text", "--pane-id", &hx_pane_id.to_string()])
48 .output()?
49 .stdout,
50 )?;
51
52 let hx_status_line_str = wezterm_pane_text
53 .lines()
54 .nth_back(1)
55 .ok_or_else(|| report!("missing hx status line"))
56 .attach_with(|| format!("pane_id={hx_pane_id} text={wezterm_pane_text:#?}"))?;
57
58 let hx_status_line = HxStatusLine::from_str(hx_status_line_str)?;
59
60 ytil_sys::file::cp_to_system_clipboard(&mut format_hx_status_line(&hx_status_line)?.as_bytes())?;
61
62 Ok(())
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}