Skip to content

Commit e03ad06

Browse files
Merge pull request #725 from kellda/zulip-ellipsis
Add an ellipsis at the end of truncated topics
2 parents 6bf4d88 + 90c7d38 commit e03ad06

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/handlers/major_change.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,21 @@ async fn handle(
157157
labels.push(Label { name: label_to_add });
158158
let github_req = issue.set_labels(&ctx.github, labels);
159159

160-
let mut zulip_topic = format!(" {}", issue.zulip_topic_reference());
161-
// We prepend the issue title, truncating such that the overall length does
162-
// not exceed 60 characters (a Zulip limitation).
163-
zulip_topic.insert_str(
164-
0,
165-
&issue.title[..std::cmp::min(issue.title.len(), 60 - zulip_topic.len())],
166-
);
160+
// Concatenate the issue title and the topic reference, truncating such that
161+
// the overall length does not exceed 60 characters (a Zulip limitation).
162+
let topic_ref = issue.zulip_topic_reference();
163+
// Skip chars until the last characters that can be written:
164+
// Maximum 60, minus the reference, minus the elipsis and the space
165+
let mut chars = issue
166+
.title
167+
.char_indices()
168+
.skip(60 - topic_ref.chars().count() - 2);
169+
let zulip_topic = match chars.next() {
170+
Some((len, _)) if chars.next().is_some() => {
171+
format!("{}… {}", &issue.title[..len], topic_ref)
172+
}
173+
_ => format!("{} {}", issue.title, topic_ref),
174+
};
167175

168176
let zulip_req = crate::zulip::MessageApiRequest {
169177
recipient: crate::zulip::Recipient::Stream {

src/handlers/notify_zulip.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ pub(super) async fn handle_input<'a>(
6868
let mut topic = config.topic.clone();
6969
topic = topic.replace("{number}", &event.issue.number.to_string());
7070
topic = topic.replace("{title}", &event.issue.title);
71-
topic.truncate(60); // Zulip limitation
71+
// Truncate to 60 chars (a Zulip limitation)
72+
let mut chars = topic.char_indices().skip(59);
73+
if let (Some((len, _)), Some(_)) = (chars.next(), chars.next()) {
74+
topic.truncate(len);
75+
topic.push('…');
76+
}
7277

7378
let mut msg = match input.notification_type {
7479
NotificationType::Labeled => config.message_on_add.as_ref().unwrap().clone(),

0 commit comments

Comments
 (0)