be able to open an editor!
This commit is contained in:
parent
b5c383d3c3
commit
7bd7e2bcbb
|
@ -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"
|
||||
|
|
|
@ -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() })
|
||||
}
|
||||
}
|
||||
|
|
13
src/error.rs
13
src/error.rs
|
@ -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>;
|
||||
|
|
Loading…
Reference in New Issue