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

This commit is contained in:
Benjamin Lee 2024-02-27 21:09:37 -08:00 committed by GitHub
parent 5be886301b
commit 1cb280df8b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 16 deletions

View file

@ -58,7 +58,7 @@ use crate::{
base::{IambResult, RoomInfo}, base::{IambResult, RoomInfo},
config::ApplicationSettings, config::ApplicationSettings,
message::html::{parse_matrix_html, StyleTree}, message::html::{parse_matrix_html, StyleTree},
util::{space_span, wrapped_text}, util::{space, space_span, take_width, wrapped_text},
}; };
mod html; mod html;
@ -909,13 +909,13 @@ impl Message {
} }
let Span { content, style } = self.sender_span(info, settings); let Span { content, style } = self.sender_span(info, settings);
let stop = content.len().min(28); let ((truncated, width), _) = take_width(content, 28);
let s = &content[..stop]; let padding = 28 - width;
let sender = if align_right { let sender = if align_right {
format!("{: >width$} ", s, width = 28) space(padding) + &truncated + " "
} else { } else {
format!("{: <width$} ", s, width = 28) truncated.into_owned() + &space(padding) + " "
}; };
Span::styled(sender, style).into() Span::styled(sender, style).into()

View file

@ -26,19 +26,19 @@ pub fn split_cow(cow: Cow<'_, str>, idx: usize) -> (Cow<'_, str>, Cow<'_, str>)
pub fn take_width(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<'_, str>) { pub fn take_width(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<'_, str>) {
// Find where to split the line. // Find where to split the line.
let mut idx = 0;
let mut w = 0; let mut w = 0;
for (i, g) in UnicodeSegmentation::grapheme_indices(s.as_ref(), true) { let idx = UnicodeSegmentation::grapheme_indices(s.as_ref(), true)
let gw = UnicodeWidthStr::width(g); .find_map(|(i, g)| {
idx = i; let gw = UnicodeWidthStr::width(g);
if w + gw > width {
if w + gw > width { Some(i)
break; } else {
} w += gw;
None
w += gw; }
} })
.unwrap_or(s.len());
let (s0, s1) = split_cow(s, idx); let (s0, s1) = split_cow(s, idx);