From 7e3b3fa74f5ddf9d69538b2c4f87e0288d0dcb9e Mon Sep 17 00:00:00 2001 From: Jokler Date: Thu, 26 Oct 2017 22:33:02 +0200 Subject: Make the Emoji plugin respond to messages in private chats --- src/plugins/emoji.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/plugins/emoji.rs') diff --git a/src/plugins/emoji.rs b/src/plugins/emoji.rs index d2ed956..5d37feb 100644 --- a/src/plugins/emoji.rs +++ b/src/plugins/emoji.rs @@ -67,7 +67,9 @@ impl Plugin for Emoji { fn execute(&mut self, server: &IrcServer, message: &Message) -> Result<(), IrcError> { match message.command { - Command::PRIVMSG(ref target, ref content) => self.emoji(server, content, target), + Command::PRIVMSG(_, ref content) => { + self.emoji(server, content, message.response_target().unwrap()) + } _ => Ok(()), } } -- cgit v1.2.3-70-g09d2 From 2b943fa26f1d3c199cbc03e65c567e5c29887465 Mon Sep 17 00:00:00 2001 From: Jokler Date: Fri, 27 Oct 2017 18:19:39 +0200 Subject: Add emoji counting --- src/plugins/emoji.rs | 67 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 14 deletions(-) (limited to 'src/plugins/emoji.rs') diff --git a/src/plugins/emoji.rs b/src/plugins/emoji.rs index 5d37feb..99f6c26 100644 --- a/src/plugins/emoji.rs +++ b/src/plugins/emoji.rs @@ -1,43 +1,82 @@ extern crate unicode_names; +use std::fmt; + use irc::client::prelude::*; use irc::error::Error as IrcError; use plugin::*; +struct EmojiHandle { + symbol: char, + count: i32, +} + +impl fmt::Display for EmojiHandle { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + + let name = match unicode_names::name(self.symbol) { + Some(sym) => sym.to_string().to_lowercase(), + None => String::from("UNKNOWN"), + }; + + if self.count > 1 { + write!(f, "{}x {}", self.count, name) + } else { + write!(f, "{}", name) + } + } +} + #[derive(PluginName, Debug)] pub struct Emoji; + impl Emoji { pub fn new() -> Emoji { Emoji {} } fn emoji(&self, server: &IrcServer, content: &str, target: &str) -> Result<(), IrcError> { - - let mut names: Vec = Vec::new(); - for emoji in self.return_emojis(content) { - - let name = match unicode_names::name(emoji) { - Some(v) => format!("{}", v).to_lowercase(), - None => "UNKNOWN".to_string(), - }; - - names.push(name); - } + let names = self.return_emojis(content) + .iter() + .map(|e| e.to_string()) + .collect::>(); server.send_privmsg(target, &names.join(", ")) } - fn return_emojis(&self, string: &str) -> Vec { + fn return_emojis(&self, string: &str) -> Vec { + + let mut emojis: Vec = Vec::new(); + + let mut current = EmojiHandle { + symbol: ' ', + count: 0, + }; - let mut emojis: Vec = Vec::new(); for c in string.chars() { if self.is_emoji(&c) { - emojis.push(c); + if current.symbol == c { + current.count = current.count + 1; + + } else { + if current.count > 0 { + emojis.push(current); + } + + current = EmojiHandle { + symbol: c, + count: 1, + } + } } } + if current.count > 0 { + emojis.push(current); + } + emojis } -- cgit v1.2.3-70-g09d2 From d203f2689ac7addf36a32150691f459212199f09 Mon Sep 17 00:00:00 2001 From: Jokler Date: Sat, 28 Oct 2017 23:34:30 +0200 Subject: Reformat the emoji counting loop --- src/plugins/emoji.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) (limited to 'src/plugins/emoji.rs') diff --git a/src/plugins/emoji.rs b/src/plugins/emoji.rs index 99f6c26..09d5c27 100644 --- a/src/plugins/emoji.rs +++ b/src/plugins/emoji.rs @@ -46,7 +46,6 @@ impl Emoji { } fn return_emojis(&self, string: &str) -> Vec { - let mut emojis: Vec = Vec::new(); let mut current = EmojiHandle { @@ -56,19 +55,21 @@ impl Emoji { for c in string.chars() { - if self.is_emoji(&c) { - if current.symbol == c { - current.count = current.count + 1; - - } else { - if current.count > 0 { - emojis.push(current); - } - - current = EmojiHandle { - symbol: c, - count: 1, - } + if !self.is_emoji(&c) { + continue; + } + + if current.symbol == c { + current.count += 1; + + } else { + if current.count > 0 { + emojis.push(current); + } + + current = EmojiHandle { + symbol: c, + count: 1, } } } -- cgit v1.2.3-70-g09d2