Commit 16f2266c55

16f2266c556413568f29d723167216a534d21252

parent: a97c3c9f3c

Unregistered key

Steve Jason <145211128+56steve@users.noreply.github.com> · 2026-11-17 06:01 UTC
committer: <noreply@github.com>

Fix panic while truncating a multibyte username in `:message` window (#863)

Layout: unified · split

src/windows/room/message.rs +47 −8
@@ -4,7 +4,7 @@ use ratatui_image::sliced::{SignedPosition, SlicedImage};
4 4
5use crate::prelude::*; 5use crate::prelude::*;
6use crate::preview::ImageStatus; 6use crate::preview::ImageStatus;
7use crate::util::space; 7use crate::util::{space, take_width_grapheme};
8use crate::windows::room::chat; 8use crate::windows::room::chat;
9 9
10fn contains_reaction( 10fn contains_reaction(
@@ -514,22 +514,28 @@ impl ListItem<IambInfo> for MessageInfoItem {
514 let mut date = timestamp.as_datetime().format("%T %A, %B %d %Y").to_string(); 514 let mut date = timestamp.as_datetime().format("%T %A, %B %d %Y").to_string();
515 515
516 // truncate if needed 516 // truncate if needed
517 if user.len() > width { 517 let mut user_width = UnicodeWidthStr::width(user.as_ref());
518 if user_width > width {
518 date.clear(); 519 date.clear();
519 let user = user.to_mut(); 520 let keep = width.saturating_sub(2);
520 std::mem::drop(user.drain(width.saturating_sub(2)..)); 521 let ((head, head_width), _) =
522 take_width_grapheme(Cow::Borrowed(user.as_ref()), keep);
523 let mut head = head.into_owned();
524 user_width = head_width;
521 if width >= 2 { 525 if width >= 2 {
522 user.push_str(".."); 526 head.push_str("..");
527 user_width += 2;
523 } 528 }
524 } else if user.len() + date.len() >= width { 529 user = Cow::Owned(head);
525 let date_width = width.saturating_sub(user.len() + 2); 530 } else if user_width + date.len() >= width {
531 let date_width = width.saturating_sub(user_width + 2);
526 std::mem::drop(date.drain(..date.len().saturating_sub(date_width))); 532 std::mem::drop(date.drain(..date.len().saturating_sub(date_width)));
527 if date_width >= 2 { 533 if date_width >= 2 {
528 date.insert_str(0, ".."); 534 date.insert_str(0, "..");
529 } 535 }
530 } 536 }
531 537
532 let padding = width - user.len() - date.len(); 538 let padding = width - user_width - date.len();
533 let date_style = theme.timeline.date; 539 let date_style = theme.timeline.date;
534 540
535 let line = Span::styled(user, theme.selected(user_style, selected)) + 541 let line = Span::styled(user, theme.selected(user_style, selected)) +
@@ -682,6 +688,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for MessageInfoItem {
682#[cfg(test)] 688#[cfg(test)]
683mod tests { 689mod tests {
684 use super::*; 690 use super::*;
691 use crate::config::UserDisplayStyle;
685 use crate::tests::*; 692 use crate::tests::*;
686 693
687 #[tokio::test] 694 #[tokio::test]
@@ -720,4 +727,36 @@ mod tests {
720 let text = item.show(false, &viewctx, &store); 727 let text = item.show(false, &viewctx, &store);
721 assert_eq!(text.width(), viewctx.get_width()); 728 assert_eq!(text.width(), viewctx.get_width());
722 } 729 }
730
731 #[tokio::test]
732 async fn test_message_header_truncate_multibyte_name() {
733 let mut store = mock_store().await;
734 let room_id = TEST_ROOM1_ID.clone();
735 let user_id = TEST_USER1.clone();
736 let msg_key = MSG1_KEY.clone();
737
738 store.application.settings.tunables.username_display = UserDisplayStyle::DisplayName;
739
740 let item = MessageInfoItem::Header(room_id.clone(), user_id.clone(), msg_key.ts);
741
742 let mut viewctx = ViewportContext::new();
743 viewctx.dimensions.1 = 10;
744
745 // Names whose byte offsets don't line up with their display columns.
746 let names = [
747 "\u{e9}\u{e9}\u{e9}\u{e9}\u{e9}\u{e9}",
748 "\u{4e16}\u{754c}\u{4e16}\u{754c}\u{4e16}\u{754c}",
749 ];
750
751 for name in names {
752 let room = store.application.rooms.get_mut(&room_id).unwrap();
753 room.display_names.set(user_id.clone(), Some(name.into()), true);
754
755 for width in 0..20 {
756 viewctx.dimensions.0 = width;
757 let text = item.show(false, &viewctx, &store);
758 assert_eq!(text.width(), viewctx.get_width());
759 }
760 }
761 }
723} 762}