Skip to main content

ansiq_widgets/
line_gauge.rs

1use ansiq_core::{Element, ElementKind, Layout, Length, Line, LineGaugeProps, Style};
2
3pub struct LineGauge<Message = ()> {
4    element: Element<Message>,
5}
6
7impl<Message> Default for LineGauge<Message> {
8    fn default() -> Self {
9        Self {
10            element: Element::new(ElementKind::LineGauge(LineGaugeProps {
11                block: None,
12                ratio: 0.0,
13                label: None,
14                filled_symbol: ansiq_core::symbols::line::HORIZONTAL.to_string(),
15                unfilled_symbol: ansiq_core::symbols::line::HORIZONTAL.to_string(),
16                filled_style: Style::default(),
17                unfilled_style: Style::default(),
18            }))
19            .with_layout(Layout {
20                width: Length::Fill,
21                height: Length::Auto,
22            }),
23        }
24    }
25}
26
27impl<Message> LineGauge<Message> {
28    pub fn new() -> Self {
29        Self::default()
30    }
31
32    pub fn block(mut self, block: crate::Block<Message>) -> Self {
33        if let ElementKind::LineGauge(props) = &mut self.element.kind {
34            props.block = Some(block.into_frame());
35        }
36        self
37    }
38
39    pub fn percent(self, percent: u16) -> Self {
40        assert!(
41            percent <= 100,
42            "Percentage should be between 0 and 100 inclusively."
43        );
44        self.ratio(f64::from(percent) / 100.0)
45    }
46
47    pub fn ratio(mut self, ratio: f64) -> Self {
48        assert!(
49            (0.0..=1.0).contains(&ratio),
50            "Ratio should be between 0 and 1 inclusively."
51        );
52        if let ElementKind::LineGauge(props) = &mut self.element.kind {
53            props.ratio = ratio;
54        }
55        self
56    }
57
58    pub fn label<T>(mut self, label: T) -> Self
59    where
60        T: Into<Line>,
61    {
62        if let ElementKind::LineGauge(props) = &mut self.element.kind {
63            props.label = Some(label.into());
64        }
65        self
66    }
67
68    pub fn line_set(mut self, set: ansiq_core::symbols::line::Set) -> Self {
69        if let ElementKind::LineGauge(props) = &mut self.element.kind {
70            props.filled_symbol = set.horizontal.to_string();
71            props.unfilled_symbol = set.horizontal.to_string();
72        }
73        self
74    }
75
76    pub fn filled_symbol(mut self, symbol: impl Into<String>) -> Self {
77        if let ElementKind::LineGauge(props) = &mut self.element.kind {
78            props.filled_symbol = symbol.into();
79        }
80        self
81    }
82
83    pub fn unfilled_symbol(mut self, symbol: impl Into<String>) -> Self {
84        if let ElementKind::LineGauge(props) = &mut self.element.kind {
85            props.unfilled_symbol = symbol.into();
86        }
87        self
88    }
89
90    pub fn filled_style<S: Into<Style>>(mut self, style: S) -> Self {
91        if let ElementKind::LineGauge(props) = &mut self.element.kind {
92            props.filled_style = style.into();
93        }
94        self
95    }
96
97    pub fn unfilled_style<S: Into<Style>>(mut self, style: S) -> Self {
98        if let ElementKind::LineGauge(props) = &mut self.element.kind {
99            props.unfilled_style = style.into();
100        }
101        self
102    }
103
104    pub fn layout(mut self, layout: Layout) -> Self {
105        self.element.layout = layout;
106        self
107    }
108
109    pub fn style<S: Into<Style>>(mut self, style: S) -> Self {
110        self.element.style = style.into();
111        self
112    }
113
114    pub fn build(self) -> Element<Message> {
115        self.element
116    }
117}