mirror of https://github.com/gbmor/clinte
check if $EDITOR contains flags before calling
If it contains flags, split by whitespace and pass them in as arguments to std::process::Command. Otherwise, just pass it in as normal. This was causing a panic/abort.
This commit is contained in:
parent
e5c197812e
commit
e63da0b7e0
|
@ -103,7 +103,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "clinte"
|
||||
version = "2.1.2"
|
||||
version = "2.1.3"
|
||||
dependencies = [
|
||||
"chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "clinte"
|
||||
version = "2.1.2"
|
||||
version = "2.1.3"
|
||||
authors = ["Ben Morrison <ben@gbmor.dev>"]
|
||||
edition = "2018"
|
||||
description = "CLI note posting system for shared UNIX boxes."
|
||||
|
|
45
src/ed.rs
45
src/ed.rs
|
@ -12,6 +12,8 @@ use crate::error;
|
|||
|
||||
use crate::user;
|
||||
|
||||
// Creates a temporary file to call $EDITOR on. Returns the
|
||||
// path to the file on success.
|
||||
fn create_tmp_file() -> Result<String, std::io::Error> {
|
||||
let the_time = Utc::now().to_rfc3339();
|
||||
let file_name = format!("/tmp/clinte_ed_{}_{}", *user::NAME, the_time);
|
||||
|
@ -21,6 +23,7 @@ fn create_tmp_file() -> Result<String, std::io::Error> {
|
|||
}
|
||||
}
|
||||
|
||||
// This calls $EDITOR and pre-fills it with the provided text.
|
||||
#[cfg(not(test))]
|
||||
pub fn call(body: &str) -> String {
|
||||
// If they don't have $EDITOR set, just default to nano
|
||||
|
@ -28,9 +31,9 @@ pub fn call(body: &str) -> String {
|
|||
let editor = match env::var("EDITOR") {
|
||||
Ok(ed) => {
|
||||
if &ed == "" {
|
||||
"nano".into()
|
||||
"nano".trim().to_string()
|
||||
} else {
|
||||
ed
|
||||
ed.trim().to_string()
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
|
@ -46,14 +49,36 @@ pub fn call(body: &str) -> String {
|
|||
"Couldn't populate tempfile with message",
|
||||
);
|
||||
|
||||
error::helper(
|
||||
process::Command::new(editor)
|
||||
.arg(&tmp_loc)
|
||||
.stdin(process::Stdio::inherit())
|
||||
.stdout(process::Stdio::inherit())
|
||||
.output(),
|
||||
"Couldn't call editor",
|
||||
);
|
||||
// Check if $EDITOR contains flags. Change the way we call $EDITOR if so,
|
||||
// because otherwise it will explode.
|
||||
if editor.contains(" ") {
|
||||
let ed_split = editor.split_whitespace().collect::<Vec<&str>>();
|
||||
let mut args = vec![];
|
||||
ed_split.iter().enumerate().for_each(|(i, e)| {
|
||||
if i == 0 {
|
||||
return;
|
||||
}
|
||||
args.push(e.to_string());
|
||||
});
|
||||
args.push(tmp_loc.clone());
|
||||
error::helper(
|
||||
process::Command::new(ed_split[0])
|
||||
.args(&args)
|
||||
.stdin(process::Stdio::inherit())
|
||||
.stdout(process::Stdio::inherit())
|
||||
.output(),
|
||||
"Couldn't call editor",
|
||||
);
|
||||
} else {
|
||||
error::helper(
|
||||
process::Command::new(editor)
|
||||
.arg(&tmp_loc)
|
||||
.stdin(process::Stdio::inherit())
|
||||
.stdout(process::Stdio::inherit())
|
||||
.output(),
|
||||
"Couldn't call editor",
|
||||
);
|
||||
}
|
||||
|
||||
let body = error::helper(
|
||||
fs::read_to_string(&tmp_loc),
|
||||
|
|
Loading…
Reference in New Issue