yhfp/
main.rs

1//! Copy current Helix file path with line number to clipboard.
2//!
3//! # Usage
4//! ```bash
5//! yhfp # copies /absolute/path/to/file.rs:123 to clipboard
6//! ```
7//!
8//! # Errors
9//! - `WezTerm` command execution fails or exits non-zero.
10//! - Status line missing or cannot be parsed.
11//! - Invalid UTF-8 in process output.
12#![feature(exit_status_error)]
13
14use core::str::FromStr;
15use std::process::Command;
16
17use color_eyre::eyre::eyre;
18use ytil_editor::Editor;
19use ytil_hx::HxStatusLine;
20use ytil_sys::cli::Args;
21
22/// Formats Helix status line into file path with line number.
23///
24/// # Errors
25/// - UTF-8 conversion fails.
26fn format_hx_status_line(hx_status_line: &HxStatusLine) -> color_eyre::Result<String> {
27    let file_path = hx_status_line
28        .file_path
29        .to_str()
30        .ok_or_else(|| eyre!("cannot convert path to str | path={:#?}", hx_status_line.file_path))?;
31
32    Ok(format!("{file_path}:{}", hx_status_line.position.line))
33}
34
35/// Copy current Helix file path with line number to clipboard.
36fn main() -> color_eyre::Result<()> {
37    color_eyre::install()?;
38
39    let args = ytil_sys::cli::get();
40    if args.has_help() {
41        println!("{}", include_str!("../help.txt"));
42        return Ok(());
43    }
44
45    let hx_pane_id = ytil_wezterm::get_sibling_pane_with_titles(
46        &ytil_wezterm::get_all_panes(&[])?,
47        ytil_wezterm::get_current_pane_id()?,
48        Editor::Hx.pane_titles(),
49    )?
50    .pane_id;
51
52    let wezterm_pane_text = String::from_utf8(
53        Command::new("wezterm")
54            .args(["cli", "get-text", "--pane-id", &hx_pane_id.to_string()])
55            .output()?
56            .stdout,
57    )?;
58
59    let hx_status_line_str = wezterm_pane_text
60        .lines()
61        .nth_back(1)
62        .ok_or_else(|| eyre!("missing hx status line | pane_id={hx_pane_id} text={wezterm_pane_text:#?}"))?;
63
64    let hx_status_line = HxStatusLine::from_str(hx_status_line_str)?;
65
66    ytil_sys::file::cp_to_system_clipboard(&mut format_hx_status_line(&hx_status_line)?.as_bytes())?;
67
68    Ok(())
69}