be able to open an editor!

This commit is contained in:
Abraham Toriz 2021-07-20 22:08:07 -05:00
parent b5c383d3c3
commit 7bd7e2bcbb
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
3 changed files with 61 additions and 4 deletions

View File

@ -25,6 +25,7 @@ ansi_term = "0.12"
csv = "1.1"
regex = "1.5"
lazy_static = "1.4"
tempfile = "3"
[dev-dependencies]
pretty_assertions = "0.7.2"

View File

@ -1,6 +1,49 @@
use crate::error;
use crate::config::Config;
use std::process::{Command, Stdio};
use std::io::Read;
pub fn get_string(_config: &Config) -> error::Result<String> {
unimplemented!()
use tempfile::NamedTempFile;
use crate::error::{Error, Result};
pub fn get_string(note_editor: Option<&String>) -> Result<String> {
let note_editor = if let Some(note_editor) = note_editor {
note_editor
} else {
return Err(Error::EditorIsEmpty);
};
let parts: Vec<_> = note_editor.split(" ").filter(|p| p.len() > 0).collect();
let editor = if let Some(name) = parts.get(0) {
name.to_owned()
} else {
return Err(Error::EditorIsEmpty);
};
let mut c = Command::new(editor);
if parts.len() > 1 {
for part in &parts[1..] {
c.arg(part);
}
}
let mut tmpfile = NamedTempFile::new()?;
c.arg(tmpfile.as_ref());
let status = c
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()?
.status;
if status.success() {
let mut note = String::new();
tmpfile.read_to_string(&mut note)?;
Ok(note)
} else {
Err(Error::EditorFailed { editor: note_editor.clone(), error: status.to_string() })
}
}

View File

@ -96,6 +96,19 @@ good old SQL.")]
id: String,
col: String,
},
#[error("A note wasn't provided, and the config file specifies that the note
is required, but an empty string was found in the config value for 'note_editor'.
You can fix this by running t config and specifying a note editor. Check
t config --help for more options.")]
EditorIsEmpty,
#[error("Running editor '{editor}' failed with error: '{error}'")]
EditorFailed {
editor: String,
error: String,
}
}
pub type Result<T> = result::Result<T, Error>;