Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react generalise #330

Merged
merged 3 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ ctrl-p = "select_channel_modal previous"
[keybindings.message_selected]
alt-y = ""
alt-w = "copy_message selected"
ctrl-t = "react :thumbsup:"
ctrl-h = "react ❤️"
```

## License
Expand Down
22 changes: 13 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ impl App {
Command::ToggleMultiline => {
self.is_multiline_input = !self.is_multiline_input;
}
Command::React => {
Command::React(reaction) => {
if let Some(idx) = self.channels.state.selected() {
self.add_reaction(idx).await;
self.add_reaction(idx, reaction).await;
}
}
Command::OpenUrl => {
Expand Down Expand Up @@ -436,8 +436,12 @@ impl App {
}
}

pub async fn add_reaction(&mut self, channel_idx: usize) -> Option<()> {
let reaction = self.take_reaction()?;
pub async fn add_reaction(
&mut self,
channel_idx: usize,
reaction: Option<String>,
) -> Option<()> {
let reaction = reaction.or_else(|| self.take_reaction()?);
let channel = self.storage.channel(self.channels.items[channel_idx])?;
let message = self.selected_message()?;
let remove = reaction.is_none();
Expand Down Expand Up @@ -1648,7 +1652,7 @@ impl HandleReactionOptions {
}

/// Returns an emoji string if `s` is an emoji or if `s` is a GitHub emoji shortcode.
fn to_emoji(s: &str) -> Option<&str> {
pub fn to_emoji(s: &str) -> Option<&str> {
let s = s.trim();
if emojis::get(s).is_some() {
Some(s)
Expand Down Expand Up @@ -1834,7 +1838,7 @@ pub(crate) mod tests {
.select(Some(0));

app.get_input().put_char('👍');
app.add_reaction(0).await;
app.add_reaction(0, None).await;

let arrived_at = app.messages[&channel_id].items[0];
let reactions = &app
Expand All @@ -1860,7 +1864,7 @@ pub(crate) mod tests {
for c in ":thumbsup:".chars() {
app.get_input().put_char(c);
}
app.add_reaction(0).await;
app.add_reaction(0, None).await;

let arrived_at = app.messages[&channel_id].items[0];
let reactions = &app
Expand Down Expand Up @@ -1891,7 +1895,7 @@ pub(crate) mod tests {
.into_owned();
message.reactions.push((app.user_id, "👍".to_string()));
app.storage.store_message(channel_id, message);
app.add_reaction(0).await;
app.add_reaction(0, None).await;

let reactions = &app
.storage
Expand All @@ -1914,7 +1918,7 @@ pub(crate) mod tests {
for c in ":thumbsup".chars() {
app.get_input().put_char(c);
}
app.add_reaction(0).await;
app.add_reaction(0, None).await;

assert_eq!(app.get_input().data, ":thumbsup");
let arrived_at = app.messages[&channel_id].items[0];
Expand Down
17 changes: 16 additions & 1 deletion src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crokey::KeyCombination;
use serde::{Deserialize, Serialize};
use strum::{EnumIter, EnumProperty, EnumString, VariantNames};

use crate::app::to_emoji;

pub type KeybindingConfig = HashMap<KeyCombination, String>;
pub type ModeKeybindingConfig = HashMap<WindowMode, KeybindingConfig>;
pub type Keybinding = HashMap<KeyCombination, Command>;
Expand Down Expand Up @@ -161,7 +163,7 @@ pub enum Command {
#[strum(props(desc = "Switch between single-line and multi-line modes."))]
ToggleMultiline,
#[strum(props(desc = "Sends emoji from input line as reaction on selected message."))]
React,
React(Option<String>),
#[strum(props(desc = "Scroll a widget", usage = "scroll help up|down entry"))]
#[strum(serialize = "scroll", to_string = "scroll {0} {1} {2}")]
Scroll(Widget, DirectionVertical, MoveAmountVisual),
Expand Down Expand Up @@ -376,6 +378,19 @@ fn parse(input: &str) -> Result<Command, CommandParseError> {
Ok(Command::CopyMessage(selector))
// Ok(Command::CopyMessage(MessageSelector::from_str(args.first().unwrap_or(&""))?))
}
Command::React(_) => {
let usage = E::InsufficientArgs {
cmd: cmd_str.to_string(),
hint: Some("Optional emoji or :emoji code:".into()),
};
match args.first() {
None => Ok(Command::React(None)),
Some(&s) => match to_emoji(s) {
Some(em) => Ok(Command::React(Some(em.into()))),
None => Err(usage),
},
}
}
_ => Ok(cmd),
}
}
Expand Down