Commit 8edfe506b3

8edfe506b30e5109738feb01dda4ab877946ae34

parent: fe0ac776e1

Unregistered key

kaizoplant <kaizoplant@proton.me> · 2026-09-11 21:23 UTC
committer: <noreply@github.com>

Truncate usernames by grapheme instead of word boundaries (#651)

Layout: unified · split

src/message/mod.rs +3 −3
@@ -35,7 +35,7 @@ use crate::message::html::{StyleTree, parse_matrix_html};
35use crate::message::state::{body_cow_state, html_state}; 35use crate::message::state::{body_cow_state, html_state};
36use crate::prelude::*; 36use crate::prelude::*;
37use crate::preview::ImageStatus; 37use crate::preview::ImageStatus;
38use crate::util::{replace_emojis_in_str, space, space_span, take_width, wrapped_text}; 38use crate::util::{replace_emojis_in_str, space, space_span, take_width_grapheme, wrapped_text};
39 39
40mod compose; 40mod compose;
41mod html; 41mod html;
@@ -1276,14 +1276,14 @@ impl Message {
1276 let show_in_gutter = gutter_enabled && user_gutter > 2; 1276 let show_in_gutter = gutter_enabled && user_gutter > 2;
1277 1277
1278 if show_in_gutter { 1278 if show_in_gutter {
1279 let ((truncated, width), _) = take_width(content, user_gutter - 2); 1279 let ((truncated, width), _) = take_width_grapheme(content, user_gutter - 2);
1280 let padding = user_gutter - 2 - width; 1280 let padding = user_gutter - 2 - width;
1281 1281
1282 let sender = format!("{}{} ", space(padding), truncated); 1282 let sender = format!("{}{} ", space(padding), truncated);
1283 1283
1284 SenderSpan::Gutter(Span::styled(sender, style)) 1284 SenderSpan::Gutter(Span::styled(sender, style))
1285 } else if UnicodeWidthStr::width(content.as_ref()) > width { 1285 } else if UnicodeWidthStr::width(content.as_ref()) > width {
1286 let ((truncated, _), _) = take_width(content, width); 1286 let ((truncated, _), _) = take_width_grapheme(content, width);
1287 1287
1288 SenderSpan::Line(Span::styled(truncated, style)) 1288 SenderSpan::Line(Span::styled(truncated, style))
1289 } else { 1289 } else {
src/util.rs +20 −13
@@ -20,12 +20,11 @@ pub fn split_cow(cow: Cow<'_, str>, idx: usize) -> (Cow<'_, str>, Cow<'_, str>)
20 }, 20 },
21 } 21 }
22} 22}
23
24pub fn take_width(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<'_, str>) { 23pub fn take_width(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<'_, str>) {
25 // Find where to split the line. 24 // Find where to split the line.
26 let mut cur_width = 0; 25 let mut cur_width = 0;
27 26
28 let mut idx = UnicodeSegmentation::split_word_bound_indices(s.as_ref()) 27 let idx = UnicodeSegmentation::split_word_bound_indices(s.as_ref())
29 .find_map(|(i, word)| { 28 .find_map(|(i, word)| {
30 let word_width = UnicodeWidthStr::width(word); 29 let word_width = UnicodeWidthStr::width(word);
31 if cur_width + word_width > width { 30 if cur_width + word_width > width {
@@ -39,17 +38,7 @@ pub fn take_width(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<
39 38
40 if idx == 0 { 39 if idx == 0 {
41 // first word is wider than available; fall back to splitting by width 40 // first word is wider than available; fall back to splitting by width
42 idx = UnicodeSegmentation::grapheme_indices(s.as_ref(), true) 41 return take_width_grapheme(s, width);
43 .find_map(|(i, graph)| {
44 let graph_width = UnicodeWidthStr::width(graph);
45 if cur_width + graph_width > width {
46 Some(i)
47 } else {
48 cur_width += graph_width;
49 None
50 }
51 })
52 .unwrap_or(s.len());
53 } 42 }
54 43
55 let (s0, s1) = split_cow(s, idx); 44 let (s0, s1) = split_cow(s, idx);
@@ -57,6 +46,24 @@ pub fn take_width(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<
57 ((s0, cur_width), s1) 46 ((s0, cur_width), s1)
58} 47}
59 48
49pub fn take_width_grapheme(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<'_, str>) {
50 let mut cur_width = 0;
51 let idx = UnicodeSegmentation::grapheme_indices(s.as_ref(), true)
52 .find_map(|(i, graph)| {
53 let graph_width = UnicodeWidthStr::width(graph);
54 if cur_width + graph_width > width {
55 Some(i)
56 } else {
57 cur_width += graph_width;
58 None
59 }
60 })
61 .unwrap_or(s.len());
62
63 let (s0, s1) = split_cow(s, idx);
64 ((s0, cur_width), s1)
65}
66
60pub struct WrappedLinesIterator<'a> { 67pub struct WrappedLinesIterator<'a> {
61 iter: std::vec::IntoIter<Cow<'a, str>>, 68 iter: std::vec::IntoIter<Cow<'a, str>>,
62 curr: Option<Cow<'a, str>>, 69 curr: Option<Cow<'a, str>>,