diff --git a/src/message/mod.rs b/src/message/mod.rs index a343e89..7f1e46b 100644 --- a/src/message/mod.rs +++ b/src/message/mod.rs @@ -58,7 +58,7 @@ use crate::{ base::{IambResult, RoomInfo}, config::ApplicationSettings, message::html::{parse_matrix_html, StyleTree}, - util::{space_span, wrapped_text}, + util::{space, space_span, take_width, wrapped_text}, }; mod html; @@ -909,13 +909,13 @@ impl Message { } let Span { content, style } = self.sender_span(info, settings); - let stop = content.len().min(28); - let s = &content[..stop]; + let ((truncated, width), _) = take_width(content, 28); + let padding = 28 - width; let sender = if align_right { - format!("{: >width$} ", s, width = 28) + space(padding) + &truncated + " " } else { - format!("{: , idx: usize) -> (Cow<'_, str>, Cow<'_, str>) pub fn take_width(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<'_, str>) { // Find where to split the line. - let mut idx = 0; let mut w = 0; - for (i, g) in UnicodeSegmentation::grapheme_indices(s.as_ref(), true) { - let gw = UnicodeWidthStr::width(g); - idx = i; - - if w + gw > width { - break; - } - - w += gw; - } + let idx = UnicodeSegmentation::grapheme_indices(s.as_ref(), true) + .find_map(|(i, g)| { + let gw = UnicodeWidthStr::width(g); + if w + gw > width { + Some(i) + } else { + w += gw; + None + } + }) + .unwrap_or(s.len()); let (s0, s1) = split_cow(s, idx);