Commit 1cb280df8b
Unregistered key
Layout: unified · split
src/message/mod.rs +5 −5
| @@ -58,7 +58,7 @@ use crate::{ | ||
| 58 | 58 | base::{IambResult, RoomInfo}, |
| 59 | 59 | config::ApplicationSettings, |
| 60 | 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 | |
| 64 | 64 | mod html; |
| @@ -909,13 +909,13 @@ impl Message { | ||
| 909 | 909 | } |
| 910 | 910 | |
| 911 | 911 | 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; | |
| 914 | 914 | |
| 915 | 915 | let sender = if align_right { |
| 916 | format!("{: >width$} ", s, width = 28) | |
| 916 | space(padding) + &truncated + " " | |
| 917 | 917 | } else { |
| 918 | format!("{: <width$} ", s, width = 28) | |
| 918 | truncated.into_owned() + &space(padding) + " " | |
| 919 | 919 | }; |
| 920 | 920 | |
| 921 | 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 | |
| 27 | 27 | pub fn take_width(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<'_, str>) { |
| 28 | 28 | // Find where to split the line. |
| 29 | let mut idx = 0; | |
| 30 | 29 | let mut w = 0; |
| 31 | 30 | |
| 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()); | |
| 42 | 42 | |
| 43 | 43 | let (s0, s1) = split_cow(s, idx); |
| 44 | 44 | |