Use `ellipse` crate for truncation

This commit is contained in:
Yash Karandikar 2022-07-28 18:32:17 -05:00
parent 5fb4e69b6d
commit 8965be4a8b
3 changed files with 20 additions and 6 deletions

16
Cargo.lock generated
View File

@ -210,6 +210,7 @@ name = "dircord"
version = "0.1.0"
dependencies = [
"anyhow",
"ellipse",
"fancy-regex",
"irc",
"lazy_static",
@ -222,6 +223,15 @@ dependencies = [
"vergen",
]
[[package]]
name = "ellipse"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1835a82a08e5c9393639e7cf99786a65af71f7fa9df7c91a519f2d52e6fa052d"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "encoding"
version = "0.2.33"
@ -1617,6 +1627,12 @@ dependencies = [
"tinyvec",
]
[[package]]
name = "unicode-segmentation"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99"
[[package]]
name = "unicode-width"
version = "0.1.9"

View File

@ -14,6 +14,7 @@ lazy_static = "1.4.0"
pulldown-cmark = "0.9.1"
fancy-regex = "0.10.0"
tokio-stream = "0.1.9"
ellipse = "0.2.0"
[dependencies.tokio]
version = "1.20.0"

View File

@ -1,6 +1,7 @@
use crate::{
regex, ChannelMappingKey, MembersKey, OptionReplacer, OptionStringKey, SenderKey, UserIdKey,
};
use ellipse::Ellipse;
use fancy_regex::{Captures, Replacer};
use pulldown_cmark::Parser;
use serenity::{
@ -150,7 +151,7 @@ impl EventHandler for Handler {
{
if let Ok(mut reply) = channel_id.message(&ctx, message_id).await {
reply.guild_id = guild_id; // lmao
let (reply_prefix, reply_content_limit) = create_prefix(&reply, true, &ctx).await;
let (reply_prefix, _) = create_prefix(&reply, true, &ctx).await;
let mut content = reply.content;
content = content.replace("\r\n", " "); // just in case
@ -160,11 +161,7 @@ impl EventHandler for Handler {
content = discord_to_irc_processing(&content, &**members_lock, &ctx, &roles).await;
let to_send = if content.len() > reply_content_limit {
format!("{}...", &content[..reply_content_limit - 3])
} else {
content
};
let to_send = (&*content).truncate_ellipse(40); // limit taken from discord
sender
.send_privmsg(channel, &format!("{}{}", reply_prefix, to_send))