Debian Codegraph @

src/canvas/widgets/mem_basic.rs

raw symbols SCIP
1 use std::borrow::Cow;
2
3 use ratatui::{
4 Frame,
5 layout::{Constraint, Direction, Layout, Rect},
6 };
7
8 use crate::{
9 app::App,
10 canvas::{Painter, components::pipe_gauge::PipeGauge, drawing_utils::widget_block},
11 collection::memory::MemData,
12 get_binary_unit_and_denominator,
13 };
14
15 /// Convert memory info into a string representing a fraction.
16 #[inline]
17 fn memory_fraction_label(data: &MemData) -> Cow<'static, str> {
18 let total_bytes = data.total_bytes.get();
19 let (unit, denominator) = get_binary_unit_and_denominator(total_bytes);
20 let used = data.used_bytes as f64 / denominator;
21 let total = total_bytes as f64 / denominator;
22
23 format!("{used:.1}{unit}/{total:.1}{unit}").into()
24 }
25
26 /// Convert memory info into a string representing a percentage.
27 #[inline]
28 fn memory_percentage_label(data: &MemData) -> Cow<'static, str> {
29 let total_bytes = data.total_bytes.get();
30 let percentage = data.used_bytes as f64 / total_bytes as f64 * 100.0;
31 format!("{percentage:3.0}%").into()
32 }
33
34 #[inline]
35 fn memory_label(data: &MemData, is_percentage: bool) -> Cow<'static, str> {
36 if is_percentage {
37 memory_percentage_label(data)
38 } else {
39 memory_fraction_label(data)
40 }
41 }
42
43 impl Painter {
44 pub fn draw_basic_memory(
45 &self, f: &mut Frame<'_>, app_state: &mut App, draw_loc: Rect, widget_id: u64,
46 ) {
47 let mut draw_widgets: Vec<PipeGauge<'_>> = Vec::new();
48
49 f.render_widget(
50 widget_block(
51 true,
52 app_state.current_widget.widget_id == widget_id,
53 self.styles.border_type,
54 self.styles.general_widget_style,
55 )
56 .border_style(self.styles.highlighted_border_style),
57 draw_loc,
58 );
59
60 let data = app_state.data_store.get_data();
61
62 let (ram_percentage, ram_label) = if let Some(ram_harvest) = &data.ram_harvest {
63 (
64 ram_harvest.percentage(),
65 memory_label(ram_harvest, app_state.basic_mode_use_percent),
66 )
67 } else {
68 (
69 0.0,
70 if app_state.basic_mode_use_percent {
71 " 0%".into()
72 } else {
73 "0.0B/0.0B".into()
74 },
75 )
76 };
77
78 draw_widgets.push(
79 PipeGauge::default()
80 .ratio(ram_percentage / 100.0)
81 .start_label("RAM")
82 .inner_label(ram_label)
83 .label_style(self.styles.ram_style)
84 .gauge_style(self.styles.ram_style),
85 );
86
87 if let Some(swap_harvest) = &data.swap_harvest {
88 let swap_percentage = swap_harvest.percentage();
89 let swap_label = memory_label(swap_harvest, app_state.basic_mode_use_percent);
90
91 draw_widgets.push(
92 PipeGauge::default()
93 .ratio(swap_percentage / 100.0)
94 .start_label("SWP")
95 .inner_label(swap_label)
96 .label_style(self.styles.swap_style)
97 .gauge_style(self.styles.swap_style),
98 );
99 }
100
101 #[cfg(not(target_os = "windows"))]
102 {
103 if let Some(cache_harvest) = &data.cache_harvest {
104 let cache_percentage = cache_harvest.percentage();
105 let cache_fraction_label =
106 memory_label(cache_harvest, app_state.basic_mode_use_percent);
107
108 draw_widgets.push(
109 PipeGauge::default()
110 .ratio(cache_percentage / 100.0)
111 .start_label("CHE")
112 .inner_label(cache_fraction_label)
113 .label_style(self.styles.cache_style)
114 .gauge_style(self.styles.cache_style),
115 );
116 }
117 }
118
119 #[cfg(feature = "zfs")]
120 {
121 if let Some(arc_harvest) = &data.arc_harvest {
122 let arc_percentage = arc_harvest.percentage();
123 let arc_fraction_label =
124 memory_label(arc_harvest, app_state.basic_mode_use_percent);
125
126 draw_widgets.push(
127 PipeGauge::default()
128 .ratio(arc_percentage / 100.0)
129 .start_label("ARC")
130 .inner_label(arc_fraction_label)
131 .label_style(self.styles.arc_style)
132 .gauge_style(self.styles.arc_style),
133 );
134 }
135 }
136
137 #[cfg(feature = "gpu")]
138 {
139 let gpu_styles = &self.styles.gpu_colours;
140 let mut colour_index = 0;
141
142 for (_, harvest) in data.gpu_harvest.iter() {
143 let percentage = harvest.percentage();
144 let label = memory_label(harvest, app_state.basic_mode_use_percent);
145
146 let style = {
147 if gpu_styles.is_empty() {
148 ratatui::style::Style::default()
149 } else {
150 let colour = gpu_styles[colour_index % gpu_styles.len()];
151 colour_index += 1;
152
153 colour
154 }
155 };
156
157 draw_widgets.push(
158 PipeGauge::default()
159 .ratio(percentage / 100.0)
160 .start_label("GPU")
161 .inner_label(label)
162 .label_style(style)
163 .gauge_style(style),
164 );
165 }
166 }
167
168 let margined_loc = Layout::default()
169 .constraints(vec![Constraint::Length(1); draw_widgets.len()])
170 .direction(Direction::Vertical)
171 .horizontal_margin(1)
172 .split(draw_loc);
173
174 draw_widgets
175 .into_iter()
176 .enumerate()
177 .for_each(|(index, widget)| {
178 f.render_widget(widget, margined_loc[index]);
179 });
180
181 // Update draw loc in widget map
182 if app_state.should_get_widget_bounds()
183 && let Some(widget) = app_state.widget_map.get_mut(&widget_id)
184 {
185 widget.top_left_corner = Some((draw_loc.x, draw_loc.y));
186 widget.bottom_right_corner =
187 Some((draw_loc.x + draw_loc.width, draw_loc.y + draw_loc.height));
188 }
189 }
190 }