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::{
58 base::{IambResult, RoomInfo}, 58 base::{IambResult, RoomInfo},
59 config::ApplicationSettings, 59 config::ApplicationSettings,
60 message::html::{parse_matrix_html, StyleTree}, 60 message::html::{parse_matrix_html, StyleTree},
61 util::{space_span, wrapped_text}, 61 util::{space, space_span, take_width, wrapped_text},
62}; 62};
63 63
64mod html; 64mod html;
@@ -909,13 +909,13 @@ impl Message {
909 } 909 }
910 910
911 let Span { content, style } = self.sender_span(info, settings); 911 let Span { content, style } = self.sender_span(info, settings);
912 let stop = content.len().min(28); 912 let ((truncated, width), _) = take_width(content, 28);
913 let s = &content[..stop]; 913 let padding = 28 - width;
914 914
915 let sender = if align_right { 915 let sender = if align_right {
916 format!("{: >width$} ", s, width = 28) 916 space(padding) + &truncated + " "
917 } else { 917 } else {
918 format!("{: <width$} ", s, width = 28) 918 truncated.into_owned() + &space(padding) + " "
919 }; 919 };
920 920
921 Span::styled(sender, style).into() 921 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>)
26 26
27pub fn take_width(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<'_, str>) { 27pub fn take_width(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<'_, str>) {
28 // Find where to split the line. 28 // Find where to split the line.
29 let mut idx = 0;
30 let mut w = 0; 29 let mut w = 0;
31 30
32 for (i, g) in UnicodeSegmentation::grapheme_indices(s.as_ref(), true) { 31 let idx = UnicodeSegmentation::grapheme_indices(s.as_ref(), true)
33 let gw = UnicodeWidthStr::width(g); 32 .find_map(|(i, g)| {
34 idx = i; 33 let gw = UnicodeWidthStr::width(g);
35 34 if w + gw > width {
36 if w + gw > width { 35 Some(i)
37 break; 36 } else {
38 } 37 w += gw;
39 38 None
40 w += gw; 39 }
41 } 40 })
41 .unwrap_or(s.len());
42 42
43 let (s0, s1) = split_cow(s, idx); 43 let (s0, s1) = split_cow(s, idx);
44 44