use std::{
ffi::OsString,
process::{Command, Stdio},
};
use crate::profile::Profile;
const PANE_VARIABLE: &str = "HERDR_PANE_ID";
const SOURCE: &str = "ditto";
const HERDR: &str = "herdr";
pub fn pane() -> Option<String> {
pane_named(std::env::var_os(PANE_VARIABLE))
}
fn pane_named(value: Option<OsString>) -> Option<String> {
value
.and_then(|pane| pane.into_string().ok())
.filter(|pane| !pane.is_empty())
}
pub fn report_profile(profile: &Profile) {
let Some(pane) = pane() else { return };
let _ = report_command(&pane, profile)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status();
}
fn report_command(pane: &str, profile: &Profile) -> Command {
let mut command = Command::new(HERDR);
command
.arg("pane")
.arg("report-metadata")
.arg(pane)
.arg("--source")
.arg(SOURCE)
.arg("--token")
.arg(format!("profile={}", profile.name));
command
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::*;
use crate::profile::OpencodeHome;
fn profile() -> Profile {
Profile {
name: "work".to_owned(),
claude_home: PathBuf::from("/profiles/work/claude"),
codex_home: PathBuf::from("/profiles/work/codex"),
fx_home: PathBuf::from("/profiles/work/fx-home"),
omp_home: PathBuf::from("/omp/profiles/work/agent"),
opencode: OpencodeHome {
data: PathBuf::from("/profiles/work/opencode/data"),
config: PathBuf::from("/profiles/work/opencode/config"),
state: PathBuf::from("/profiles/work/opencode/state"),
},
pi_home: PathBuf::from("/profiles/work/pi"),
prime_agent_home: PathBuf::from("/profiles/work/prime-agent"),
generic: Vec::new(),
managed: true,
}
}
#[test]
fn a_pane_is_only_a_pane_when_herdr_named_one() {
assert_eq!(pane_named(None), None);
assert_eq!(pane_named(Some(OsString::from(""))), None);
assert_eq!(
pane_named(Some(OsString::from("w1B:p3"))),
Some("w1B:p3".to_owned())
);
}
#[test]
fn the_pane_comes_before_the_options_herdr_parses() {
let command = report_command("w1B:p3", &profile());
let arguments: Vec<_> = command.get_args().collect();
assert_eq!(
arguments,
[
"pane",
"report-metadata",
"w1B:p3",
"--source",
"ditto",
"--token",
"profile=work",
]
);
}
}