• 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

84.75
/src/styled/string/lazy.rs
1
// Copyright (c) 2022 Tony Barbitta
2
//
3
// This Source Code Form is subject to the terms of the Mozilla Public
4
// License, v. 2.0. If a copy of the MPL was not distributed with this
5
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6

7
use once_cell::sync::OnceCell;
8

9
use crate::{Ansi, IntoAnsi};
10

11
/// Fourth iteration of a styled string type. This one uses `once_cell::Lazy` to calculate
12
/// the styled text only when needed. Unfortunately this does mean that the text and the
13
/// style would not able to be changed after initialization...or does it...?
14
pub struct LazyPrettyString(String, Ansi, OnceCell<String>);
15

16
impl LazyPrettyString {
17
    /// Creates a new [`LazyPrettyString`] with the given `text` and `style`.
18
    pub fn new(text: impl std::fmt::Display, style: impl IntoAnsi) -> Self {
1✔
19
        let text = text.to_string();
1✔
20
        let style = style.into_ansi();
1✔
21
        Self(text, style, OnceCell::new())
1✔
22
    }
1✔
23

24
    /// Get the "raw" (aka unstyled / original) text.
25
    #[must_use]
26
    pub fn raw(&self) -> &str {
3✔
27
        self.0.as_ref()
3✔
28
    }
3✔
29

30
    /// Update the text for this [`LazyPrettyString`] in place.
31
    pub fn modify_text(&mut self, f: impl FnOnce(&mut String)) {
1✔
32
        f(&mut self.0);
1✔
33
        self.2 = OnceCell::new();
1✔
34
    }
1✔
35

36
    /// Sets the text to `text`.
37
    pub fn set_text(&mut self, text: impl std::fmt::Display) {
1✔
38
        self.0 = text.to_string();
1✔
39
        self.2 = OnceCell::new();
1✔
40
    }
1✔
41

42
    /// Get the [`Ansi`] styling applied to this text.
43
    #[must_use]
44
    pub fn style(&self) -> &Ansi {
×
45
        &self.1
×
46
    }
×
47

48
    /// Modify the styling applied to this text using the given closure.
49
    pub fn modify_style<F: FnMut(&mut Ansi)>(&mut self, mut f: F) {
1✔
50
        f(&mut self.1);
1✔
51
        self.2 = OnceCell::new();
1✔
52
    }
1✔
53

54
    /// Sets the styling applied to this text to the given `style`.
55
    pub fn set_style(&mut self, style: impl IntoAnsi) {
1✔
56
        self.1 = style.into_ansi();
1✔
57
        self.2 = OnceCell::new();
1✔
58
    }
1✔
59

60
    /// Get the formatted value of this [`LazyPrettyString`].
61
    #[must_use]
62
    pub fn value(&self) -> &str {
3✔
63
        self.2
3✔
64
            .get_or_init(|| self.1.paint_text(self.raw()))
3✔
65
            .as_str()
3✔
66
    }
3✔
67

68
    /// Gets the length of the ***original text***, i.e. the VISIBLE length.
69
    #[must_use]
70
    pub fn len(&self) -> usize {
×
71
        self.0.len()
×
72
    }
×
73

74
    /// Checks if the original / **visible** text is empty
75
    #[must_use]
76
    pub fn is_empty(&self) -> bool {
×
77
        self.0.is_empty()
×
78
    }
×
79
}
80

81
impl std::fmt::Display for LazyPrettyString {
82
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3✔
83
        write!(f, "{}", self.value())
3✔
84
    }
3✔
85
}
86

87
#[cfg(test)]
88
mod tests {
89
    use super::*;
90

91
    #[test]
1✔
92
    fn mutability() {
1✔
93
        let mut lps = LazyPrettyString::new("Hello, Red!", Ansi::from_fg((255, 0, 0)));
1✔
94
        println!("{lps}");
1✔
95
        lps.modify_style(|ansi| *ansi = Ansi::from_fg((0, 255, 0)));
1✔
96
        lps.modify_text(|s| *s = "Hello, Green!".to_string());
1✔
97
        println!("{lps}");
1✔
98
        lps.set_style(Ansi::from_fg((0, 0, 255)));
1✔
99
        lps.set_text("Hello, Blue!");
1✔
100
        println!("{lps}");
1✔
101
    }
1✔
102

103
    #[test]
1✔
104
    fn sizeof() {
1✔
105
        println!(
1✔
106
            "Sizeof LazyPrettyString = {}",
1✔
107
            std::mem::size_of::<LazyPrettyString>()
1✔
108
        );
1✔
109
    }
1✔
110
}
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