Skip to content

Commit

Permalink
improve pre and post edit choices interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod committed Feb 4, 2024
1 parent 35c1453 commit dd7e1a0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 72 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### 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:

```toml
Expand Down Expand Up @@ -907,6 +908,7 @@ Few major concepts changed:
[sourcehut#41]: https://todo.sr.ht/~soywod/pimalaya/41
[sourcehut#43]: https://todo.sr.ht/~soywod/pimalaya/43
[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#60]: https://todo.sr.ht/~soywod/pimalaya/60
[sourcehut#95]: https://todo.sr.ht/~soywod/pimalaya/95
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "himalaya"
description = "CLI to manage emails"
version = "1.0.0-beta.2"
version = "1.0.0-beta.3"
authors = ["soywod <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
126 changes: 56 additions & 70 deletions src/ui/choice.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,42 @@
use anyhow::{anyhow, bail, Context, Result};
use log::{debug, error};
use std::io::{self, Write};
use anyhow::Result;
use dialoguer::Select;

use super::THEME;

#[derive(Clone, Debug)]
pub enum PreEditChoice {
Edit,
Discard,
Quit,
}

impl ToString for PreEditChoice {
fn to_string(&self) -> String {
match self {
Self::Edit => "Edit it".into(),
Self::Discard => "Discard it".into(),
Self::Quit => "Quit".into(),
}
}
}

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 choices = [
PreEditChoice::Edit,
PreEditChoice::Discard,
PreEditChoice::Quit,
];

let mut buf = String::new();
io::stdin()
.read_line(&mut buf)
.context("cannot read stdin")?;
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()?;

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"))
}
}
Ok(choices[choice_idx].clone())
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PostEditChoice {
#[cfg(feature = "message-send")]
Send,
Expand All @@ -52,45 +47,36 @@ pub enum PostEditChoice {
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")?;
impl ToString for PostEditChoice {
fn to_string(&self) -> String {
match self {
#[cfg(feature = "message-send")]
Self::Send => "Send it".into(),
Self::Edit => "Edit it again".into(),
Self::LocalDraft => "Save it as local draft".into(),
#[cfg(feature = "message-add")]
Self::RemoteDraft => "Save it as remote draft".into(),
Self::Discard => "Discard it".into(),
}
}
}

match buf.bytes().next().map(|bytes| bytes as char) {
pub fn post_edit() -> Result<PostEditChoice> {
let choices = [
#[cfg(feature = "message-send")]
Some('s') => {
debug!("send choice matched");
Ok(PostEditChoice::Send)
}
Some('l') => {
debug!("save local draft choice matched");
Ok(PostEditChoice::LocalDraft)
}
PostEditChoice::Send,
PostEditChoice::Edit,
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");
}
}
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())
}

0 comments on commit dd7e1a0

Please sign in to comment.