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};
3535use crate::message::state::{body_cow_state, html_state};
3636use crate::prelude::*;
3737use 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};
3939
4040mod compose;
4141mod html;
@@ -1276,14 +1276,14 @@ impl Message {
12761276 let show_in_gutter = gutter_enabled && user_gutter > 2;
12771277
12781278 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);
12801280 let padding = user_gutter - 2 - width;
12811281
12821282 let sender = format!("{}{} ", space(padding), truncated);
12831283
12841284 SenderSpan::Gutter(Span::styled(sender, style))
12851285 } else if UnicodeWidthStr::width(content.as_ref()) > width {
1286 let ((truncated, _), _) = take_width(content, width);
1286 let ((truncated, _), _) = take_width_grapheme(content, width);
12871287
12881288 SenderSpan::Line(Span::styled(truncated, style))
12891289 } else {
src/util.rs +20 −13
@@ -20,12 +20,11 @@ pub fn split_cow(cow: Cow<'_, str>, idx: usize) -> (Cow<'_, str>, Cow<'_, str>)
2020 },
2121 }
2222}
23
2423pub fn take_width(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<'_, str>) {
2524 // Find where to split the line.
2625 let mut cur_width = 0;
2726
28 let mut idx = UnicodeSegmentation::split_word_bound_indices(s.as_ref())
27 let idx = UnicodeSegmentation::split_word_bound_indices(s.as_ref())
2928 .find_map(|(i, word)| {
3029 let word_width = UnicodeWidthStr::width(word);
3130 if cur_width + word_width > width {
@@ -39,17 +38,7 @@ pub fn take_width(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<
3938
4039 if idx == 0 {
4140 // first word is wider than available; fall back to splitting by width
42 idx = UnicodeSegmentation::grapheme_indices(s.as_ref(), true)
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());
41 return take_width_grapheme(s, width);
5342 }
5443
5544 let (s0, s1) = split_cow(s, idx);
@@ -57,6 +46,24 @@ pub fn take_width(s: Cow<'_, str>, width: usize) -> ((Cow<'_, str>, usize), Cow<
5746 ((s0, cur_width), s1)
5847}
5948
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
6067pub struct WrappedLinesIterator<'a> {
6168 iter: std::vec::IntoIter<Cow<'a, str>>,
6269 curr: Option<Cow<'a, str>>,