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

veeso / tui-realm-stdlib / 15137208932

20 May 2025 12:13PM UTC coverage: 67.595% (-0.7%) from 68.289%
15137208932

push

github

web-flow
Fix clippy lints, apply some pedantic fixes and general small improvements (#32)

* style(examples): directly have values as a type instead of casting

* style(examples/utils): ignore unused warnings

* style: run clippy auto fix

and remove redundant tests from "label"

* style: apply some clippy pedantic auto fixes

* test: set specific strings for "should_panic"

So that other panics are catched as failed tests.

* style(bar_chart): remove casting to "u64" when "usize" is directly provided and needed

* refactor(table): move making rows to own function

To appease "clippy::too_many_lines" and slightly reduce nesting.

* style: add "#[must_use]" where applicable

To hint not to forget a value.

104 of 192 new or added lines in 19 files covered. (54.17%)

12 existing lines in 2 files now uncovered.

2985 of 4416 relevant lines covered (67.6%)

1.66 hits per line

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

48.86
/src/components/span.rs
1
//! ## Span
2
//!
3
//! `Span` represents a read-only text component without any container, but with the possibility to define multiple text parts.
4
//! The main difference with `Label` is that the Span allows different styles inside the same component for the texsts.
5

6
use tuirealm::command::{Cmd, CmdResult};
7
use tuirealm::props::{
8
    Alignment, AttrValue, Attribute, Color, PropPayload, PropValue, Props, Style, TextModifiers,
9
    TextSpan,
10
};
11
use tuirealm::ratatui::text::Line;
12
use tuirealm::ratatui::{
13
    layout::Rect,
14
    text::{Span as TuiSpan, Text},
15
    widgets::Paragraph,
16
};
17
use tuirealm::{Frame, MockComponent, State};
18

19
// -- Component
20

21
/// ## Span
22
///
23
/// represents a read-only text component without any container, but with multy-style text parts
24
#[derive(Default)]
25
#[must_use]
26
pub struct Span {
27
    props: Props,
28
}
29

30
impl Span {
31
    pub fn foreground(mut self, fg: Color) -> Self {
1✔
32
        self.attr(Attribute::Foreground, AttrValue::Color(fg));
1✔
33
        self
1✔
34
    }
1✔
35

36
    pub fn background(mut self, bg: Color) -> Self {
1✔
37
        self.attr(Attribute::Background, AttrValue::Color(bg));
1✔
38
        self
1✔
39
    }
1✔
40

41
    pub fn modifiers(mut self, m: TextModifiers) -> Self {
1✔
42
        self.attr(Attribute::TextProps, AttrValue::TextModifiers(m));
1✔
43
        self
1✔
44
    }
1✔
45

46
    pub fn alignment(mut self, a: Alignment) -> Self {
1✔
47
        self.attr(Attribute::Alignment, AttrValue::Alignment(a));
1✔
48
        self
1✔
49
    }
1✔
50

51
    pub fn spans(mut self, s: &[TextSpan]) -> Self {
1✔
52
        self.attr(
1✔
53
            Attribute::Text,
1✔
54
            AttrValue::Payload(PropPayload::Vec(
1✔
55
                s.iter().cloned().map(PropValue::TextSpan).collect(),
1✔
56
            )),
1✔
57
        );
58
        self
1✔
59
    }
1✔
60
}
61

62
impl MockComponent for Span {
63
    fn view(&mut self, render: &mut Frame, area: Rect) {
×
64
        // Make a Span
65
        if self.props.get_or(Attribute::Display, AttrValue::Flag(true)) == AttrValue::Flag(true) {
×
66
            // Make text
67
            let foreground = self
×
68
                .props
×
69
                .get_or(Attribute::Foreground, AttrValue::Color(Color::Reset))
×
70
                .unwrap_color();
×
71
            let background = self
×
72
                .props
×
73
                .get_or(Attribute::Background, AttrValue::Color(Color::Reset))
×
74
                .unwrap_color();
×
75
            // binding required as "spans" is a reference and otherwise would not live long enough
76
            let payload = self
×
77
                .props
×
78
                .get_ref(Attribute::Text)
×
79
                .and_then(|x| x.as_payload());
×
80
            let spans: Vec<TuiSpan> = match payload {
×
81
                Some(PropPayload::Vec(ref spans)) => spans
×
82
                    .iter()
×
83
                    // this will skip any "PropValue" that is not a "TextSpan", instead of panicing
NEW
84
                    .filter_map(|x| x.as_text_span())
×
85
                    .map(|x| {
×
86
                        // Keep colors and modifiers, or use default
87
                        let (fg, bg, modifiers) =
×
88
                            crate::utils::use_or_default_styles(&self.props, x);
×
89
                        TuiSpan::styled(
×
90
                            &x.content,
×
91
                            Style::default().add_modifier(modifiers).fg(fg).bg(bg),
×
92
                        )
93
                    })
×
94
                    .collect(),
×
95
                _ => Vec::new(),
×
96
            };
97
            let text: Text = Text::from(Line::from(spans));
×
98
            // Text properties
99
            let alignment: Alignment = self
×
100
                .props
×
101
                .get_or(Attribute::Alignment, AttrValue::Alignment(Alignment::Left))
×
102
                .unwrap_alignment();
×
103
            render.render_widget(
×
104
                Paragraph::new(text)
×
105
                    .alignment(alignment)
×
106
                    .style(Style::default().bg(background).fg(foreground)),
×
107
                area,
×
108
            );
109
        }
×
110
    }
×
111

112
    fn query(&self, attr: Attribute) -> Option<AttrValue> {
×
113
        self.props.get(attr)
×
114
    }
×
115

116
    fn attr(&mut self, attr: Attribute, value: AttrValue) {
5✔
117
        self.props.set(attr, value);
5✔
118
    }
5✔
119

120
    fn state(&self) -> State {
1✔
121
        State::None
1✔
122
    }
1✔
123

124
    fn perform(&mut self, _cmd: Cmd) -> CmdResult {
×
125
        CmdResult::None
×
126
    }
×
127
}
128

129
#[cfg(test)]
130
mod tests {
131

132
    use super::*;
133

134
    use pretty_assertions::assert_eq;
135

136
    #[test]
137
    fn test_components_span() {
1✔
138
        let component = Span::default()
1✔
139
            .background(Color::Blue)
1✔
140
            .foreground(Color::Red)
1✔
141
            .modifiers(TextModifiers::BOLD)
1✔
142
            .alignment(Alignment::Center)
1✔
143
            .spans(&[
1✔
144
                TextSpan::from("Press "),
1✔
145
                TextSpan::from("<ESC>").fg(Color::Cyan).bold(),
1✔
146
                TextSpan::from(" to quit"),
1✔
147
            ]);
1✔
148
        // Get value
149
        assert_eq!(component.state(), State::None);
1✔
150
    }
1✔
151
}
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