| 1 |
use ratatui::text::Text; |
| 2 |
use unicode_ellipsis::truncate_str; |
| 3 |
|
| 4 |
/// Truncates text if it is too long, and adds an ellipsis at the end if needed. |
| 5 |
/// |
| 6 |
/// TODO: Maybe cache results from this function for some cases? e.g. columns |
| 7 |
#[inline] |
| 8 |
pub fn truncate_to_text<'a, U: Into<usize>>(content: &str, width: U) -> Text<'a> { |
| 9 |
Text::raw(truncate_str(content, width.into()).to_string()) |
| 10 |
} |
| 11 |
|
| 12 |
/// Checks that the first string is equal to any of the other ones in a ASCII |
| 13 |
/// case-insensitive match. |
| 14 |
/// |
| 15 |
/// The generated code is the same as writing: |
| 16 |
/// `to_ascii_lowercase(a) == to_ascii_lowercase(b) || to_ascii_lowercase(a) == |
| 17 |
/// to_ascii_lowercase(c)`, but without allocating and copying temporaries. |
| 18 |
/// |
| 19 |
/// # Examples |
| 20 |
/// |
| 21 |
/// ```ignore |
| 22 |
/// assert!(multi_eq_ignore_ascii_case!("test", "test")); |
| 23 |
/// assert!(multi_eq_ignore_ascii_case!("test", "a" | "b" | "test")); |
| 24 |
/// assert!(!multi_eq_ignore_ascii_case!("test", "a" | "b" | "c")); |
| 25 |
/// ``` |
| 26 |
#[macro_export] |
| 27 |
macro_rules! multi_eq_ignore_ascii_case { |
| 28 |
( $lhs:expr, $last:literal ) => { |
| 29 |
$lhs.eq_ignore_ascii_case($last) |
| 30 |
}; |
| 31 |
( $lhs:expr, $head:literal | $($tail:tt)* ) => { |
| 32 |
$lhs.eq_ignore_ascii_case($head) || multi_eq_ignore_ascii_case!($lhs, $($tail)*) |
| 33 |
}; |
| 34 |
} |
| 35 |
|
| 36 |
#[cfg(test)] |
| 37 |
mod tests { |
| 38 |
|
| 39 |
#[test] |
| 40 |
fn test_multi_eq_ignore_ascii_case() { |
| 41 |
assert!( |
| 42 |
multi_eq_ignore_ascii_case!("test", "test"), |
| 43 |
"single comparison should succeed" |
| 44 |
); |
| 45 |
assert!( |
| 46 |
multi_eq_ignore_ascii_case!("test", "a" | "test"), |
| 47 |
"double comparison should succeed" |
| 48 |
); |
| 49 |
assert!( |
| 50 |
multi_eq_ignore_ascii_case!("test", "a" | "b" | "test"), |
| 51 |
"multi comparison should succeed" |
| 52 |
); |
| 53 |
|
| 54 |
assert!( |
| 55 |
!multi_eq_ignore_ascii_case!("test", "a"), |
| 56 |
"single non-matching should fail" |
| 57 |
); |
| 58 |
assert!( |
| 59 |
!multi_eq_ignore_ascii_case!("test", "a" | "b"), |
| 60 |
"double non-matching should fail" |
| 61 |
); |
| 62 |
assert!( |
| 63 |
!multi_eq_ignore_ascii_case!("test", "a" | "b" | "c"), |
| 64 |
"multi non-matching should fail" |
| 65 |
); |
| 66 |
} |
| 67 |
} |