Commit 1cb280df8b

1cb280df8b3df7abd10c03862edd62c459984a54

parent: 5be886301b

Unregistered key

Benjamin Lee <benjamin@computer.surgery> · 2024-02-28 05:09 UTC
committer: <noreply@github.com>

Fix truncation/padding for non-ASCII sender names (#182)

Layout: unified · split

src/message/mod.rs +5 −5
@@ -58,7 +58,7 @@ use crate::{
5858 base::{IambResult, RoomInfo},
5959 config::ApplicationSettings,
6060 message::html::{parse_matrix_html, StyleTree},
61 util::{space_span, wrapped_text},
61 util::{space, space_span, take_width, wrapped_text},
6262};
6363
6464mod html;
@@ -909,13 +909,13 @@ impl Message {
909909 }
910910
911911 let Span { content, style } = self.sender_span(info, settings);
912 let stop = content.len().min(28);
913 let s = &content[..stop];
912 let ((truncated, width), _) = take_width(content, 28);
913 let padding = 28 - width;
914914
915915 let sender = if align_right {
916 format!("{: >width$} ", s, width = 28)
916 space(padding) + &truncated + " "
917917 } else {
918 format!("{: <width$} ", s, width = 28)
918 truncated.into_owned() + &space(padding) + " "
919919 };
920920
921921 Span::styled(sender, style).into()
src/util.rs +11 −11
@@ -26,19 +26,19 @@ pub fn split_cow(cow: Cow<'_, str>, idx: usize) -> (Cow<'_, str>, Cow<'_, str>)
2626
2727pub fn take_width(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<'_, str>) {
2828 // Find where to split the line.
29 let mut idx = 0;
3029 let mut w = 0;
3130
32 for (i, g) in UnicodeSegmentation::grapheme_indices(s.as_ref(), true) {
33 let gw = UnicodeWidthStr::width(g);
34 idx = i;
35
36 if w + gw > width {
37 break;
38 }
39
40 w += gw;
41 }
31 let idx = UnicodeSegmentation::grapheme_indices(s.as_ref(), true)
32 .find_map(|(i, g)| {
33 let gw = UnicodeWidthStr::width(g);
34 if w + gw > width {
35 Some(i)
36 } else {
37 w += gw;
38 None
39 }
40 })
41 .unwrap_or(s.len());
4242
4343 let (s0, s1) = split_cow(s, idx);
4444