use super::*;
pub fn editor(question: &str, initial: &str) -> Result<String> {
if scripted() {
return Err(Error::NotATerminal);
}
need_terminal()?;
let command = editor_command(std::env::var("VISUAL").ok(), std::env::var("EDITOR").ok());
edit(question, initial, &command)
}
pub(super) fn editor_command(visual: Option<String>, editor: Option<String>) -> String {
let set = |e: &Option<String>| e.as_ref().is_some_and(|e| !e.trim().is_empty());
let chosen = [visual, editor].into_iter().find(set).flatten();
chosen.unwrap_or_else(|| if cfg!(windows) { "notepad" } else { "vi" }.into())
}
struct Scratch(std::path::PathBuf);
impl Scratch {
fn new(text: &str) -> io::Result<Scratch> {
let mut options = std::fs::OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
let mut tries = 0;
loop {
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.subsec_nanos());
let name = format!("tinsel-{}-{nanos}-{tries}.md", std::process::id());
let path = std::env::temp_dir().join(name);
match options.open(&path) {
Ok(mut file) => {
let scratch = Scratch(path);
file.write_all(text.as_bytes())?;
return Ok(scratch);
}
Err(e) if e.kind() == io::ErrorKind::AlreadyExists && tries < 100 => tries += 1,
Err(e) => return Err(e),
}
}
}
}
impl Drop for Scratch {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.0);
}
}
fn editor_process(command: &str, path: &std::path::Path) -> std::process::Command {
#[cfg(unix)]
{
let mut run = std::process::Command::new("sh");
run.arg("-c").arg(format!("{command} \"$@\"")).arg(command).arg(path);
run
}
#[cfg(not(unix))]
{
let mut words = command.split_whitespace();
let mut run = std::process::Command::new(words.next().unwrap_or("notepad"));
run.args(words).arg(path);
run
}
}
pub(super) fn edit(question: &str, initial: &str, command: &str) -> Result<String> {
let program = command.split_whitespace().next().unwrap_or("vi");
let file = Scratch::new(initial)?;
let _paused = crate::display::pause_all();
let mut frame = Frame::default();
frame.draw(&[format!(
"{} {}",
ask(question),
paint(MUTED, &format!("opening {program}…"))
)]);
let status = editor_process(command, &file.0).status();
let text = std::fs::read_to_string(&file.0);
drop(file);
let couldnt_open = |frame: &mut Frame| {
frame.draw(&[format!(
"{} {}",
ask(question),
paint(RED, &format!("couldn't open {program}"))
)]);
};
match status {
Ok(s) if s.success() => {}
Ok(s) if cfg!(unix) && matches!(s.code(), Some(126 | 127)) => {
couldnt_open(&mut frame);
return Err(Error::Io(io::Error::new(
io::ErrorKind::NotFound,
format!("couldn't run the editor “{command}”"),
)));
}
Ok(_) => return Err(cancel(&mut frame, question)),
Err(e) => {
couldnt_open(&mut frame);
return Err(Error::Io(e));
}
}
let text = text?;
let lines = text.lines().filter(|l| !l.trim().is_empty()).count();
frame.draw(&[answered(question, &crate::format::plural(lines as u64, "line"))]);
Ok(text)
}