aboutsummaryrefslogtreecommitdiffstats
path: root/src/edit.rs
blob: cac8794246b2826bcf6d9afb9203b75caeab27d5 (plain) (blame)
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
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 editor = std::path::Path::new(&editor);

    let mut args = vec![];
    match editor.file_name() {
        Some(editor) => match editor.to_str() {
            Some("vim") => {
                // disable swap files and viminfo for password entry
                args.push(std::ffi::OsStr::new("-ni"));
                args.push(std::ffi::OsStr::new("NONE"));
            }
            _ => {
                // other editor support welcomed
            }
        },
        None => {
            return Err(Error::InvalidEditor {
                var: var.to_string(),
                editor: editor.as_os_str().to_os_string(),
            })
        }
    }

    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);

    args.push(file.as_os_str());
    let res = std::process::Command::new(&editor)
        .args(&args)
        .status()
        .unwrap();
    if !res.success() {
        return Err(Error::FailedToRunEditor {
            editor: editor.to_owned(),
            res,
        });
    }

    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)
}