• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tonyb983 / ansirs / 5689906129

pending completion
5689906129

Pull #5

github

web-flow
Merge e3fde201b into 2565c124a
Pull Request #5: Update Rust crate serde to 1.0.177

2261 of 2777 relevant lines covered (81.42%)

22.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

20.0
/src/styled/string/refstr.rs
1
use crate::{Ansi, IntoAnsi};
2

3
/// This implementation of a styled / formatted string uses a reference to the original text so as to not
4
/// allocate a new [`String`] for each instance. This *does* mean that the `value` call must be calculated
5
/// each time it is called.
6
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
×
7
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
×
8
pub struct PrettyStr<'original>(Option<Ansi>, &'original str);
9

10
impl<'original> PrettyStr<'original> {
11
    /// Takes a plain string `text` and a style `format`.
12
    pub fn new(text: &'original str, format: impl IntoAnsi) -> Self {
×
13
        let style = format.into_ansi();
×
14
        let style = if style.is_default() {
×
15
            None
×
16
        } else {
17
            Some(style)
×
18
        };
19

20
        Self(style, text)
×
21
    }
×
22

23
    /// Get the "raw" (aka unstyled / original) text.
24
    #[must_use]
25
    pub fn raw(&self) -> &str {
×
26
        self.1
×
27
    }
×
28

29
    /// Get the [`Ansi`] styling applied to this text.
30
    #[must_use]
31
    pub fn style(&self) -> Option<&Ansi> {
×
32
        self.0.as_ref()
×
33
    }
×
34

35
    /// Get the formatted text of this [`PrettyStr`].
36
    #[must_use]
37
    pub fn value(&self) -> String {
×
38
        self.0.unwrap_or_default().paint_text(self.1)
×
39
    }
×
40

41
    /// Gets the length of the ***original text***, i.e. the VISIBLE length.
42
    #[must_use]
43
    pub fn len(&self) -> usize {
×
44
        self.1.len()
×
45
    }
×
46

47
    /// Checks if the original / **visible** text is empty
48
    #[must_use]
49
    pub fn is_empty(&self) -> bool {
×
50
        self.1.is_empty()
×
51
    }
×
52

53
    /// Modify the styling applied to this text using the given closure.
54
    pub fn modify_style<F: FnMut(Option<&Ansi>) -> Option<Ansi>>(&mut self, mut f: F) {
×
55
        let new = f(self.0.as_ref());
×
56
        self.0 = new;
×
57
    }
×
58
}
59

60
#[cfg(test)]
61
mod tests {
62
    use super::*;
63

64
    #[test]
1✔
65
    fn sizeof() {
1✔
66
        println!(
1✔
67
            "Size of PrettyStr: {}",
1✔
68
            std::mem::size_of::<PrettyStr<'_>>()
1✔
69
        );
1✔
70
    }
1✔
71
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc