| 1 |
Description: avoid not-in-Debian crate unicode-ellipsis |
| 2 |
This essentially reverts upstream git commit 073ca89. |
| 3 |
Author: Jonas Smedegaard <[email protected]> |
| 4 |
Forwarded: not-needed |
| 5 |
Last-Update: 2024-09-14 |
| 6 |
--- |
| 7 |
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/ |
| 8 |
--- a/Cargo.toml |
| 9 |
+++ b/Cargo.toml |
| 10 |
@@ -97,7 +97,6 @@ |
| 11 |
sysinfo = "0.39.5" |
| 12 |
timeless = "0.1.0" |
| 13 |
toml_edit = { version = "0.25.12", features = ["serde"] } |
| 14 |
|
| 15 |
unicode-segmentation = "1.13.2" |
| 16 |
unicode-width = "0.2.2" |
| 17 |
|
| 18 |
--- a/src/utils/input.rs |
| 19 |
+++ b/src/utils/input.rs |
| 20 |
@@ -4,10 +4,9 @@ |
| 21 |
use std::ops::Range; |
| 22 |
|
| 23 |
use concat_string::concat_string; |
| 24 |
|
| 25 |
use unicode_segmentation::{GraphemeCursor, GraphemeIncomplete, UnicodeSegmentation}; |
| 26 |
|
| 27 |
|
| 28 |
+use crate::{app::CursorDirection, utils::{int_hash::IntIndexMap, strings::str_width}}; |
| 29 |
|
| 30 |
/// An input field's state. |
| 31 |
pub struct InputFieldState { |
| 32 |
@@ -208,7 +207,7 @@ |
| 33 |
for (index, grapheme) in |
| 34 |
UnicodeSegmentation::grapheme_indices(self.current_query.as_str(), true) |
| 35 |
{ |
| 36 |
|
| 37 |
+ let width = str_width(grapheme); |
| 38 |
let end = curr_offset + width; |
| 39 |
|
| 40 |
self.size_mappings.insert(index, curr_offset..end); |
| 41 |
--- a/src/utils/general.rs |
| 42 |
+++ b/src/utils/general.rs |
| 43 |
@@ -99,22 +99,4 @@ |
| 44 |
let val: usize = 100; |
| 45 |
assert_eq!(val.clamp_lower(50), 100); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
} |
| 66 |
--- a/src/utils/strings.rs |
| 67 |
+++ b/src/utils/strings.rs |
| 68 |
@@ -1,12 +1,169 @@ |
| 69 |
+use std::num::NonZeroUsize; |
| 70 |
+ |
| 71 |
use ratatui::text::Text; |
| 72 |
|
| 73 |
+use unicode_segmentation::UnicodeSegmentation; |
| 74 |
+use unicode_width::UnicodeWidthStr; |
| 75 |
|
| 76 |
/// Truncates text if it is too long, and adds an ellipsis at the end if needed. |
| 77 |
/// |
| 78 |
/// TODO: Maybe cache results from this function for some cases? e.g. columns |
| 79 |
#[inline] |
| 80 |
pub fn truncate_to_text<'a, U: Into<usize>>(content: &str, width: U) -> Text<'a> { |
| 81 |
|
| 82 |
+ Text::raw(truncate_str(content, width)) |
| 83 |
+} |
| 84 |
+ |
| 85 |
+/// Returns the width of a str `s`. This takes into account some things like |
| 86 |
+/// joiners when calculating width. |
| 87 |
+#[inline] |
| 88 |
+pub fn str_width(s: &str) -> usize { |
| 89 |
+ UnicodeSegmentation::graphemes(s, true) |
| 90 |
+ .map(|g| { |
| 91 |
+ if g.contains('\u{200d}') { |
| 92 |
+ 2 |
| 93 |
+ } else { |
| 94 |
+ UnicodeWidthStr::width(g) |
| 95 |
+ } |
| 96 |
+ }) |
| 97 |
+ .sum() |
| 98 |
+} |
| 99 |
+ |
| 100 |
+/// Returns the "width" of grapheme `g`. This takes into account some things like |
| 101 |
+/// joiners when calculating width. |
| 102 |
+/// |
| 103 |
+/// Note that while you *can* pass in an entire string, the point is to check |
| 104 |
+/// individual graphemes (e.g. `"a"`, `"💎"`, `"大"`, `"🇨🇦"`). |
| 105 |
+#[inline] |
| 106 |
+fn grapheme_width(g: &str) -> usize { |
| 107 |
+ if g.contains('\u{200d}') { |
| 108 |
+ 2 |
| 109 |
+ } else { |
| 110 |
+ UnicodeWidthStr::width(g) |
| 111 |
+ } |
| 112 |
+} |
| 113 |
+ |
| 114 |
+enum AsciiIterationResult { |
| 115 |
+ Complete, |
| 116 |
+ Remaining(usize), |
| 117 |
+} |
| 118 |
+ |
| 119 |
+/// Greedily add characters to the output until a non-ASCII grapheme is found, or |
| 120 |
+/// the output is `width` long. |
| 121 |
+#[inline] |
| 122 |
+fn greedy_ascii_add(content: &str, width: NonZeroUsize) -> (String, AsciiIterationResult) { |
| 123 |
+ let width: usize = width.into(); |
| 124 |
+ |
| 125 |
+ const SIZE_OF_ELLIPSIS: usize = 3; |
| 126 |
+ let mut text = Vec::with_capacity(width - 1 + SIZE_OF_ELLIPSIS); |
| 127 |
+ |
| 128 |
+ let s = content.as_bytes(); |
| 129 |
+ |
| 130 |
+ let mut current_index = 0; |
| 131 |
+ |
| 132 |
+ while current_index < width - 1 { |
| 133 |
+ let current_byte = s[current_index]; |
| 134 |
+ if current_byte.is_ascii() { |
| 135 |
+ text.push(current_byte); |
| 136 |
+ current_index += 1; |
| 137 |
+ } else { |
| 138 |
+ debug_assert!(text.is_ascii()); |
| 139 |
+ |
| 140 |
+ let current_index = AsciiIterationResult::Remaining(current_index); |
| 141 |
+ |
| 142 |
+ // SAFETY: This conversion is safe to do unchecked, we only push ASCII characters up to |
| 143 |
+ // this point. |
| 144 |
+ let current_text = unsafe { String::from_utf8_unchecked(text) }; |
| 145 |
+ |
| 146 |
+ return (current_text, current_index); |
| 147 |
+ } |
| 148 |
+ } |
| 149 |
+ |
| 150 |
+ // If we made it all the way through, then we probably hit the width limit. |
| 151 |
+ debug_assert!(text.is_ascii()); |
| 152 |
+ |
| 153 |
+ let current_index = if s[current_index].is_ascii() { |
| 154 |
+ let mut ellipsis = [0; SIZE_OF_ELLIPSIS]; |
| 155 |
+ '…'.encode_utf8(&mut ellipsis); |
| 156 |
+ text.extend_from_slice(&ellipsis); |
| 157 |
+ AsciiIterationResult::Complete |
| 158 |
+ } else { |
| 159 |
+ AsciiIterationResult::Remaining(current_index) |
| 160 |
+ }; |
| 161 |
+ |
| 162 |
+ // SAFETY: This conversion is safe to do unchecked, we only push ASCII characters up to |
| 163 |
+ // this point. |
| 164 |
+ let current_text = unsafe { String::from_utf8_unchecked(text) }; |
| 165 |
+ |
| 166 |
+ (current_text, current_index) |
| 167 |
+} |
| 168 |
+ |
| 169 |
+/// Truncates a string to the specified width with an ellipsis character. |
| 170 |
+/// |
| 171 |
+/// NB: This probably does not handle EVERY case, but I think it handles most cases |
| 172 |
+/// we will use this function for fine... hopefully. |
| 173 |
+/// |
| 174 |
+/// TODO: Maybe fuzz this function? |
| 175 |
+/// TODO: Maybe release this as a lib? Testing against Fish's script [here](https://github.com/ridiculousfish/widecharwidth) might be useful. |
| 176 |
+#[inline] |
| 177 |
+pub fn truncate_str<U: Into<usize>>(content: &str, width: U) -> String { |
| 178 |
+ let width = width.into(); |
| 179 |
+ |
| 180 |
+ if content.len() <= width { |
| 181 |
+ // If the entire string fits in the width, then we just |
| 182 |
+ // need to copy the entire string over. |
| 183 |
+ |
| 184 |
+ content.to_owned() |
| 185 |
+ } else if let Some(nz_width) = NonZeroUsize::new(width) { |
| 186 |
+ // What we are essentially doing is optimizing for the case that |
| 187 |
+ // most, if not all of the string is ASCII. As such: |
| 188 |
+ // - Step through each byte until (width - 1) is hit or we find a non-ascii |
| 189 |
+ // byte. |
| 190 |
+ // - If the byte is ascii, then add it. |
| 191 |
+ // |
| 192 |
+ // If we didn't get a complete truncated string, then continue on treating the rest as graphemes. |
| 193 |
+ |
| 194 |
+ let (mut text, res) = greedy_ascii_add(content, nz_width); |
| 195 |
+ match res { |
| 196 |
+ AsciiIterationResult::Complete => text, |
| 197 |
+ AsciiIterationResult::Remaining(current_index) => { |
| 198 |
+ let mut curr_width = text.len(); |
| 199 |
+ let mut early_break = false; |
| 200 |
+ |
| 201 |
+ // This tracks the length of the last added string - note this does NOT match the grapheme *width*. |
| 202 |
+ // Since the previous characters are always ASCII, this is always initialized as 1, unless the string |
| 203 |
+ // is empty. |
| 204 |
+ let mut last_added_str_len = if text.is_empty() { 0 } else { 1 }; |
| 205 |
+ |
| 206 |
+ // Cases to handle: |
| 207 |
+ // - Completes adding the entire string. |
| 208 |
+ // - Adds a character up to the boundary, then fails. |
| 209 |
+ // - Adds a character not up to the boundary, then fails. |
| 210 |
+ // Inspired by https://tomdebruijn.com/posts/rust-string-length-width-calculations/ |
| 211 |
+ for g in UnicodeSegmentation::graphemes(&content[current_index..], true) { |
| 212 |
+ let g_width = grapheme_width(g); |
| 213 |
+ |
| 214 |
+ if curr_width + g_width <= width { |
| 215 |
+ curr_width += g_width; |
| 216 |
+ last_added_str_len = g.len(); |
| 217 |
+ text.push_str(g); |
| 218 |
+ } else { |
| 219 |
+ early_break = true; |
| 220 |
+ break; |
| 221 |
+ } |
| 222 |
+ } |
| 223 |
+ |
| 224 |
+ if early_break { |
| 225 |
+ if curr_width == width { |
| 226 |
+ // Remove the last grapheme cluster added. |
| 227 |
+ text.truncate(text.len() - last_added_str_len); |
| 228 |
+ } |
| 229 |
+ text.push('…'); |
| 230 |
+ } |
| 231 |
+ text |
| 232 |
+ } |
| 233 |
+ } |
| 234 |
+ } else { |
| 235 |
+ String::default() |
| 236 |
+ } |
| 237 |
} |
| 238 |
|
| 239 |
/// Checks that the first string is equal to any of the other ones in a ASCII |
| 240 |
@@ -35,6 +192,262 @@ |
| 241 |
|
| 242 |
#[cfg(test)] |
| 243 |
mod tests { |
| 244 |
+ use crate::utils::general::sort_partial_fn; |
| 245 |
+ |
| 246 |
+ use super::*; |
| 247 |
+ |
| 248 |
+ #[test] |
| 249 |
+ fn test_sort_partial_fn() { |
| 250 |
+ let mut x = vec![9, 5, 20, 15, 10, 5]; |
| 251 |
+ let mut y = vec![1.0, 15.0, -1.0, -100.0, -100.1, 16.15, -100.0]; |
| 252 |
+ |
| 253 |
+ x.sort_by(|a, b| sort_partial_fn(false)(a, b)); |
| 254 |
+ assert_eq!(x, vec![5, 5, 9, 10, 15, 20]); |
| 255 |
+ |
| 256 |
+ x.sort_by(|a, b| sort_partial_fn(true)(a, b)); |
| 257 |
+ assert_eq!(x, vec![20, 15, 10, 9, 5, 5]); |
| 258 |
+ |
| 259 |
+ y.sort_by(|a, b| sort_partial_fn(false)(a, b)); |
| 260 |
+ assert_eq!(y, vec![-100.1, -100.0, -100.0, -1.0, 1.0, 15.0, 16.15]); |
| 261 |
+ |
| 262 |
+ y.sort_by(|a, b| sort_partial_fn(true)(a, b)); |
| 263 |
+ assert_eq!(y, vec![16.15, 15.0, 1.0, -1.0, -100.0, -100.0, -100.1]); |
| 264 |
+ } |
| 265 |
+ |
| 266 |
+ #[test] |
| 267 |
+ fn test_truncate_str() { |
| 268 |
+ let cpu_header = "CPU(c)▲"; |
| 269 |
+ |
| 270 |
+ assert_eq!( |
| 271 |
+ truncate_str(cpu_header, 8_usize), |
| 272 |
+ cpu_header, |
| 273 |
+ "should match base string as there is extra room" |
| 274 |
+ ); |
| 275 |
+ |
| 276 |
+ assert_eq!( |
| 277 |
+ truncate_str(cpu_header, 7_usize), |
| 278 |
+ cpu_header, |
| 279 |
+ "should match base string as there is enough room" |
| 280 |
+ ); |
| 281 |
+ |
| 282 |
+ assert_eq!(truncate_str(cpu_header, 6_usize), "CPU(c…"); |
| 283 |
+ assert_eq!(truncate_str(cpu_header, 5_usize), "CPU(…"); |
| 284 |
+ assert_eq!(truncate_str(cpu_header, 4_usize), "CPU…"); |
| 285 |
+ assert_eq!(truncate_str(cpu_header, 1_usize), "…"); |
| 286 |
+ assert_eq!(truncate_str(cpu_header, 0_usize), ""); |
| 287 |
+ } |
| 288 |
+ |
| 289 |
+ #[test] |
| 290 |
+ fn test_truncate_ascii() { |
| 291 |
+ let content = "0123456"; |
| 292 |
+ |
| 293 |
+ assert_eq!( |
| 294 |
+ truncate_str(content, 8_usize), |
| 295 |
+ content, |
| 296 |
+ "should match base string as there is extra room" |
| 297 |
+ ); |
| 298 |
+ |
| 299 |
+ assert_eq!( |
| 300 |
+ truncate_str(content, 7_usize), |
| 301 |
+ content, |
| 302 |
+ "should match base string as there is enough room" |
| 303 |
+ ); |
| 304 |
+ |
| 305 |
+ assert_eq!(truncate_str(content, 6_usize), "01234…"); |
| 306 |
+ assert_eq!(truncate_str(content, 5_usize), "0123…"); |
| 307 |
+ assert_eq!(truncate_str(content, 4_usize), "012…"); |
| 308 |
+ assert_eq!(truncate_str(content, 1_usize), "…"); |
| 309 |
+ assert_eq!(truncate_str(content, 0_usize), ""); |
| 310 |
+ } |
| 311 |
+ |
| 312 |
+ #[test] |
| 313 |
+ fn test_truncate_cjk() { |
| 314 |
+ let cjk = "施氏食獅史"; |
| 315 |
+ |
| 316 |
+ assert_eq!( |
| 317 |
+ truncate_str(cjk, 11_usize), |
| 318 |
+ cjk, |
| 319 |
+ "should match base string as there is extra room" |
| 320 |
+ ); |
| 321 |
+ |
| 322 |
+ assert_eq!( |
| 323 |
+ truncate_str(cjk, 10_usize), |
| 324 |
+ cjk, |
| 325 |
+ "should match base string as there is enough room" |
| 326 |
+ ); |
| 327 |
+ |
| 328 |
+ assert_eq!(truncate_str(cjk, 9_usize), "施氏食獅…"); |
| 329 |
+ assert_eq!(truncate_str(cjk, 8_usize), "施氏食…"); |
| 330 |
+ assert_eq!(truncate_str(cjk, 2_usize), "…"); |
| 331 |
+ assert_eq!(truncate_str(cjk, 1_usize), "…"); |
| 332 |
+ assert_eq!(truncate_str(cjk, 0_usize), ""); |
| 333 |
+ |
| 334 |
+ let cjk_2 = "你好嗎"; |
| 335 |
+ assert_eq!(truncate_str(cjk_2, 5_usize), "你好…"); |
| 336 |
+ assert_eq!(truncate_str(cjk_2, 4_usize), "你…"); |
| 337 |
+ assert_eq!(truncate_str(cjk_2, 3_usize), "你…"); |
| 338 |
+ assert_eq!(truncate_str(cjk_2, 2_usize), "…"); |
| 339 |
+ assert_eq!(truncate_str(cjk_2, 1_usize), "…"); |
| 340 |
+ assert_eq!(truncate_str(cjk_2, 0_usize), ""); |
| 341 |
+ } |
| 342 |
+ |
| 343 |
+ #[test] |
| 344 |
+ fn test_truncate_mixed_one() { |
| 345 |
+ let test = "Test (施氏食獅史) Test"; |
| 346 |
+ |
| 347 |
+ assert_eq!( |
| 348 |
+ truncate_str(test, 30_usize), |
| 349 |
+ test, |
| 350 |
+ "should match base string as there is extra room" |
| 351 |
+ ); |
| 352 |
+ |
| 353 |
+ assert_eq!( |
| 354 |
+ truncate_str(test, 22_usize), |
| 355 |
+ test, |
| 356 |
+ "should match base string as there is just enough room" |
| 357 |
+ ); |
| 358 |
+ |
| 359 |
+ assert_eq!( |
| 360 |
+ truncate_str(test, 21_usize), |
| 361 |
+ "Test (施氏食獅史) Te…", |
| 362 |
+ "should truncate the t and replace the s with ellipsis" |
| 363 |
+ ); |
| 364 |
+ |
| 365 |
+ assert_eq!(truncate_str(test, 20_usize), "Test (施氏食獅史) T…"); |
| 366 |
+ assert_eq!(truncate_str(test, 19_usize), "Test (施氏食獅史) …"); |
| 367 |
+ assert_eq!(truncate_str(test, 18_usize), "Test (施氏食獅史)…"); |
| 368 |
+ assert_eq!(truncate_str(test, 17_usize), "Test (施氏食獅史…"); |
| 369 |
+ assert_eq!(truncate_str(test, 16_usize), "Test (施氏食獅…"); |
| 370 |
+ assert_eq!(truncate_str(test, 15_usize), "Test (施氏食獅…"); |
| 371 |
+ assert_eq!(truncate_str(test, 14_usize), "Test (施氏食…"); |
| 372 |
+ assert_eq!(truncate_str(test, 13_usize), "Test (施氏食…"); |
| 373 |
+ assert_eq!(truncate_str(test, 8_usize), "Test (…"); |
| 374 |
+ assert_eq!(truncate_str(test, 7_usize), "Test (…"); |
| 375 |
+ assert_eq!(truncate_str(test, 6_usize), "Test …"); |
| 376 |
+ assert_eq!(truncate_str(test, 5_usize), "Test…"); |
| 377 |
+ assert_eq!(truncate_str(test, 4_usize), "Tes…"); |
| 378 |
+ } |
| 379 |
+ |
| 380 |
+ #[test] |
| 381 |
+ fn test_truncate_mixed_two() { |
| 382 |
+ let test = "Test (施氏abc食abc獅史) Test"; |
| 383 |
+ |
| 384 |
+ assert_eq!( |
| 385 |
+ truncate_str(test, 30_usize), |
| 386 |
+ test, |
| 387 |
+ "should match base string as there is extra room" |
| 388 |
+ ); |
| 389 |
+ |
| 390 |
+ assert_eq!( |
| 391 |
+ truncate_str(test, 28_usize), |
| 392 |
+ test, |
| 393 |
+ "should match base string as there is just enough room" |
| 394 |
+ ); |
| 395 |
+ |
| 396 |
+ assert_eq!(truncate_str(test, 26_usize), "Test (施氏abc食abc獅史) T…"); |
| 397 |
+ assert_eq!(truncate_str(test, 21_usize), "Test (施氏abc食abc獅…"); |
| 398 |
+ assert_eq!(truncate_str(test, 20_usize), "Test (施氏abc食abc…"); |
| 399 |
+ assert_eq!(truncate_str(test, 16_usize), "Test (施氏abc食…"); |
| 400 |
+ assert_eq!(truncate_str(test, 15_usize), "Test (施氏abc…"); |
| 401 |
+ assert_eq!(truncate_str(test, 14_usize), "Test (施氏abc…"); |
| 402 |
+ assert_eq!(truncate_str(test, 11_usize), "Test (施氏…"); |
| 403 |
+ assert_eq!(truncate_str(test, 10_usize), "Test (施…"); |
| 404 |
+ } |
| 405 |
+ |
| 406 |
+ #[test] |
| 407 |
+ fn test_truncate_flags() { |
| 408 |
+ let flag = "🇨🇦"; |
| 409 |
+ assert_eq!(truncate_str(flag, 3_usize), flag); |
| 410 |
+ assert_eq!(truncate_str(flag, 2_usize), flag); |
| 411 |
+ assert_eq!(truncate_str(flag, 1_usize), "…"); |
| 412 |
+ assert_eq!(truncate_str(flag, 0_usize), ""); |
| 413 |
+ |
| 414 |
+ let flag_text = "oh 🇨🇦"; |
| 415 |
+ assert_eq!(truncate_str(flag_text, 6_usize), flag_text); |
| 416 |
+ assert_eq!(truncate_str(flag_text, 5_usize), flag_text); |
| 417 |
+ assert_eq!(truncate_str(flag_text, 4_usize), "oh …"); |
| 418 |
+ |
| 419 |
+ let flag_text_wrap = "!🇨🇦!"; |
| 420 |
+ assert_eq!(truncate_str(flag_text_wrap, 6_usize), flag_text_wrap); |
| 421 |
+ assert_eq!(truncate_str(flag_text_wrap, 4_usize), flag_text_wrap); |
| 422 |
+ assert_eq!(truncate_str(flag_text_wrap, 3_usize), "!…"); |
| 423 |
+ assert_eq!(truncate_str(flag_text_wrap, 2_usize), "!…"); |
| 424 |
+ assert_eq!(truncate_str(flag_text_wrap, 1_usize), "…"); |
| 425 |
+ |
| 426 |
+ let flag_cjk = "加拿大🇨🇦"; |
| 427 |
+ assert_eq!(truncate_str(flag_cjk, 9_usize), flag_cjk); |
| 428 |
+ assert_eq!(truncate_str(flag_cjk, 8_usize), flag_cjk); |
| 429 |
+ assert_eq!(truncate_str(flag_cjk, 7_usize), "加拿大…"); |
| 430 |
+ assert_eq!(truncate_str(flag_cjk, 6_usize), "加拿…"); |
| 431 |
+ assert_eq!(truncate_str(flag_cjk, 5_usize), "加拿…"); |
| 432 |
+ assert_eq!(truncate_str(flag_cjk, 4_usize), "加…"); |
| 433 |
+ |
| 434 |
+ let flag_mix = "🇨🇦加gaa拿naa大daai🇨🇦"; |
| 435 |
+ assert_eq!(truncate_str(flag_mix, 20_usize), flag_mix); |
| 436 |
+ assert_eq!(truncate_str(flag_mix, 19_usize), "🇨🇦加gaa拿naa大daai…"); |
| 437 |
+ assert_eq!(truncate_str(flag_mix, 18_usize), "🇨🇦加gaa拿naa大daa…"); |
| 438 |
+ assert_eq!(truncate_str(flag_mix, 17_usize), "🇨🇦加gaa拿naa大da…"); |
| 439 |
+ assert_eq!(truncate_str(flag_mix, 15_usize), "🇨🇦加gaa拿naa大…"); |
| 440 |
+ assert_eq!(truncate_str(flag_mix, 14_usize), "🇨🇦加gaa拿naa…"); |
| 441 |
+ assert_eq!(truncate_str(flag_mix, 13_usize), "🇨🇦加gaa拿naa…"); |
| 442 |
+ assert_eq!(truncate_str(flag_mix, 3_usize), "🇨🇦…"); |
| 443 |
+ assert_eq!(truncate_str(flag_mix, 2_usize), "…"); |
| 444 |
+ assert_eq!(truncate_str(flag_mix, 1_usize), "…"); |
| 445 |
+ assert_eq!(truncate_str(flag_mix, 0_usize), ""); |
| 446 |
+ } |
| 447 |
+ |
| 448 |
+ /// This might not be the best way to handle it, but this at least tests that it doesn't crash... |
| 449 |
+ #[test] |
| 450 |
+ #[ignore] |
| 451 |
+ fn test_truncate_hindi() { |
| 452 |
+ // cSpell:disable |
| 453 |
+ let test = "हिन्दी"; |
| 454 |
+ assert_eq!(truncate_str(test, 10_usize), test); |
| 455 |
+ assert_eq!(truncate_str(test, 6_usize), "हिन्दी"); |
| 456 |
+ assert_eq!(truncate_str(test, 5_usize), "हिन्दी"); |
| 457 |
+ assert_eq!(truncate_str(test, 4_usize), "हिन्…"); |
| 458 |
+ assert_eq!(truncate_str(test, 3_usize), "हि…"); |
| 459 |
+ assert_eq!(truncate_str(test, 2_usize), "…"); |
| 460 |
+ assert_eq!(truncate_str(test, 1_usize), "…"); |
| 461 |
+ assert_eq!(truncate_str(test, 0_usize), ""); |
| 462 |
+ // cSpell:enable |
| 463 |
+ } |
| 464 |
+ |
| 465 |
+ #[test] |
| 466 |
+ fn truncate_emoji() { |
| 467 |
+ let heart_1 = "♥"; |
| 468 |
+ assert_eq!(truncate_str(heart_1, 2_usize), heart_1); |
| 469 |
+ assert_eq!(truncate_str(heart_1, 1_usize), heart_1); |
| 470 |
+ assert_eq!(truncate_str(heart_1, 0_usize), ""); |
| 471 |
+ |
| 472 |
+ let heart_2 = "❤"; |
| 473 |
+ assert_eq!(truncate_str(heart_2, 2_usize), heart_2); |
| 474 |
+ assert_eq!(truncate_str(heart_2, 1_usize), heart_2); |
| 475 |
+ assert_eq!(truncate_str(heart_2, 0_usize), ""); |
| 476 |
+ |
| 477 |
+ // This one has a U+FE0F modifier at the end, and is thus considered "emoji-presentation", |
| 478 |
+ // see https://github.com/fish-shell/fish-shell/issues/10461#issuecomment-2079624670. |
| 479 |
+ // This shouldn't really be a common issue in a terminal but eh. |
| 480 |
+ let heart_emoji_pres = "❤️"; |
| 481 |
+ assert_eq!(truncate_str(heart_emoji_pres, 2_usize), heart_emoji_pres); |
| 482 |
+ assert_eq!(truncate_str(heart_emoji_pres, 1_usize), "…"); |
| 483 |
+ assert_eq!(truncate_str(heart_emoji_pres, 0_usize), ""); |
| 484 |
+ |
| 485 |
+ let emote = "💎"; |
| 486 |
+ assert_eq!(truncate_str(emote, 2_usize), emote); |
| 487 |
+ assert_eq!(truncate_str(emote, 1_usize), "…"); |
| 488 |
+ assert_eq!(truncate_str(emote, 0_usize), ""); |
| 489 |
+ |
| 490 |
+ let family = "👨👨👧👦"; |
| 491 |
+ assert_eq!(truncate_str(family, 2_usize), family); |
| 492 |
+ assert_eq!(truncate_str(family, 1_usize), "…"); |
| 493 |
+ assert_eq!(truncate_str(family, 0_usize), ""); |
| 494 |
+ |
| 495 |
+ let scientist = "👩🔬"; |
| 496 |
+ assert_eq!(truncate_str(scientist, 2_usize), scientist); |
| 497 |
+ assert_eq!(truncate_str(scientist, 1_usize), "…"); |
| 498 |
+ assert_eq!(truncate_str(scientist, 0_usize), ""); |
| 499 |
+ } |
| 500 |
|
| 501 |
#[test] |
| 502 |
fn test_multi_eq_ignore_ascii_case() { |
| 503 |
--- a/src/canvas/dialogs/process_kill_dialog.rs |
| 504 |
+++ b/src/canvas/dialogs/process_kill_dialog.rs |
| 505 |
@@ -1,5 +1,7 @@ |
| 506 |
//! A dialog box to handle killing processes. |
| 507 |
|
| 508 |
+use crate::utils::strings::truncate_str; |
| 509 |
+ |
| 510 |
use std::time::Instant; |
| 511 |
|
| 512 |
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))] |
| 513 |
@@ -640,7 +642,7 @@ |
| 514 |
|
| 515 |
if let Some(first_pid) = pids.first() { |
| 516 |
let truncated_process_name = |
| 517 |
|
| 518 |
+ truncate_str(process_name, MAX_PROCESS_NAME_WIDTH); |
| 519 |
|
| 520 |
let text = if pids.len() > 1 { |
| 521 |
Line::from(format!( |
| 522 |
--- a/src/canvas/components/time_series/vendored.rs |
| 523 |
+++ b/src/canvas/components/time_series/vendored.rs |
| 524 |
@@ -29,6 +29,7 @@ |
| 525 |
use crate::{ |
| 526 |
canvas::components::time_series::LegendConstraints, |
| 527 |
utils::general::{saturating_log2, saturating_log10}, |
| 528 |
+ utils::strings::truncate_str, |
| 529 |
}; |
| 530 |
|
| 531 |
pub const DEFAULT_LEGEND_CONSTRAINTS: LegendConstraints = LegendConstraints { |
| 532 |
@@ -925,7 +926,7 @@ |
| 533 |
dataset_name.clone() |
| 534 |
} else { |
| 535 |
let inner = dataset_name.to_string(); |
| 536 |
|
| 537 |
+ let truncated = truncate_str(&inner, max_width as usize); |
| 538 |
|
| 539 |
Line::from(truncated.to_string()) |
| 540 |
}; |