mirror of
https://github.com/pimalaya/himalaya.git
synced 2026-06-17 13:17:55 +08:00
split input into submodules
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use log::debug;
|
||||
use std::io::{self, Write};
|
||||
|
||||
pub enum PreEditChoice {
|
||||
Edit,
|
||||
Discard,
|
||||
Quit,
|
||||
}
|
||||
|
||||
pub fn pre_edit() -> Result<PreEditChoice> {
|
||||
println!("A draft was found:");
|
||||
print!("(e)dit, (d)iscard or (q)uit? ");
|
||||
io::stdout().flush().context("cannot flush stdout")?;
|
||||
|
||||
let mut buf = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut buf)
|
||||
.context("cannot read stdin")?;
|
||||
|
||||
match buf.bytes().next().map(|bytes| bytes as char) {
|
||||
Some('e') => {
|
||||
debug!("edit choice matched");
|
||||
Ok(PreEditChoice::Edit)
|
||||
}
|
||||
Some('d') => {
|
||||
debug!("discard choice matched");
|
||||
Ok(PreEditChoice::Discard)
|
||||
}
|
||||
Some('q') => {
|
||||
debug!("quit choice matched");
|
||||
Ok(PreEditChoice::Quit)
|
||||
}
|
||||
Some(choice) => {
|
||||
debug!("invalid choice `{}`", choice);
|
||||
Err(anyhow!("invalid choice `{}`", choice))
|
||||
}
|
||||
None => {
|
||||
debug!("empty choice");
|
||||
Err(anyhow!("empty choice"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum PostEditChoice {
|
||||
Send,
|
||||
Edit,
|
||||
LocalDraft,
|
||||
RemoteDraft,
|
||||
Discard,
|
||||
}
|
||||
|
||||
pub fn post_edit() -> Result<PostEditChoice> {
|
||||
print!("(s)end, (e)dit, (l)ocal/(r)emote draft or (d)iscard? ");
|
||||
io::stdout().flush().context("cannot flush stdout")?;
|
||||
|
||||
let mut buf = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut buf)
|
||||
.context("cannot read stdin")?;
|
||||
|
||||
match buf.bytes().next().map(|bytes| bytes as char) {
|
||||
Some('s') => {
|
||||
debug!("send choice matched");
|
||||
Ok(PostEditChoice::Send)
|
||||
}
|
||||
Some('l') => {
|
||||
debug!("save local draft choice matched");
|
||||
Ok(PostEditChoice::LocalDraft)
|
||||
}
|
||||
Some('r') => {
|
||||
debug!("save remote draft matched");
|
||||
Ok(PostEditChoice::RemoteDraft)
|
||||
}
|
||||
Some('e') => {
|
||||
debug!("edit choice matched");
|
||||
Ok(PostEditChoice::Edit)
|
||||
}
|
||||
Some('d') => {
|
||||
debug!("discard choice matched");
|
||||
Ok(PostEditChoice::Discard)
|
||||
}
|
||||
Some(choice) => {
|
||||
debug!("invalid choice `{}`", choice);
|
||||
Err(anyhow!("invalid choice `{}`", choice))
|
||||
}
|
||||
None => {
|
||||
debug!("empty choice");
|
||||
Err(anyhow!("empty choice"))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use log::{debug, error};
|
||||
use std::{
|
||||
env,
|
||||
fs::File,
|
||||
io::{Read, Write},
|
||||
process::Command,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
domain::msg,
|
||||
ui::choice::{self, PreEditChoice},
|
||||
};
|
||||
|
||||
pub fn open_editor_with_tpl(tpl: &[u8]) -> Result<String> {
|
||||
let path = msg::utils::draft_path();
|
||||
if path.exists() {
|
||||
debug!("draft found");
|
||||
loop {
|
||||
match choice::pre_edit() {
|
||||
Ok(choice) => match choice {
|
||||
PreEditChoice::Edit => return open_editor_with_draft(),
|
||||
PreEditChoice::Discard => break,
|
||||
PreEditChoice::Quit => return Err(anyhow!("edition aborted")),
|
||||
},
|
||||
Err(err) => error!("{}", err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
debug!("create draft");
|
||||
File::create(&path)
|
||||
.context(format!("cannot create draft file `{:?}`", path))?
|
||||
.write(tpl)
|
||||
.context(format!("cannot write draft file `{:?}`", path))?;
|
||||
|
||||
debug!("open editor");
|
||||
Command::new(env::var("EDITOR").context("cannot find `$EDITOR` env var")?)
|
||||
.arg(&path)
|
||||
.status()
|
||||
.context("cannot launch editor")?;
|
||||
|
||||
debug!("read draft");
|
||||
let mut draft = String::new();
|
||||
File::open(&path)
|
||||
.context(format!("cannot open draft file `{:?}`", path))?
|
||||
.read_to_string(&mut draft)
|
||||
.context(format!("cannot read draft file `{:?}`", path))?;
|
||||
|
||||
Ok(draft)
|
||||
}
|
||||
|
||||
pub fn open_editor_with_draft() -> Result<String> {
|
||||
let path = msg::utils::draft_path();
|
||||
|
||||
// Opens editor and saves user input to draft file
|
||||
Command::new(env::var("EDITOR").context("cannot find `EDITOR` env var")?)
|
||||
.arg(&path)
|
||||
.status()
|
||||
.context("cannot launch editor")?;
|
||||
|
||||
// Extracts draft file content
|
||||
let mut draft = String::new();
|
||||
File::open(&path)
|
||||
.context(format!("cannot open file `{:?}`", path))?
|
||||
.read_to_string(&mut draft)
|
||||
.context(format!("cannot read file `{:?}`", path))?;
|
||||
|
||||
Ok(draft)
|
||||
}
|
||||
+3
-1
@@ -1,3 +1,5 @@
|
||||
//! Modules related to User Interface.
|
||||
//! Module related to User Interface.
|
||||
|
||||
pub mod choice;
|
||||
pub mod editor;
|
||||
pub mod table;
|
||||
|
||||
Reference in New Issue
Block a user