1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
use yew::html::IntoPropValue;
use yew::prelude::*;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TruncateContent {
Default(String),
Middle(String, String),
Start(String),
}
impl TruncateContent {
pub fn middle<S: Into<String>, E: Into<String>>(start: S, end: E) -> Self {
Self::Middle(start.into(), end.into())
}
pub fn start<S: Into<String>>(start: S) -> Self {
Self::Start(start.into())
}
}
impl From<String> for TruncateContent {
fn from(value: String) -> Self {
Self::Default(value)
}
}
impl From<&str> for TruncateContent {
fn from(value: &str) -> Self {
Self::Default(value.to_string())
}
}
impl IntoPropValue<TruncateContent> for String {
fn into_prop_value(self) -> TruncateContent {
TruncateContent::Default(self)
}
}
impl IntoPropValue<TruncateContent> for &str {
fn into_prop_value(self) -> TruncateContent {
TruncateContent::Default(self.to_string())
}
}
/// Helps creating content for [`Truncate`].
pub trait IntoTruncateContent {
/// Truncate at the start of the content.
fn truncate_start(self) -> TruncateContent;
/// Truncate `num` characters before the end of the string.
fn truncate_before(self, num: usize) -> TruncateContent;
}
impl<T: ToString> IntoTruncateContent for T {
fn truncate_start(self) -> TruncateContent {
TruncateContent::Start(self.to_string())
}
/// This function is supposed to truncate `num` characters before the end of the string.
///
/// ## Bytes, Code Points, and Grapheme Clusters
///
/// However, what it actually does is to truncate the string at the next Unicode code point,
/// after `num` bytes (not characters). This is quick and should work reasonably well with
/// the Latin 1 character set (or, UTF-8 characters which are represented by a single byte).
///
/// Given a string with multi-byte code points, or even grapheme clusters (user-perceived
/// characters, which may consists of multiple Unicode code points), this will split at the
/// wrong location.
///
/// It will still split, and not skip any data. But it might lead to an unexpected (shorter)
/// end section.
///
/// What about an actual correct implementation? That would be possible by using an additional
/// dependency. It would also need to count all code points and grapheme clusters from the
/// start of the string. The question is: is that worth it? Maybe, maybe not!?
fn truncate_before(self, num: usize) -> TruncateContent {
let s = self.to_string();
let len = s.len();
if num == 0 {
return TruncateContent::Default(s);
}
if num > len {
return TruncateContent::Start(s);
}
let mut end = len - num;
loop {
if end == 0 {
return TruncateContent::Start(s);
}
if s.is_char_boundary(end) {
break;
}
// we can't get negative, as we exit the loop when end == 0
end -= 1;
}
let (start, end) = s.split_at(end);
TruncateContent::Middle(start.to_string(), end.to_string())
}
}
/// Properties for [`Truncate`].
#[derive(PartialEq, Properties)]
pub struct TruncateProperties {
pub content: TruncateContent,
#[prop_or_default]
pub id: Option<AttrValue>,
#[prop_or_default]
pub style: Option<AttrValue>,
#[prop_or_default]
pub class: Classes,
}
/// Truncate component
///
/// A **truncate** is a tool used to shorten numeric and non-numeric character strings, typically when the string overflows its container.
///
/// See: <https://www.patternfly.org/components/truncate>
///
/// ## Properties
///
/// Defined by [`TruncateProperties`].
#[function_component(Truncate)]
pub fn truncate(props: &TruncateProperties) -> Html {
let mut class = classes!("pf-v5-c-truncate");
class.extend(&props.class);
html!(
<span
{class}
style={props.style.clone()}
id={props.id.clone()}
>
{
match &props.content {
TruncateContent::Default(value) => html!(
<span class="pf-v5-c-truncate__start">{ &value }</span>
),
TruncateContent::Middle(start, end) => html!(<>
<span class="pf-v5-c-truncate__start">{ &start }</span>
<span class="pf-v5-c-truncate__end">{ &end }</span>
</>),
TruncateContent::Start(value) => html!(<>
<span class="pf-v5-c-truncate__end">{ &value }{ "\u{200E}" }</span>
</>),
}
}
</span>
)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
pub fn test_mid_basic() {
let content = "0123456789".truncate_before(5);
assert_eq!(
TruncateContent::Middle("01234".to_string(), "56789".to_string()),
content
);
}
#[test]
pub fn test_mid_empty() {
let content = "".truncate_before(5);
assert_eq!(TruncateContent::Start("".to_string()), content);
}
#[test]
pub fn test_mid_over() {
let content = "0123456789".truncate_before(20);
assert_eq!(TruncateContent::Start("0123456789".to_string()), content);
}
#[test]
pub fn test_mid_zero() {
let content = "0123456789".truncate_before(0);
assert_eq!(TruncateContent::Default("0123456789".to_string()), content);
}
}