forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.rs
More file actions
39 lines (31 loc) · 1017 Bytes
/
Copy patheditor.rs
File metadata and controls
39 lines (31 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
extern crate tempfile;
use self::tempfile::NamedTempFile;
use std::error::Error;
use std::ffi::OsString;
use std::io::prelude::*;
use std::process::Command;
pub fn edit_string(s: &str) -> Result<String, Box<Error>> {
let mut file = NamedTempFile::new()?;
file.write_all(s.as_bytes())?;
editor_output(&mut file)
}
fn editor_output(file: &mut NamedTempFile) -> Result<String, Box<Error>> {
use std::io::SeekFrom::Start;
let status = Command::new(editor_command()?)
.arg(file.path().as_os_str())
.spawn()?
.wait()?;
if !status.success() {
return Err("Editor did not exit successfully. Aborting".into());
}
let mut buffer = String::new();
file.seek(Start(0))?;
file.read_to_string(&mut buffer)?;
Ok(buffer)
}
fn editor_command() -> Result<OsString, Box<Error>> {
use std::env;
env::var_os("VISUAL")
.or_else(|| env::var_os("EDITOR"))
.ok_or_else(|| "Either $VISUAL or $EDITOR must be set to edit files".into())
}