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
#![warn(clippy::pedantic)]
#![warn(missing_docs)]
#![doc = include_str!("../README.md")]

pub mod prelude {
    //! `use quork::prelude::*` To include common helpful items

    cfg_if::cfg_if! {
        if #[cfg(feature = "traits")] {
            pub use crate::traits::flip::*;
            pub use crate::traits::lock::*;
            pub use crate::traits::truthy::*;
            pub use crate::traits::list::*;
        }
    }

    #[cfg(feature = "macros")]
    pub use crate::macros::*;

    #[cfg(feature = "root")]
    pub use crate::root::is_root;
}

#[cfg(windows)]
pub mod win;

#[cfg(unix)]
pub mod unix;

#[cfg(feature = "macros")]
pub mod macros;

#[cfg(feature = "traits")]
pub mod traits;

#[cfg(feature = "network")]
pub mod network;

cfg_if::cfg_if! {
    if #[cfg(feature = "root")] {
        pub mod root;
    }
}

/// Truncation helpers for truncating strings when formatting
pub mod truncate {
    use std::fmt;

    pub use crate::traits::truncate::Truncation;

    #[derive(Debug)]
    #[must_use]
    /// A wrapper that truncates its contents when used.
    ///
    /// This should be used through the [`fmt::Display`] trait,
    /// in [`println!`] and other formatting circumstances
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use quork::truncate::Truncate;
    /// let name = Truncate::new("Juliette Cordor", 8);
    ///
    /// assert_eq!(name.to_string(), "Juliette")
    /// ```
    pub struct Truncate<T> {
        length: usize,
        data: T,
        suffix: Option<String>,
    }

    impl<T> Truncate<T> {
        /// Construct a new [`Truncate`] from the provided data and length
        pub fn new(data: T, length: usize) -> Self {
            Self {
                length,
                data,
                suffix: None,
            }
        }

        /// Add a suffix to the provided [`Truncate`], to print after the truncation
        ///
        /// # Examples
        ///
        /// ```rust
        /// # use quork::truncate::Truncate;
        /// let mut name = Truncate::new("Juliette Cordor", 8).with_suffix("...");
        ///
        /// assert_eq!(name.to_string(), "Julie...")
        /// ```
        pub fn with_suffix(self, suffix: impl fmt::Display) -> Self {
            Self {
                suffix: Some(suffix.to_string()),
                ..self
            }
        }
    }

    impl<T: fmt::Display> fmt::Display for Truncate<T> {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            // TODO: Remove string allocation here?
            let truncated = &self
                .data
                .to_string()
                .chars()
                .take(if let Some(ref suffix) = self.suffix {
                    // Account for length of suffix
                    self.length - suffix.len()
                } else {
                    self.length
                })
                .collect::<String>();

            truncated.fmt(f)?;

            if let Some(ref suffix) = self.suffix {
                suffix.fmt(f)?;
            }

            Ok(())
        }
    }

    #[must_use]
    #[deprecated = "This is no longer used internally, and was never intended to be used externally"]
    /// A wrapper over a writer that truncates the output to a certain length
    ///
    /// Primarily intended to be used through the [`Truncate`] struct
    pub struct TruncatedFormatter<'a> {
        remaining: usize,
        writer: &'a mut fmt::Formatter<'a>,
    }

    #[allow(deprecated)]
    impl<'a> TruncatedFormatter<'a> {
        /// Construct a new [`TruncatedFormatter`] from the provided writer and length
        pub fn new(writer: &'a mut fmt::Formatter<'a>, length: usize) -> Self {
            Self {
                remaining: length,
                writer,
            }
        }
    }

    #[allow(deprecated)]
    impl<'a> fmt::Write for TruncatedFormatter<'a> {
        fn write_str(&mut self, s: &str) -> fmt::Result {
            if self.remaining < s.len() {
                self.writer.write_str(&s[0..self.remaining])?;
                self.remaining = 0;
                Ok(())
            } else {
                self.remaining -= s.len();
                self.writer.write_str(s)
            }
        }
    }

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

        #[test]
        fn test_with_suffix() {
            let suffixed = Truncate::new("Hello, World!", 8).with_suffix("...");

            assert_eq!("Hello...", suffixed.to_string());
        }

        #[test]
        fn test_with_padding() {
            let truncated = Truncate::new("Hello, World!", 5);

            let padded = format!("{truncated:<10}");

            assert_eq!("Hello     ", padded);
        }

        // This used to crash, this test exists to ensure that doesn't happen again
        #[test]
        fn test_longer_length_than_string() {
            let truncated = Truncate::new("Hey", 15);

            assert_eq!("Hey", truncated.to_string());
        }
    }
}