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
//! Truncate and ellipse strings in a human-friendly way.
//!
//! Uses unicode extended grapheme clusters.
//!
//! # Example
//! ```
//! use ellipse::Ellipse;
//!
//! let input = "🇩🇪🇬🇧🇮🇹🇫🇷";
//! assert_eq!(&input.truncate_ellipse(2), "🇩🇪🇬🇧...");
//! ```

use std::borrow::Cow;
use unicode_segmentation::UnicodeSegmentation;

/// Truncate and ellipse strings in a human-friendly way.
///
/// # Example
/// ```
/// use ellipse::Ellipse;
///
/// let input = "🇩🇪🇬🇧🇮🇹🇫🇷";
/// assert_eq!(&input.truncate_ellipse(2), "🇩🇪🇬🇧...");
/// ```
pub trait Ellipse {
    type Output;

    /// Truncate to a length of `len` extended grapheme clusters and place the given
    /// ellipse string at the end when truncating.
    ///
    /// Truncating to a length of 0 will yield the empty element without an
    /// attached ellipsis.
    fn truncate_ellipse_with(&self, len: usize, ellipse: &str) -> Self::Output;

    /// Truncate to a length of `len` extended grapheme clusters and add `...` at
    /// the end of the string when truncating.
    ///
    /// Truncating to a length of 0 will yield the empty element without an
    /// attached ellipsis.
    fn truncate_ellipse(&self, len: usize) -> Self::Output {
        self.truncate_ellipse_with(len, "...")
    }
}

impl<'a> Ellipse for &'a str {
    type Output = Cow<'a, str>;

    fn truncate_ellipse_with(&self, len: usize, ellipse: &str) -> Self::Output {
        if self.graphemes(true).count() <= len {
            return Cow::Borrowed(self);
        } else if len == 0 {
            return Cow::Borrowed("");
        }

        let result = self
            .graphemes(true)
            .take(len)
            .chain(ellipse.graphemes(true))
            .collect();
        Cow::Owned(result)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn smoke() {
        assert_eq!(&"Hello, World!".truncate_ellipse(3), "Hel...",);
    }

    #[test]
    fn ellipse_with() {
        assert_eq!(&"Hello, World!".truncate_ellipse_with(3, "---"), "Hel---");
    }

    #[test]
    fn truncate_to_empty() {
        assert_eq!(&"Hello, World!".truncate_ellipse(0), "");
    }

    #[test]
    fn weird_chars() {
        assert_eq!(
            &"🇩🇪🇬🇧🇮🇹🇫🇷".truncate_ellipse(2),
            "🇩🇪🇬🇧..."
        );
    }
}