improve pre and post edit choices interaction

This commit is contained in:
Clément DOUIN 2024-02-04 12:13:14 +01:00
parent 35c1453863
commit dd7e1a02be
No known key found for this signature in database
GPG key ID: 353E4A18EE0FAB72
4 changed files with 62 additions and 74 deletions

View file

@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### Changed
- Improved pre and post edit choices interaction. [sourcehut#58]
- Changed `envelope.watch.{event}.{hook}`: hooks can now be cumulated. For example it is possible to send a system notification and execute a shell command when receiving a new envelope: - Changed `envelope.watch.{event}.{hook}`: hooks can now be cumulated. For example it is possible to send a system notification and execute a shell command when receiving a new envelope:
```toml ```toml
@ -907,6 +908,7 @@ Few major concepts changed:
[sourcehut#41]: https://todo.sr.ht/~soywod/pimalaya/41 [sourcehut#41]: https://todo.sr.ht/~soywod/pimalaya/41
[sourcehut#43]: https://todo.sr.ht/~soywod/pimalaya/43 [sourcehut#43]: https://todo.sr.ht/~soywod/pimalaya/43
[sourcehut#54]: https://todo.sr.ht/~soywod/pimalaya/54 [sourcehut#54]: https://todo.sr.ht/~soywod/pimalaya/54
[sourcehut#58]: https://todo.sr.ht/~soywod/pimalaya/58
[sourcehut#59]: https://todo.sr.ht/~soywod/pimalaya/59 [sourcehut#59]: https://todo.sr.ht/~soywod/pimalaya/59
[sourcehut#60]: https://todo.sr.ht/~soywod/pimalaya/60 [sourcehut#60]: https://todo.sr.ht/~soywod/pimalaya/60
[sourcehut#95]: https://todo.sr.ht/~soywod/pimalaya/95 [sourcehut#95]: https://todo.sr.ht/~soywod/pimalaya/95

2
Cargo.lock generated
View file

@ -1840,7 +1840,7 @@ dependencies = [
[[package]] [[package]]
name = "himalaya" name = "himalaya"
version = "1.0.0-beta.2" version = "1.0.0-beta.3"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",

View file

@ -1,7 +1,7 @@
[package] [package]
name = "himalaya" name = "himalaya"
description = "CLI to manage emails" description = "CLI to manage emails"
version = "1.0.0-beta.2" version = "1.0.0-beta.3"
authors = ["soywod <clement.douin@posteo.net>"] authors = ["soywod <clement.douin@posteo.net>"]
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"

View file

@ -1,47 +1,42 @@
use anyhow::{anyhow, bail, Context, Result}; use anyhow::Result;
use log::{debug, error}; use dialoguer::Select;
use std::io::{self, Write};
use super::THEME;
#[derive(Clone, Debug)]
pub enum PreEditChoice { pub enum PreEditChoice {
Edit, Edit,
Discard, Discard,
Quit, Quit,
} }
pub fn pre_edit() -> Result<PreEditChoice> { impl ToString for PreEditChoice {
println!("A draft was found:"); fn to_string(&self) -> String {
print!("(e)dit, (d)iscard or (q)uit? "); match self {
io::stdout().flush().context("cannot flush stdout")?; Self::Edit => "Edit it".into(),
Self::Discard => "Discard it".into(),
let mut buf = String::new(); Self::Quit => "Quit".into(),
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) => {
error!(r#"invalid choice "{}""#, choice);
Err(anyhow!(r#"invalid choice "{}""#, choice))
}
None => {
error!("empty choice");
Err(anyhow!("empty choice"))
} }
} }
} }
pub fn pre_edit() -> Result<PreEditChoice> {
let choices = [
PreEditChoice::Edit,
PreEditChoice::Discard,
PreEditChoice::Quit,
];
let choice_idx = Select::with_theme(&*THEME)
.with_prompt("A draft was found, what would you like to do with it?")
.items(&choices)
.default(0)
.interact()?;
Ok(choices[choice_idx].clone())
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PostEditChoice { pub enum PostEditChoice {
#[cfg(feature = "message-send")] #[cfg(feature = "message-send")]
Send, Send,
@ -52,45 +47,36 @@ pub enum PostEditChoice {
Discard, Discard,
} }
pub fn post_edit() -> Result<PostEditChoice> { impl ToString for PostEditChoice {
print!("(s)end, (e)dit, (l)ocal/(r)emote draft or (d)iscard? "); fn to_string(&self) -> String {
io::stdout().flush().context("cannot flush stdout")?; match self {
#[cfg(feature = "message-send")]
let mut buf = String::new(); Self::Send => "Send it".into(),
io::stdin() Self::Edit => "Edit it again".into(),
.read_line(&mut buf) Self::LocalDraft => "Save it as local draft".into(),
.context("cannot read stdin")?; #[cfg(feature = "message-add")]
Self::RemoteDraft => "Save it as remote draft".into(),
match buf.bytes().next().map(|bytes| bytes as char) { Self::Discard => "Discard it".into(),
#[cfg(feature = "message-send")]
Some('s') => {
debug!("send choice matched");
Ok(PostEditChoice::Send)
}
Some('l') => {
debug!("save local draft choice matched");
Ok(PostEditChoice::LocalDraft)
}
#[cfg(feature = "message-add")]
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) => {
error!(r#"invalid choice "{}""#, choice);
bail!("invalid choice {choice}");
}
None => {
error!("empty choice");
bail!("empty choice");
} }
} }
} }
pub fn post_edit() -> Result<PostEditChoice> {
let choices = [
#[cfg(feature = "message-send")]
PostEditChoice::Send,
PostEditChoice::Edit,
PostEditChoice::LocalDraft,
#[cfg(feature = "message-add")]
PostEditChoice::RemoteDraft,
PostEditChoice::Discard,
];
let choice_idx = Select::with_theme(&*THEME)
.with_prompt("What would you like to do with this message?")
.items(&choices)
.default(0)
.interact()?;
Ok(choices[choice_idx].clone())
}