forked from doy/rbw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.rs
86 lines (76 loc) · 2.65 KB
/
edit.rs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use crate::prelude::*;
use std::io::{Read as _, Write as _};
pub fn edit(contents: &str, help: &str) -> Result<String> {
let mut var = "VISUAL";
let editor = std::env::var_os(var).unwrap_or_else(|| {
var = "EDITOR";
std::env::var_os(var).unwrap_or_else(|| "/usr/bin/vim".into())
});
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join("rbw");
let mut fh = std::fs::File::create(&file).unwrap();
fh.write_all(contents.as_bytes()).unwrap();
fh.write_all(help.as_bytes()).unwrap();
drop(fh);
let (cmd, args) = if contains_shell_metacharacters(&editor) {
let mut cmdline = std::ffi::OsString::new();
cmdline.extend([
editor.as_ref(),
std::ffi::OsStr::new(" "),
file.as_os_str(),
]);
let editor_args = vec![std::ffi::OsString::from("-c"), cmdline];
(std::path::Path::new("/bin/sh"), editor_args)
} else {
let editor = std::path::Path::new(&editor);
let mut editor_args = vec![];
#[allow(clippy::single_match)] // more to come
match editor.file_name() {
Some(editor) => match editor.to_str() {
Some("vim" | "nvim") => {
// disable swap files and viminfo for password entry
editor_args.push(std::ffi::OsString::from("-ni"));
editor_args.push(std::ffi::OsString::from("NONE"));
}
_ => {
// other editor support welcomed
}
},
None => {
return Err(Error::InvalidEditor {
var: var.to_string(),
editor: editor.as_os_str().to_os_string(),
})
}
}
editor_args.push(file.clone().into_os_string());
(editor, editor_args)
};
let res = std::process::Command::new(cmd).args(&args).status();
match res {
Ok(res) => {
if !res.success() {
return Err(Error::FailedToRunEditor {
editor: cmd.to_owned(),
args,
res,
});
}
}
Err(err) => {
return Err(Error::FailedToFindEditor {
editor: cmd.to_owned(),
err,
})
}
}
let mut fh = std::fs::File::open(&file).unwrap();
let mut contents = String::new();
fh.read_to_string(&mut contents).unwrap();
drop(fh);
Ok(contents)
}
fn contains_shell_metacharacters(cmd: &std::ffi::OsStr) -> bool {
cmd.to_str()
.map_or(false, |s| s.contains(&[' ', '$', '\'', '"'][..]))
}