diff --git a/Cargo.lock b/Cargo.lock index 17f12e4..05c6f6d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1690,6 +1690,7 @@ dependencies = [ "tokio", "toml", "twitter-v2", + "unicode-segmentation", "url", ] @@ -2374,6 +2375,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-segmentation" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" + [[package]] name = "unicode-width" version = "0.1.10" diff --git a/Cargo.toml b/Cargo.toml index 0affa4f..650475f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,4 +25,5 @@ time = { version = "0.3.22", features = ["formatting", "macros"] } tokio = { version = "1.24.2", features = ["rt-multi-thread", "time", "macros"] } toml = "0.5.9" twitter-v2 = { version = "0.1.8", default-features = false, features = ["oauth2", "rustls-tls"] } +unicode-segmentation = "1.10.1" url = { version = "2.3.1", features = ["serde"] } diff --git a/src/main.rs b/src/main.rs index d1f852f..21fe24c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ use redis::AsyncCommands; use serde::{Deserialize, Serialize}; use time::OffsetDateTime; use twitter_v2::{authorization::Oauth1aToken, TwitterApi}; +use unicode_segmentation::UnicodeSegmentation; use url::Url; const TWEET_LENGTH: usize = 280; @@ -223,10 +224,16 @@ fn repo_uri(repo: &Repo) -> String { fn make_post_description(repo: &Repo, length_left: usize) -> String { let description = repo.description.replace('@', SMALL_COMMERCIAL_AT); - if repo.description.len() < length_left { + if repo.description.graphemes(true).count() < length_left { description } else { - format!("{} ...", description.split_at(length_left - 4).0) + format!( + "{} ...", + description + .graphemes(true) + .take(length_left - 4) + .collect::() + ) } }