diff options
author | Justin Martin <jaming@protonmail.com> | 2022-10-15 15:08:48 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-15 15:08:48 -0400 |
commit | bd35bbdc9c539e035bc4bf91c799d85e566a6921 (patch) | |
tree | 7de767f70396f4fb51d1fb2d0d011e8cd6350744 /src/canvas/widgets/mem_basic.rs | |
parent | 41970d9c64ba0a442cd8314456785b4bb6c2f7b7 (diff) |
feature: add gpu ram collector for nvidia feature flag (#794)
* add gpu ram collector for nvidia feature flag
* add row for TX in basic layout
* size gpu point_vec
* use vec for mem basic widget drawing
* remove to_owned
* code review: change mem tuple to struct with cfg fields, rename mem_basic ratio and use vec macro for layout
* build on freebsd
Diffstat (limited to 'src/canvas/widgets/mem_basic.rs')
-rw-r--r-- | src/canvas/widgets/mem_basic.rs | 164 |
1 files changed, 104 insertions, 60 deletions
diff --git a/src/canvas/widgets/mem_basic.rs b/src/canvas/widgets/mem_basic.rs index 3e5e585d..8f372eff 100644 --- a/src/canvas/widgets/mem_basic.rs +++ b/src/canvas/widgets/mem_basic.rs @@ -14,23 +14,7 @@ impl Painter { &self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, widget_id: u64, ) { let mem_data = &app_state.converted_data.mem_data; - let swap_data = &app_state.converted_data.swap_data; - - let margined_loc = Layout::default() - .constraints({ - #[cfg(feature = "zfs")] - { - [Constraint::Length(1); 3] - } - - #[cfg(not(feature = "zfs"))] - { - [Constraint::Length(1); 2] - } - }) - .direction(Direction::Vertical) - .horizontal_margin(1) - .split(draw_loc); + let mut draw_widgets: Vec<PipeGauge<'_>> = Vec::new(); if app_state.current_widget.widget_id == widget_id { f.render_widget( @@ -41,13 +25,8 @@ impl Painter { ); } - let ram_ratio = if let Some(mem) = mem_data.last() { - mem.1 / 100.0 - } else { - 0.0 - }; - let swap_ratio = if let Some(swap) = swap_data.last() { - swap.1 / 100.0 + let ram_percentage = if let Some(mem) = mem_data.last() { + mem.1 } else { 0.0 }; @@ -56,64 +35,129 @@ impl Painter { let memory_fraction_label = if let Some((_, label_frac)) = &app_state.converted_data.mem_labels { - label_frac.trim() - } else { - EMPTY_MEMORY_FRAC_STRING - }; - - let swap_fraction_label = - if let Some((_, label_frac)) = &app_state.converted_data.swap_labels { - label_frac.trim() + if app_state.basic_mode_use_percent { + format!("{:3.0}%", ram_percentage.round()) + } else { + label_frac.trim().to_string() + } } else { - EMPTY_MEMORY_FRAC_STRING + EMPTY_MEMORY_FRAC_STRING.to_string() }; - f.render_widget( + draw_widgets.push( PipeGauge::default() - .ratio(ram_ratio) + .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), - margined_loc[0], ); - f.render_widget( - PipeGauge::default() - .ratio(swap_ratio) - .start_label("SWP") - .inner_label(swap_fraction_label) - .label_style(self.colours.swap_style) - .gauge_style(self.colours.swap_style), - margined_loc[1], - ); + 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_ratio = if let Some(arc) = arc_data.last() { - arc.1 / 100.0 + let arc_percentage = if let Some(arc) = arc_data.last() { + arc.1 } else { 0.0 }; - let arc_fraction_label = - if let Some((_, label_frac)) = &app_state.converted_data.arc_labels { - label_frac.trim() + 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 { - EMPTY_MEMORY_FRAC_STRING + 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), + ); + } + } - f.render_widget( - PipeGauge::default() - .ratio(arc_ratio) - .start_label("ARC") - .inner_label(arc_fraction_label) - .label_style(self.colours.arc_style) - .gauge_style(self.colours.arc_style), - margined_loc[2], - ); + #[cfg(feature = "gpu")] + { + let gpu_styles = &self.colours.gpu_colour_styles; + let mut color_index = 0; + if let Some(gpu_data) = &app_state.converted_data.gpu_data { + 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) { |