summaryrefslogtreecommitdiffstats
path: root/src/app/widgets/bottom_widgets/cpu.rs
blob: 9d01cdf923055314628961e291b4d836cd90f881 (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
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
use std::borrow::Cow;

use crossterm::event::{KeyEvent, MouseEvent};
use tui::{
    backend::Backend,
    layout::{Constraint, Direction, Layout, Rect},
    Frame,
};

use crate::{
    app::{
        event::{ComponentEventResult, SelectionAction},
        text_table::SimpleColumn,
        time_graph::TimeGraphData,
        widgets::tui_stuff::BlockBuilder,
        AppConfigFields, Component, DataCollection, TextTable, TimeGraph, Widget,
    },
    canvas::Painter,
    data_conversion::{convert_cpu_data_points, ConvertedCpuData},
    options::layout_options::LayoutRule,
};

/// Which part of the [`CpuGraph`] is currently selected.
enum CpuGraphSelection {
    Graph,
    Legend,
}

/// Whether the [`CpuGraph`]'s legend is placed on the left or right.
pub enum CpuGraphLegendPosition {
    Left,
    Right,
}

/// A widget designed to show CPU usage via a graph, along with a side legend in a table.
pub struct CpuGraph {
    graph: TimeGraph,
    legend: TextTable<SimpleColumn>,
    legend_position: CpuGraphLegendPosition,
    showing_avg: bool,

    bounds: Rect,
    selected: CpuGraphSelection,

    display_data: Vec<ConvertedCpuData>,
    load_avg_data: [f32; 3],

    width: LayoutRule,
    height: LayoutRule,
}

impl CpuGraph {
    /// Creates a new [`CpuGraph`] from a config.
    pub fn from_config(app_config_fields: &AppConfigFields) -> Self {
        let graph = TimeGraph::from_config(app_config_fields);
        let legend = TextTable::new(vec![
            SimpleColumn::new_flex("CPU".into(), 0.5),
            SimpleColumn::new_hard("Use".into(), None),
        ])
        .default_ltr(false)
        .try_show_gap(app_config_fields.table_gap);
        let legend_position = if app_config_fields.left_legend {
            CpuGraphLegendPosition::Left
        } else {
            CpuGraphLegendPosition::Right
        };
        let showing_avg = app_config_fields.show_average_cpu;

        Self {
            graph,
            legend,
            legend_position,
            showing_avg,
            bounds: Rect::default(),
            selected: CpuGraphSelection::Graph,
            display_data: Default::default(),
            load_avg_data: [0.0; 3],
            width: LayoutRule::default(),
            height: LayoutRule::default(),
        }
    }

    /// Sets the width.
    pub fn width(mut self, width: LayoutRule) -> Self {
        self.width = width;
        self
    }

    /// Sets the height.
    pub fn height(mut self, height: LayoutRule) -> Self {
        self.height = height;
        self
    }
}

impl Component for CpuGraph {
    fn handle_key_event(&mut self, event: KeyEvent) -> ComponentEventResult {
        match self.selected {
            CpuGraphSelection::Graph => self.graph.handle_key_event(event),
            CpuGraphSelection::Legend => self.legend.handle_key_event(event),
        }
    }

    fn handle_mouse_event(&mut self, event: MouseEvent) -> ComponentEventResult {
        if self.graph.does_border_intersect_mouse(&event) {
            if let CpuGraphSelection::Graph = self.selected {
                self.graph.handle_mouse_event(event)
            } else {
                self.selected = CpuGraphSelection::Graph;
                self.graph.handle_mouse_event(event);
                ComponentEventResult::Redraw
            }
        } else if self.legend.does_border_intersect_mouse(&event) {
            if let CpuGraphSelection::Legend = self.selected {
                self.legend.handle_mouse_event(event)
            } else {
                self.selected = CpuGraphSelection::Legend;
                self.legend.handle_mouse_event(event);
                ComponentEventResult::Redraw
            }
        } else {
            ComponentEventResult::Unhandled
        }
    }

    fn bounds(&self) -> Rect {
        self.bounds
    }

    fn set_bounds(&mut self, new_bounds: Rect) {
        self.bounds = new_bounds;
    }
}

impl Widget for CpuGraph {
    fn get_pretty_name(&self) -> &'static str {
        "CPU"
    }

    fn draw<B: Backend>(
        &mut self, painter: &Painter, f: &mut Frame<'_, B>, area: Rect, selected: bool,
        expanded: bool,
    ) {
        let constraints = {
            const CPU_LEGEND_MIN_WIDTH: u16 = 10;
            let (legend_constraint, cpu_constraint) =
                if area.width * 15 / 100 < CPU_LEGEND_MIN_WIDTH {
                    (Constraint::Length(CPU_LEGEND_MIN_WIDTH), Constraint::Min(0))
                } else {
                    (Constraint::Percentage(15), Constraint::Percentage(85))
                };
            match self.legend_position {
                CpuGraphLegendPosition::Left => [legend_constraint, cpu_constraint],
                CpuGraphLegendPosition::Right => [cpu_constraint, legend_constraint],
            }
        };

        let split_area = Layout::default()
            .margin(0)
            .direction(Direction::Horizontal)
            .constraints(constraints)
            .split(area);

        let (graph_block_area, legend_block_area) = match self.legend_position {
            CpuGraphLegendPosition::Left => (split_area[1], split_area[0]),
            CpuGraphLegendPosition::Right => (split_area[0], split_area[1]),
        };

        let legend_block = self
            .block()
            .selected(selected && matches!(&self.selected, CpuGraphSelection::Legend))
            .show_esc(expanded)
            .hide_title(true);

        let legend_data = self
            .display_data
            .iter()
            .enumerate()
            .map(|(cpu_index, core_data)| {
                let style = Some(if cpu_index == 0 {
                    painter.colours.all_colour_style
                } else if self.showing_avg && cpu_index == 1 {
                    painter.colours.avg_colour_style
                } else {
                    let cpu_style_index = if self.showing_avg {
                        // No underflow should occur, as if cpu_index was
                        // 1 and avg is showing, it's caught by the above case!
                        cpu_index - 2
                    } else {
                        cpu_index - 1
                    };
                    painter.colours.cpu_colour_styles
                        [cpu_style_index % painter.colours.cpu_colour_styles.len()]
                });

                vec![
                    (
                        core_data.cpu_name.clone().into(),
                        Some(core_data.short_cpu_name.clone().into()),
                        style,
                    ),
                    (core_data.legend_value.clone().into(), None, style),
                ]
            })
            .collect::<Vec<_>>();

        // TODO: [Gotcha, Refactor] You MUST draw the table first, otherwise the index may mismatch after a reset. This is a bad gotcha - we should look into auto-updating the table's length!
        self.legend.draw_tui_table(
            painter,
            f,
            &legend_data,
            legend_block,
            legend_block_area,
            true,
            false,
        );

        const Y_BOUNDS: [f64; 2] = [0.0, 100.5];
        let y_bound_labels: [Cow<'static, str>; 2] = ["0%".into(), "100%".into()];

        let current_index = self.legend.current_scroll_index();
        let sliced_cpu_data = if current_index == 0 {
            &self.display_data[..]
        } else {
            &self.display_data[current_index..current_index + 1]
        };

        let cpu_data = sliced_cpu_data
            .iter()
            .enumerate()
            .map(|(cpu_index, core_data)| TimeGraphData {
                data: &core_data.cpu_data,
                label: None,
                style: {
                    let offset_cpu_index = cpu_index + current_index;
                    if offset_cpu_index == 0 {
                        painter.colours.all_colour_style
                    } else if self.showing_avg && offset_cpu_index == 1 {
                        painter.colours.avg_colour_style
                    } else {
                        let cpu_style_index = if self.showing_avg {
                            // No underflow should occur, as if offset_cpu_index was
                            // 1 and avg is showing, it's caught by the above case!
                            offset_cpu_index - 2
                        } else {
                            offset_cpu_index - 1
                        };
                        painter.colours.cpu_colour_styles