Kaynağa Gözat

Merge pull request #725 from kellda/zulip-ellipsis

Add an ellipsis at the end of truncated topics
Mark Rousskov 4 yıl önce
ebeveyn
işleme
e03ad06fed
2 değiştirilmiş dosya ile 21 ekleme ve 8 silme
  1. 15 7
      src/handlers/major_change.rs
  2. 6 1
      src/handlers/notify_zulip.rs

+ 15 - 7
src/handlers/major_change.rs

@@ -157,13 +157,21 @@ async fn handle(
     labels.push(Label { name: label_to_add });
     let github_req = issue.set_labels(&ctx.github, labels);
 
-    let mut zulip_topic = format!(" {}", issue.zulip_topic_reference());
-    // We prepend the issue title, truncating such that the overall length does
-    // not exceed 60 characters (a Zulip limitation).
-    zulip_topic.insert_str(
-        0,
-        &issue.title[..std::cmp::min(issue.title.len(), 60 - zulip_topic.len())],
-    );
+    // Concatenate the issue title and the topic reference, truncating such that
+    // the overall length does not exceed 60 characters (a Zulip limitation).
+    let topic_ref = issue.zulip_topic_reference();
+    // Skip chars until the last characters that can be written:
+    // Maximum 60, minus the reference, minus the elipsis and the space
+    let mut chars = issue
+        .title
+        .char_indices()
+        .skip(60 - topic_ref.chars().count() - 2);
+    let zulip_topic = match chars.next() {
+        Some((len, _)) if chars.next().is_some() => {
+            format!("{}… {}", &issue.title[..len], topic_ref)
+        }
+        _ => format!("{} {}", issue.title, topic_ref),
+    };
 
     let zulip_req = crate::zulip::MessageApiRequest {
         recipient: crate::zulip::Recipient::Stream {

+ 6 - 1
src/handlers/notify_zulip.rs

@@ -68,7 +68,12 @@ pub(super) async fn handle_input<'a>(
     let mut topic = config.topic.clone();
     topic = topic.replace("{number}", &event.issue.number.to_string());
     topic = topic.replace("{title}", &event.issue.title);
-    topic.truncate(60); // Zulip limitation
+    // Truncate to 60 chars (a Zulip limitation)
+    let mut chars = topic.char_indices().skip(59);
+    if let (Some((len, _)), Some(_)) = (chars.next(), chars.next()) {
+        topic.truncate(len);
+        topic.push('…');
+    }
 
     let mut msg = match input.notification_type {
         NotificationType::Labeled => config.message_on_add.as_ref().unwrap().clone(),