From e277e52cbec7bd178337019df7840691845d683d Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Sun, 3 Mar 2019 03:03:43 -0500 Subject: add "time ago" formatting --- Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 1 + src/commands/meme.rs | 24 ++++++++++++++++++++++-- src/main.rs | 1 + 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index af9f59b..cb13a8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -643,6 +643,15 @@ dependencies = [ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "isolang" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "phf 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)", + "phf_codegen 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "itertools" version = "0.8.0" @@ -1731,6 +1740,7 @@ dependencies = [ "serenity 0.5.11 (git+https://github.com/mammothbane/serenity)", "sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", + "timeago 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "typemap 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1745,6 +1755,15 @@ dependencies = [ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "timeago" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "isolang 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tokio" version = "0.1.16" @@ -2141,6 +2160,7 @@ dependencies = [ "checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" "checksum indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7e81a7c05f79578dbc15793d8b619db9ba32b4577003ef3af1a91c416798c58d" "checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08" +"checksum isolang 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9e5855a5a1ba5957e064977b94e5cd15ecbe83904a5191576be11615186cd868" "checksum itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5b8467d9c1cebe26feb08c640139247fac215782d35371ade9a2136ed6085358" "checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" @@ -2266,6 +2286,7 @@ dependencies = [ "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e2f0c90a5f3459330ac8bc0d2f879c693bb7a2f59689c1083fc4ef83834da865" "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" +"checksum timeago 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "5e63f5de45d2827876c63a56b17dfd7ddbfa40790e5c81ce9e91666166e75fc2" "checksum tokio 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "fcaabb3cec70485d0df6e9454fe514393ad1c4070dee8915f11041e95630b230" "checksum tokio-current-thread 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "c756b04680eea21902a46fca4e9f410a2332c04995af590e07ff262e2193a9a3" "checksum tokio-executor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "30c6dbf2d1ad1de300b393910e8a3aa272b724a400b6531da03eed99e329fbf0" diff --git a/Cargo.toml b/Cargo.toml index 7b6b082..efa64b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ sha1 = { version = "^0.6", features = ["std"] } regex = "~1.1" itertools = "^0.8" serde_json = "~1.0" +timeago = "^0.1" [dependencies.serenity] default-features = false diff --git a/src/commands/meme.rs b/src/commands/meme.rs index 84035ff..58b7d63 100644 --- a/src/commands/meme.rs +++ b/src/commands/meme.rs @@ -16,6 +16,10 @@ use serenity::{ model::channel::Message, prelude::*, }; +use timeago::{ + Formatter, + TimeUnit, +}; use url::Url; use audio::ytdl_url; @@ -36,6 +40,16 @@ use crate::{ Result, }; +lazy_static! { + static ref TIME_FORMATTER: Formatter = { + let mut f = Formatter::new(); + f.min_unit(TimeUnit::Minutes); + f.num_items(2); + + f + }; +} + #[inline] pub fn meme(ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { _meme(ctx, msg, args, false) @@ -127,6 +141,12 @@ pub fn history(_: &mut Context, msg: &Message, mut args: Args) -> Result<()> { .enumerate() .rev() .map(|(i, rec)| { + use chrono; + + let dt = chrono::DateTime::from_utc(rec.time, chrono::Utc{}); + let ago = TIME_FORMATTER.convert((chrono::Utc::now() - dt).to_std().unwrap()); + + let rand = if rec.random { "R, " } else { "" }; Meme::find(&conn, rec.meme_id) .and_then(|meme| { Metadata::find(&conn, meme.metadata_id).map(|metadata| (metadata, meme)) @@ -134,11 +154,11 @@ pub fn history(_: &mut Context, msg: &Message, mut args: Args) -> Result<()> { .map(|(metadata, meme)| { let author_name = crate::TARGET_GUILD_ID.member(metadata.created_by as u64).map(|m| m.display_name().into_owned()).unwrap_or("???".to_owned()); let invoker_name = crate::TARGET_GUILD_ID.member(rec.user_id as u64).map(|m| m.display_name().into_owned()).unwrap_or("???".to_owned()); - format!("{}. \"{}\" by {} ({}). invoked by {} at {}", i + 1, meme.title, author_name, metadata.created.date(), invoker_name, rec.time) + format!("{}. [{}{}] \"{}\" by {} ({}). invoked by {}.", i + 1, rand, ago, meme.title, author_name, metadata.created.date(), invoker_name) }) .unwrap_or_else(|_| { let invoker_name = crate::TARGET_GUILD_ID.member(rec.user_id as u64).map(|m| m.display_name().into_owned()).unwrap_or("???".to_owned()); - format!("{}. not found. invoked by {} at {}", i + 1, invoker_name, rec.time) + format!("{}. [{}{}] not found. invoked by {}.", i + 1, rand, ago, invoker_name) }) }) .join("\n"); diff --git a/src/main.rs b/src/main.rs index b13137e..044e6cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ extern crate serde_json; extern crate serenity; extern crate sha1; extern crate time; +extern crate timeago; extern crate typemap; extern crate url; -- cgit v1.3.1