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

veeso / tui-realm-stdlib / 22147789116

09 Feb 2026 04:20PM UTC coverage: 77.303% (+4.8%) from 72.482%
22147789116

push

github

hasezoey
refactor(table): switch to use "CommonProps"

18 of 90 new or added lines in 1 file covered. (20.0%)

405 existing lines in 20 files now uncovered.

3944 of 5102 relevant lines covered (77.3%)

4.92 hits per line

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

48.94
/src/components/container.rs
1
//! ## Container
2
//!
3
//! `Container` represents an empty container where you can put other components into it.
4
//! It will render components based on how you defined the layout.
5
//! The way it updates properties is usually assigning the attributes to all the children components, but
6
//! when defining the component you can override these behaviours implementing `attr()` by yourself.
7
//! By default it will forward `Commands' to all the children and will return a `CmdResult::Batch` with all the results.
8

9
use tuirealm::command::{Cmd, CmdResult};
10
use tuirealm::props::{AttrValue, Attribute, Borders, Color, Layout, Props, Style, Title};
11
use tuirealm::ratatui::layout::Rect;
12
use tuirealm::{Frame, MockComponent, State};
13

14
use crate::prop_ext::CommonProps;
15

16
// -- Component
17

18
/// ## Container
19
///
20
/// represents a read-only text component without any container.
21
#[must_use]
22
pub struct Container {
23
    common: CommonProps,
24
    props: Props,
25
    /// Container children
26
    pub children: Vec<Box<dyn MockComponent>>,
27
}
28

29
impl Default for Container {
30
    fn default() -> Self {
2✔
31
        Self {
2✔
32
            common: CommonProps {
2✔
33
                border: Some(Borders::default()),
2✔
34
                ..CommonProps::default()
2✔
35
            },
2✔
36
            props: Props::default(),
2✔
37
            children: Vec::new(),
2✔
38
        }
2✔
39
    }
2✔
40
}
41

42
impl Container {
43
    /// Set the main foreground color. This may get overwritten by individual text styles.
44
    pub fn foreground(mut self, fg: Color) -> Self {
2✔
45
        self.attr(Attribute::Foreground, AttrValue::Color(fg));
2✔
46
        self
2✔
47
    }
2✔
48

49
    /// Set the main background color. This may get overwritten by individual text styles.
50
    pub fn background(mut self, bg: Color) -> Self {
2✔
51
        self.attr(Attribute::Background, AttrValue::Color(bg));
2✔
52
        self
2✔
53
    }
2✔
54

55
    /// Set the main style. This may get overwritten by individual text styles.
56
    ///
57
    /// This option will overwrite any previous [`foreground`](Self::foreground), [`background`](Self::background) and [`modifiers`](Self::modifiers)!
UNCOV
58
    pub fn style(mut self, style: Style) -> Self {
×
UNCOV
59
        self.attr(Attribute::Style, AttrValue::Style(style));
×
60
        self
×
UNCOV
61
    }
×
62

63
    /// Set a custom style for the border when the component is unfocused.
64
    pub fn inactive(mut self, s: Style) -> Self {
×
65
        self.attr(Attribute::FocusStyle, AttrValue::Style(s));
×
66
        self
×
67
    }
×
68

69
    /// Add a border to the component.
70
    pub fn borders(mut self, b: Borders) -> Self {
×
71
        self.attr(Attribute::Borders, AttrValue::Borders(b));
×
72
        self
×
UNCOV
73
    }
×
74

75
    /// Add a title to the component.
76
    pub fn title<T: Into<Title>>(mut self, title: T) -> Self {
2✔
77
        self.attr(Attribute::Title, AttrValue::Title(title.into()));
2✔
78
        self
2✔
79
    }
2✔
80

81
    /// Set a ratatui Layout to use for all the child components.
82
    ///
83
    /// If this is unset, nothing gets drawn!
UNCOV
84
    pub fn layout(mut self, layout: Layout) -> Self {
×
85
        self.attr(Attribute::Layout, AttrValue::Layout(layout));
×
86
        self
×
87
    }
×
88

89
    /// Set the children Components this container contains.
90
    pub fn children(mut self, children: Vec<Box<dyn MockComponent>>) -> Self {
×
91
        self.children = children;
×
UNCOV
92
        self
×
UNCOV
93
    }
×
94
}
95

96
impl MockComponent for Container {
UNCOV
97
    fn view(&mut self, render: &mut Frame, mut area: Rect) {
×
UNCOV
98
        if !self.common.display {
×
UNCOV
99
            return;
×
UNCOV
100
        }
×
101

UNCOV
102
        if let Some(block) = self.common.get_block() {
×
UNCOV
103
            let inner = block.inner(area);
×
UNCOV
104
            // Render block
×
105
            render.render_widget(block, area);
×
UNCOV
106
            area = inner;
×
107
        }
×
108

109
        // Render children
UNCOV
110
        if let Some(layout) = self.props.get(Attribute::Layout).map(|x| x.unwrap_layout()) {
×
111
            // make chunks
UNCOV
112
            let chunks = layout.chunks(area);
×
113
            // iter chunks
UNCOV
114
            for (i, chunk) in chunks.into_iter().enumerate() {
×
UNCOV
115
                if let Some(child) = self.children.get_mut(i) {
×
UNCOV
116
                    child.view(render, chunk);
×
UNCOV
117
                }
×
118
            }
UNCOV
119
        }
×
UNCOV
120
    }
×
121

UNCOV
122
    fn query(&self, attr: Attribute) -> Option<AttrValue> {
×
UNCOV
123
        if let Some(value) = self.common.get(attr) {
×
UNCOV
124
            return Some(value);
×
UNCOV
125
        }
×
126

UNCOV
127
        self.props.get(attr)
×
UNCOV
128
    }
×
129

130
    fn attr(&mut self, attr: Attribute, value: AttrValue) {
6✔
131
        if let Some(value) = self.common.set(attr, value.clone()) {
6✔
UNCOV
132
            self.props.set(attr, value);
×
133
        }
6✔
134

135
        // dont patch borders and title to all children
136
        if matches!(attr, Attribute::Borders | Attribute::Title) {
6✔
137
            return;
2✔
138
        }
4✔
139

2✔
140
        // TODO: how do you control patching things to children? For example change the title / border of a specific child?
2✔
141
        // Patch attribute to children
2✔
142
        self.children
4✔
143
            .iter_mut()
4✔
144
            .for_each(|x| x.attr(attr, value.clone()));
4✔
145
    }
6✔
146

147
    fn state(&self) -> State {
2✔
148
        State::None
2✔
149
    }
2✔
150

UNCOV
151
    fn perform(&mut self, cmd: Cmd) -> CmdResult {
×
152
        // TODO: send commands to a specific child?
153

154
        // Send command to children and return batch
UNCOV
155
        CmdResult::Batch(self.children.iter_mut().map(|x| x.perform(cmd)).collect())
×
UNCOV
156
    }
×
157
}
158

159
#[cfg(test)]
160
mod tests {
161

162
    use super::*;
163

164
    use pretty_assertions::assert_eq;
165
    use tuirealm::props::HorizontalAlignment;
166

167
    #[test]
168
    fn test_components_paragraph() {
2✔
169
        let component = Container::default()
2✔
170
            .background(Color::Blue)
2✔
171
            .foreground(Color::Red)
2✔
172
            .title(Title::from("title").alignment(HorizontalAlignment::Center));
2✔
173
        // Get value
1✔
174
        assert_eq!(component.state(), State::None);
2✔
175
    }
2✔
176
}
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