summaryrefslogtreecommitdiffstats
path: root/src/canvas/widgets/mem_basic.rs
blob: c950329f4cee64c2f97676a415333636105843b3 (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
use tui::{
    layout::{Constraint, Direction, Layout, Rect},
    terminal::Frame,
    widgets::Block,
};

use crate::{
    app::App,
    canvas::{components::pipe_gauge::PipeGauge, Painter},
    constants::*,
};

impl Painter {
    pub fn draw_basic_memory(
        &self, f: &mut Frame<'_>, app_state: &mut App, draw_loc: Rect, widget_id: u64,
    ) {
        let mem_data = &app_state.converted_data.mem_data;
        let mut draw_widgets: Vec<PipeGauge<'_>> = Vec::new();

        if app_state.current_widget.widget_id == widget_id {
            f.render_widget(
                Block::default()
                    .borders(SIDE_BORDERS)
                    .border_style(self.colours.highlighted_border_style),
                draw_loc,
            );
        }

        let ram_percentage = if let Some(mem) = mem_data.last() {
            mem.1
        } else {
            0.0
        };

        const EMPTY_MEMORY_FRAC_STRING: &str = "0.0B/0.0B";

        let memory_fraction_label =
            if let Some((_, label_frac)) = &app_state.converted_data.mem_labels {
                if app_state.basic_mode_use_percent {
                    format!("{:3.0}%", ram_percentage.round())
                } else {
                    label_frac.trim().to_string()
                }
            } else {
                EMPTY_MEMORY_FRAC_STRING.to_string()
            };

        draw_widgets.push(
            PipeGauge::default()
                .ratio(ram_percentage / 100.0)
                .start_label("RAM")
                .inner_label(memory_fraction_label)
                .label_style(self.colours.ram_style)
                .gauge_style(self.colours.ram_style),
        );

        #[cfg(not(target_os = "windows"))]
        {
            if let Some((_, label_frac)) = &app_state.converted_data.cache_labels {
                let cache_data = &app_state.converted_data.cache_data;

                let cache_percentage = if let Some(cache) = cache_data.last() {
                    cache.1
                } else {
                    0.0
                };

                let cache_fraction_label = if app_state.basic_mode_use_percent {
                    format!("{:3.0}%", cache_percentage.round())
                } else {
                    label_frac.trim().to_string()
                };
                draw_widgets.push(
                    PipeGauge::default()
                        .ratio(cache_percentage / 100.0)
                        .start_label("CHE")
                        .inner_label(cache_fraction_label)
                        .label_style(self.colours.cache_style)
                        .gauge_style(self.colours.cache_style),
                );
            }
        }

        let swap_data = &app_state.converted_data.swap_data;

        let swap_percentage = if let Some(swap) = swap_data.last() {
            swap.1
        } else {
            0.0
        };

        if let Some((_, label_frac)) = &app_state.converted_data.swap_labels {
            let swap_fraction_label = if app_state.basic_mode_use_percent {
                format!("{:3.0}%", swap_percentage.round())
            } else {
                label_frac.trim().to_string()
            };
            draw_widgets.push(
                PipeGauge::default()
                    .ratio(swap_percentage / 100.0)
                    .start_label("SWP")
                    .inner_label(swap_fraction_label)
                    .label_style(self.colours.swap_style)
                    .gauge_style(self.colours.swap_style),
            );
        }

        #[cfg(feature = "zfs")]
        {
            let arc_data = &app_state.converted_data.arc_data;
            let arc_percentage = if let Some(arc) = arc_data.last() {
                arc.1
            } else {
                0.0
            };
            if let Some((_, label_frac)) = &app_state.converted_data.arc_labels {
                let arc_fraction_label = if app_state.basic_mode_use_percent {
                    format!("{:3.0}%", arc_percentage.round())
                } else {
                    label_frac.trim().to_string()
                };
                draw_widgets.push(
                    PipeGauge::default()
                        .ratio(arc_percentage / 100.0)
                        .start_label("ARC")
                        .inner_label(arc_fraction_label)
                        .label_style(self.colours.arc_style)
                        .gauge_style(self.colours.arc_style),
                );
            }
        }

        #[cfg(feature = "gpu")]
        {
            if let Some(gpu_data) = &app_state.converted_data.gpu_data {
                let gpu_styles = &self.colours.gpu_colour_styles;
                let mut color_index = 0;

                gpu_data.iter().for_each(|gpu_data_vec| {
                    let gpu_data = gpu_data_vec.points.as_slice();
                    let gpu_percentage = if let Some(gpu) = gpu_data.last() {
                        gpu.1
                    } else {
                        0.0
                    };
                    let trimmed_gpu_frac = {
                        if app_state.basic_mode_use_percent {
                            format!("{:3.0}%", gpu_percentage.round())
                        } else {
                            gpu_data_vec.mem_total.trim().to_string()
                        }
                    };
                    let style = {
                        if gpu_styles.is_empty() {
                            tui::style::Style::default()
                        } else if color_index >= gpu_styles.len() {
                            // cycle styles
                            color_index = 1;
                            gpu_styles[color_index - 1]
                        } else {
                            color_index += 1;
                            gpu_styles[color_index - 1]
                        }
                    };
                    draw_widgets.push(
                        PipeGauge::default()
                            .ratio(gpu_percentage / 100.0)
                            .start_label("GPU")
                            .inner_label(trimmed_gpu_frac)
                            .label_style(style)
                            .gauge_style(style),
                    );
                });
            }
        }

        let margined_loc = Layout::default()
            .constraints(vec![Constraint::Length(1); draw_widgets.len()])
            .direction(Direction::Vertical)
            .horizontal_margin(1)
            .split(draw_loc);

        draw_widgets
            .into_iter()
            .enumerate()
            .for_each(|(index, widget)| {
                f.render_widget(widget, margined_loc[index]);
            });

        // Update draw loc in widget map
        if app_state.should_get_widget_bounds() {
            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));
            }
        }
    }
}