mirror of
https://github.com/pimalaya/himalaya.git
synced 2026-06-17 13:17:55 +08:00
improve choice after editing msg
This commit is contained in:
+41
-12
@@ -1,7 +1,7 @@
|
||||
use error_chain::error_chain;
|
||||
use std::{
|
||||
env,
|
||||
fs::{remove_file, File},
|
||||
fs::File,
|
||||
io::{self, Read, Write},
|
||||
process::Command,
|
||||
};
|
||||
@@ -29,23 +29,52 @@ pub fn open_editor_with_tpl(tpl: &[u8]) -> Result<String> {
|
||||
.chain_err(|| format!("Cannot open file `{}`", draft_path.to_string_lossy()))?
|
||||
.read_to_string(&mut draft)
|
||||
.chain_err(|| format!("Cannot read file `{}`", draft_path.to_string_lossy()))?;
|
||||
remove_file(&draft_path)
|
||||
.chain_err(|| format!("Cannot remove file `{}`", draft_path.to_string_lossy()))?;
|
||||
|
||||
Ok(draft)
|
||||
}
|
||||
|
||||
pub fn ask_for_confirmation(prompt: &str) -> Result<()> {
|
||||
print!("{} (y/n) ", prompt);
|
||||
pub fn open_editor_with_draft() -> Result<String> {
|
||||
// Creates draft file
|
||||
let mut draft_path = env::temp_dir();
|
||||
draft_path.push("himalaya-draft.mail");
|
||||
|
||||
// Opens editor and saves user input to draft file
|
||||
Command::new(env::var("EDITOR").chain_err(|| "Cannot find `EDITOR` env var")?)
|
||||
.arg(&draft_path)
|
||||
.status()
|
||||
.chain_err(|| "Cannot start editor")?;
|
||||
|
||||
// Extracts draft file content
|
||||
let mut draft = String::new();
|
||||
File::open(&draft_path)
|
||||
.chain_err(|| format!("Cannot open file `{}`", draft_path.to_string_lossy()))?
|
||||
.read_to_string(&mut draft)
|
||||
.chain_err(|| format!("Cannot read file `{}`", draft_path.to_string_lossy()))?;
|
||||
|
||||
Ok(draft)
|
||||
}
|
||||
|
||||
pub enum Choice {
|
||||
Send,
|
||||
Draft,
|
||||
Edit,
|
||||
Quit,
|
||||
}
|
||||
|
||||
pub fn post_edit_choice() -> Result<Choice> {
|
||||
print!("(s)end, (d)raft, (e)dit or (q)uit? ");
|
||||
io::stdout().flush().chain_err(|| "Cannot flush stdout")?;
|
||||
|
||||
match io::stdin()
|
||||
.bytes()
|
||||
.next()
|
||||
.and_then(|res| res.ok())
|
||||
.map(|bytes| bytes as char)
|
||||
{
|
||||
Some('y') | Some('Y') => Ok(()),
|
||||
let mut buf = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut buf)
|
||||
.chain_err(|| "Cannot read stdin")?;
|
||||
|
||||
match buf.bytes().next().map(|bytes| bytes as char) {
|
||||
Some('s') => Ok(Choice::Send),
|
||||
Some('d') => Ok(Choice::Draft),
|
||||
Some('e') => Ok(Choice::Edit),
|
||||
Some('q') => Ok(Choice::Quit),
|
||||
Some(choice) => Err(format!("Invalid choice `{}`", choice).into()),
|
||||
None => Err("Empty choice".into()),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user