diff options
Diffstat (limited to 'src/editor.rs')
| -rw-r--r-- | src/editor.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/editor.rs b/src/editor.rs new file mode 100644 index 0000000..3207fb9 --- /dev/null +++ b/src/editor.rs @@ -0,0 +1,26 @@ +use anyhow::{Context, Result, ensure}; +use std::{io::Write, process::Command}; + +pub fn open(lines: &[String]) -> Result<()> { + let editor = std::env::var("EDITOR").unwrap_or_else(|_| "vi".into()); + let args = shell_words::split(&editor).context("Invalid quoting in EDITOR")?; + ensure!( + !args.is_empty(), + "EDITOR is empty; configure an editor command" + ); + let mut snapshot = tempfile::Builder::new() + .prefix("roentgen-logs-") + .suffix(".txt") + .tempfile()?; + for line in lines { + writeln!(snapshot, "{}", crate::model::display_text(line))?; + } + snapshot.flush()?; + let status = Command::new(&args[0]) + .args(&args[1..]) + .arg(snapshot.path()) + .status() + .context("Could not launch EDITOR")?; + ensure!(status.success(), "Editor exited unsuccessfully"); + Ok(()) +} |