tui-realm-stdlib 4.0.0

Standard components library for tui-realm.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! A chart with bars

use std::collections::LinkedList;

use tuirealm::command::{Cmd, CmdResult, Direction, Position};
use tuirealm::component::Component;
use tuirealm::props::{
    AttrValue, Attribute, Borders, Color, PropPayload, PropValue, Props, QueryResult, Style,
    TextModifiers, Title,
};
use tuirealm::ratatui::Frame;
use tuirealm::ratatui::layout::Rect;
use tuirealm::ratatui::widgets::BarChart as TuiBarChart;
use tuirealm::state::State;

use super::props::{
    BAR_CHART_BARS_GAP, BAR_CHART_BARS_STYLE, BAR_CHART_LABEL_STYLE, BAR_CHART_MAX_BARS,
    BAR_CHART_VALUES_STYLE,
};
// -- Props
use crate::prop_ext::CommonProps;

// -- states

/// The state that needs to be kept for the [`BarChart`] Component.
#[derive(Default)]
pub struct BarChartStates {
    pub cursor: usize,
}

impl BarChartStates {
    /// Move cursor to the left.
    pub fn move_cursor_left(&mut self) {
        if self.cursor > 0 {
            self.cursor -= 1;
        }
    }

    /// Move cursor to the right.
    pub fn move_cursor_right(&mut self, data_len: usize) {
        if data_len > 0 && self.cursor + 1 < data_len {
            self.cursor += 1;
        }
    }

    /// Reset cursor to 0.
    pub fn reset_cursor(&mut self) {
        self.cursor = 0;
    }

    /// Move cursor to the end of the chart.
    pub fn cursor_at_end(&mut self, data_len: usize) {
        if data_len > 0 {
            self.cursor = data_len - 1;
        } else {
            self.cursor = 0;
        }
    }
}

// -- component

/// A component to display a chart with bars.
/// The bar chart can work both in "active" and "disabled" mode.
///
/// ## Disabled mode
///
/// When in disabled mode, the chart won't be interactive, so you won't be able to move through data using keys.
/// If you have more data than the maximum amount of bars that can be displayed, you'll have to update data to display the remaining entries.
///
/// ## Active mode
///
/// While in active mode (default) you can put as many entries as you wish. You can move with [`Cmd::Move`] or [`Cmd::GoTo`].
#[derive(Default)]
#[must_use]
pub struct BarChart {
    common: CommonProps,
    props: Props,
    pub states: BarChartStates,
}

impl BarChart {
    /// Set the main foreground color. This may get overwritten by individual text styles.
    pub fn foreground(mut self, fg: Color) -> Self {
        self.attr(Attribute::Foreground, AttrValue::Color(fg));
        self
    }

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

    /// Set the main text modifiers. This may get overwritten by individual text styles.
    pub fn modifiers(mut self, m: TextModifiers) -> Self {
        self.attr(Attribute::TextProps, AttrValue::TextModifiers(m));
        self
    }

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

    /// Add a border to the component.
    pub fn borders(mut self, b: Borders) -> Self {
        self.attr(Attribute::Borders, AttrValue::Borders(b));
        self
    }

    /// Add a title to the component.
    pub fn title<T: Into<Title>>(mut self, title: T) -> Self {
        self.attr(Attribute::Title, AttrValue::Title(title.into()));
        self
    }

    /// Set whether this component should appear "disabled" (or also known as "locked").
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.attr(Attribute::Disabled, AttrValue::Flag(disabled));
        self
    }

    /// Set a custom style for the border when the component is unfocused.
    pub fn inactive(mut self, s: Style) -> Self {
        self.attr(Attribute::UnfocusedBorderStyle, AttrValue::Style(s));
        self
    }

    /// Set the initial Dataset
    pub fn data(mut self, data: &[(&str, u64)]) -> Self {
        // TODO: allow data to be set in "BarGroup" types instead of plain "Vec"
        let mut list: LinkedList<PropPayload> = LinkedList::new();
        for (a, b) in data {
            list.push_back(PropPayload::Pair((
                PropValue::Str((*a).to_string()),
                PropValue::U64(*b),
            )));
        }
        self.attr(
            Attribute::Dataset,
            AttrValue::Payload(PropPayload::Linked(list)),
        );
        self
    }

    /// Set a custom gap between bars, see [`BarChart::bar_gap`].
    pub fn bar_gap(mut self, gap: u16) -> Self {
        self.attr(Attribute::Custom(BAR_CHART_BARS_GAP), AttrValue::Size(gap));
        self
    }

    /// Set a custom style for all bars.
    pub fn bar_style(mut self, s: Style) -> Self {
        self.attr(Attribute::Custom(BAR_CHART_BARS_STYLE), AttrValue::Style(s));
        self
    }

    /// Set a custom style for all bar labels.
    pub fn label_style(mut self, s: Style) -> Self {
        self.attr(
            Attribute::Custom(BAR_CHART_LABEL_STYLE),
            AttrValue::Style(s),
        );
        self
    }

    /// Set the max amount of bars to display.
    ///
    /// By default the data length.
    pub fn max_bars(mut self, l: usize) -> Self {
        self.attr(Attribute::Custom(BAR_CHART_MAX_BARS), AttrValue::Length(l));
        self
    }

    /// Set a custom style for all values.
    pub fn value_style(mut self, s: Style) -> Self {
        self.attr(
            Attribute::Custom(BAR_CHART_VALUES_STYLE),
            AttrValue::Style(s),
        );
        self
    }

    /// Set the width of each bar.
    pub fn width(mut self, w: u16) -> Self {
        self.attr(Attribute::Width, AttrValue::Size(w));
        self
    }

    fn is_disabled(&self) -> bool {
        self.props
            .get(Attribute::Disabled)
            .and_then(AttrValue::as_flag)
            .unwrap_or_default()
    }

    /// ### data_len
    ///
    /// Retrieve current data len from properties
    fn data_len(&self) -> usize {
        self.props
            .get(Attribute::Dataset)
            .and_then(AttrValue::as_payload)
            .and_then(PropPayload::as_linked)
            .map_or(0, |x| x.len())
    }

    fn get_data(&self, start: usize, len: usize) -> Vec<(String, u64)> {
        if let Some(PropPayload::Linked(list)) = self
            .props
            .get(Attribute::Dataset)
            .and_then(AttrValue::as_payload)
        {
            // Recalc len
            let len: usize = std::cmp::min(len, self.data_len() - start);
            // Prepare data storage
            let mut data: Vec<(String, u64)> = Vec::with_capacity(len);
            for (cursor, item) in list.iter().enumerate() {
                // If before start, continue
                if cursor < start {
                    continue;
                }
                // Push item
                if let PropPayload::Pair((PropValue::Str(label), PropValue::U64(value))) = item {
                    data.push((label.clone(), *value));
                }
                // Break
                if data.len() >= len {
                    break;
                }
            }

            data
        } else {
            Vec::new()
        }
    }
}

impl Component for BarChart {
    fn view(&mut self, render: &mut Frame, area: Rect) {
        if !self.common.display {
            return;
        }

        // Get max elements
        let data_max_len = self
            .props
            .get(Attribute::Custom(BAR_CHART_MAX_BARS))
            .and_then(AttrValue::as_length)
            .unwrap_or(self.data_len());
        // Get data
        let data = self.get_data(self.states.cursor, data_max_len);
        let data_ref: Vec<(&str, u64)> = data.iter().map(|x| (x.0.as_str(), x.1)).collect();
        // Create widget
        let mut widget: TuiBarChart = TuiBarChart::default()
            .style(self.common.style)
            .data(data_ref.as_slice());

        if let Some(block) = self.common.get_block() {
            widget = widget.block(block);
        }

        if let Some(gap) = self
            .props
            .get(Attribute::Custom(BAR_CHART_BARS_GAP))
            .and_then(AttrValue::as_size)
        {
            widget = widget.bar_gap(gap);
        }
        if let Some(width) = self
            .props
            .get(Attribute::Width)
            .and_then(AttrValue::as_size)
        {
            widget = widget.bar_width(width);
        }
        if let Some(style) = self
            .props
            .get(Attribute::Custom(BAR_CHART_BARS_STYLE))
            .and_then(AttrValue::as_style)
        {
            widget = widget.bar_style(style);
        }
        if let Some(style) = self
            .props
            .get(Attribute::Custom(BAR_CHART_LABEL_STYLE))
            .and_then(AttrValue::as_style)
        {
            widget = widget.label_style(style);
        }
        if let Some(style) = self
            .props
            .get(Attribute::Custom(BAR_CHART_VALUES_STYLE))
            .and_then(AttrValue::as_style)
        {
            widget = widget.value_style(style);
        }

        // Render
        render.render_widget(widget, area);
    }

    fn query<'a>(&'a self, attr: Attribute) -> Option<QueryResult<'a>> {
        if let Some(value) = self.common.get_for_query(attr) {
            return Some(value);
        }

        self.props.get_for_query(attr)
    }

    fn attr(&mut self, attr: Attribute, value: AttrValue) {
        if let Some(value) = self.common.set(attr, value) {
            self.props.set(attr, value);
        }
    }

    fn perform(&mut self, cmd: Cmd) -> CmdResult {
        if !self.is_disabled() {
            match cmd {
                Cmd::Move(Direction::Left) => {
                    self.states.move_cursor_left();
                }
                Cmd::Move(Direction::Right) => {
                    self.states.move_cursor_right(self.data_len());
                }
                Cmd::GoTo(Position::Begin) => {
                    self.states.reset_cursor();
                }
                Cmd::GoTo(Position::End) => {
                    self.states.cursor_at_end(self.data_len());
                }
                _ => return CmdResult::Invalid(cmd),
            }
            return CmdResult::Visual;
        }
        CmdResult::NoChange
    }

    fn state(&self) -> State {
        State::None
    }
}

#[cfg(test)]
mod test {

    use pretty_assertions::assert_eq;
    use tuirealm::props::HorizontalAlignment;

    use super::*;

    #[test]
    fn test_components_bar_chart_states() {
        let mut states: BarChartStates = BarChartStates::default();
        assert_eq!(states.cursor, 0);
        // Incr
        states.move_cursor_right(2);
        assert_eq!(states.cursor, 1);
        // At end
        states.move_cursor_right(2);
        assert_eq!(states.cursor, 1);
        // Decr
        states.move_cursor_left();
        assert_eq!(states.cursor, 0);
        // At begin
        states.move_cursor_left();
        assert_eq!(states.cursor, 0);
        // Move at end
        states.cursor_at_end(3);
        assert_eq!(states.cursor, 2);
        states.reset_cursor();
        assert_eq!(states.cursor, 0);
    }

    #[test]
    fn test_components_bar_chart() {
        let mut component: BarChart = BarChart::default()
            .disabled(false)
            .title(Title::from("my incomes").alignment(HorizontalAlignment::Center))
            .label_style(Style::default().fg(Color::Yellow))
            .bar_style(Style::default().fg(Color::LightYellow))
            .bar_gap(2)
            .width(4)
            .borders(Borders::default())
            .max_bars(6)
            .value_style(Style::default().fg(Color::LightBlue))
            .data(&[
                ("january", 250),
                ("february", 300),
                ("march", 275),
                ("april", 312),
                ("may", 420),
                ("june", 170),
                ("july", 220),
                ("august", 160),
                ("september", 180),
                ("october", 470),
                ("november", 380),
                ("december", 820),
            ]);
        // Commands
        assert_eq!(component.state(), State::None);
        // -> Right
        assert_eq!(
            component.perform(Cmd::Move(Direction::Right)),
            CmdResult::Visual
        );
        assert_eq!(component.states.cursor, 1);
        // <- Left
        assert_eq!(
            component.perform(Cmd::Move(Direction::Left)),
            CmdResult::Visual
        );
        assert_eq!(component.states.cursor, 0);
        // End
        assert_eq!(
            component.perform(Cmd::GoTo(Position::End)),
            CmdResult::Visual
        );
        assert_eq!(component.states.cursor, 11);
        // Home
        assert_eq!(
            component.perform(Cmd::GoTo(Position::Begin)),
            CmdResult::Visual
        );
        assert_eq!(component.states.cursor, 0);
    }
}