use std::io::Write; use std::process::Command; use anyhow::Context; use serde::Deserialize; use crate::config::CommandSpec; use crate::revisions::jj; pub fn edit(ext: &str, content: &str) -> anyhow::Result { let argv = editor().context("invalid ui.editor")?; let mut file = tempfile::Builder::new().suffix(ext).tempfile()?; file.write_all(content.as_bytes())?; file.flush()?; let status = Command::new(&argv[0]).args(&argv[1..]).arg(file.path()).status()?; anyhow::ensure!(status.success(), "editor exited with {status}"); Ok(std::fs::read_to_string(file.path())?) } fn editor() -> anyhow::Result> { let raw = jj(&["config", "get", "ui.editor"])?; let raw = raw.trim_end(); let spec = if raw.starts_with('[') { CommandSpec::deserialize(toml::de::ValueDeserializer::new(raw))? } else { CommandSpec::String(raw.into()) }; spec.argv() }