atmos/os_lib/web_engine/
text_ellipsis.rs1use alloc::format;
7use alloc::string::{String, ToString};
8use alloc::vec::Vec;
9
10pub const DEFAULT_ELLIPSIS: &str = "…";
12
13pub fn truncate_text_ellipsis<F>(
21 text: &str,
22 available_w: i32,
23 ellipsis: &str,
24 is_rtl: bool,
25 mut measure_fn: F,
26) -> String
27where
28 F: FnMut(&str) -> i32,
29{
30 if available_w <= 0 || text.is_empty() {
31 return String::new();
32 }
33 let full_w = measure_fn(text);
34 if full_w <= available_w {
35 return text.to_string();
36 }
37 let ellipsis_w = measure_fn(ellipsis);
38 let max_w = (available_w - ellipsis_w).max(0);
39 if max_w <= 0 {
40 return ellipsis.to_string();
41 }
42
43 if is_rtl {
44 let mut truncated = Vec::new();
45 let mut tw = 0i32;
46 for c in text.chars().rev() {
47 let mut cs = String::new();
48 cs.push(c);
49 let cw = measure_fn(&cs);
50 if tw + cw > max_w {
51 break;
52 }
53 truncated.push(c);
54 tw += cw;
55 }
56 let kept: String = truncated.into_iter().rev().collect();
57 format!("{}{}", ellipsis, kept)
58 } else {
59 let mut truncated = String::new();
60 let mut tw = 0i32;
61 for c in text.chars() {
62 let mut cs = String::new();
63 cs.push(c);
64 let cw = measure_fn(&cs);
65 if tw + cw > max_w {
66 break;
67 }
68 truncated.push(c);
69 tw += cw;
70 }
71 format!("{}{}", truncated, ellipsis)
72 }
73}