Skip to content

Commit

Permalink
Use unicode-segmentation for split
Browse files Browse the repository at this point in the history
  • Loading branch information
pbzweihander committed Jul 31, 2023
1 parent 7a9786c commit a8b361f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<String>()
)
}
}

Expand Down

0 comments on commit a8b361f

Please sign in to comment.