summaryrefslogtreecommitdiffstats
path: root/src/canvas/widgets/mem_graph.rs
blob: 9136b1ba7207a90c7873e1dc1479de8b0700d03f (plain)
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
use crate::{app::App, canvas::Painter, constants::*};

use tui::{
    backend::Backend,
    layout::{Constraint, Rect},
    symbols::Marker,
    terminal::Frame,
    text::Span,
    text::Spans,
    widgets::{Axis, Block, Borders, Chart, Dataset},
};
use unicode_segmentation::UnicodeSegmentation;

pub trait MemGraphWidget {
    fn draw_memory_graph<B: Backend>(
        &self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, widget_id: u64,
    );
}

impl MemGraphWidget for Painter {
    fn draw_memory_graph<B: Backend>(
        &self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, widget_id: u64,
    ) {
        if let Some(mem_widget_state) = app_state.mem_state.widget_states.get_mut(&widget_id) {
            let mem_data: &[(f64, f64)] = &app_state.canvas_data.mem_data;
            let swap_data: &[(f64, f64)] = &app_state.canvas_data.swap_data;

            let display_time_labels = vec![
                Span::styled(
                    format!("{}s", mem_widget_state.current_display_time / 1000),
                    self.colours.graph_style,
                ),
                Span::styled("0s".to_string(), self.colours.graph_style),
            ];
            let y_axis_label = vec![
                Span::styled("0%", self.colours.graph_style),
                Span::styled("100%", self.colours.graph_style),
            ];

            let x_axis = if app_state.app_config_fields.hide_time
                || (app_state.app_config_fields.autohide_time
                    && mem_widget_state.autohide_timer.is_none())
            {
                Axis::default().bounds([-(mem_widget_state.current_display_time as f64), 0.0])
            } else if let Some(time) = mem_widget_state.autohide_timer {
                if std::time::Instant::now().duration_since(time).as_millis()
                    < AUTOHIDE_TIMEOUT_MILLISECONDS as u128
                {
                    Axis::default()
                        .bounds([-(mem_widget_state.current_display_time as f64), 0.0])
                        .style(self.colours.graph_style)
                        .labels(display_time_labels)
                } else {
                    mem_widget_state.autohide_timer = None;
                    Axis::default().bounds([-(mem_widget_state.current_display_time as f64), 0.0])
                }
            } else if draw_loc.height < TIME_LABEL_HEIGHT_LIMIT {
                Axis::default().bounds([-(mem_widget_state.current_display_time as f64), 0.0])
            } else {
                Axis::default()
                    .bounds([-(mem_widget_state.current_display_time as f64), 0.0])
                    .style(self.colours.graph_style)
                    .labels(display_time_labels)
            };

            let y_axis = Axis::default()
                .style(self.colours.graph_style)
                .bounds([0.0, 100.5])
                .labels(y_axis_label);

            let mut mem_canvas_vec: Vec<Dataset<'_>> = vec![];

            if let Some((label_percent, label_frac)) = &app_state.canvas_data.mem_labels {
                let mem_label = format!("RAM:{}{}", label_percent, label_frac);
                mem_canvas_vec.push(
                    Dataset::default()
                        .name(mem_label)
                        .marker(if app_state.app_config_fields.use_dot {
                            Marker::Dot
                        } else {
                            Marker::Braille
                        })
                        .style(self.colours.ram_style)
                        .data(&mem_data)
                        .graph_type(tui::widgets::GraphType::Line),
                );
            }

            if let Some((label_percent, label_frac)) = &app_state.canvas_data.swap_labels {
                let swap_label = format!("SWP:{}{}", label_percent, label_frac);
                mem_canvas_vec.push(
                    Dataset::default()
                        .name(swap_label)
                        .marker(if app_state.app_config_fields.use_dot {
                            Marker::Dot
                        } else {
                            Marker::Braille
                        })
                        .style(self.colours.swap_style)
                        .data(&swap_data)
                        .graph_type(tui::widgets::GraphType::Line),
                );
            }

            let is_on_widget = widget_id == app_state.current_widget.widget_id;
            let border_style = if is_on_widget {
                self.colours.highlighted_border_style
            } else {
                self.colours.border_style
            };

            let title = if app_state.is_expanded {
                const TITLE_BASE: &str = " Memory ── Esc to go back ";
                Spans::from(vec![
                    Span::styled(" Memory ", self.colours.widget_title_style),
                    Span::styled(
                        format!(
                            "─{}─ Esc to go back ",
                            "─".repeat(usize::from(draw_loc.width).saturating_sub(
                                UnicodeSegmentation::graphemes(TITLE_BASE, true).count() + 2
                            ))
                        ),
                        border_style,
                    ),
                ])
            } else {
                Spans::from(Span::styled(
                    " Memory ".to_string(),
                    self.colours.widget_title_style,
                ))
            };

            f.render_widget(
                Chart::new(mem_canvas_vec)
                    .block(
                        Block::default()
                            .title(title)
                            .borders(Borders::ALL)
                            .border_style(if app_state.current_widget.widget_id == widget_id {
                                self.colours.highlighted_border_style
                            } else {
                                self.colours.border_style
                            }),
                    )
                    .x_axis(x_axis)
                    .y_axis(y_axis)
                    .hidden_legend_constraints((Constraint::Ratio(3, 4), Constraint::Ratio(3, 4))),
                draw_loc,
            );
        }

        if app_state.should_get_widget_bounds() {
            // Update draw loc in widget map
            if let Some(widget) = app_state.widget_map.get_mut(&widget_id) {
                widget.top_left_corner = Some((draw_loc.x, draw_loc.y));
                widget.bottom_right_corner =
                    Some((draw_loc.x + draw_loc.width, draw_loc.y + draw_loc.height));
            }
        }
    }
}