Skip to content

Commit

Permalink
add sort_cpu_hist mods to cpu legend
Browse files Browse the repository at this point in the history
  • Loading branch information
acxz committed Apr 22, 2022
1 parent 2abd403 commit dc6d508
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
23 changes: 21 additions & 2 deletions src/canvas/widgets/cpu_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use tui::{
};

const CPU_LEGEND_HEADER: [&str; 2] = ["CPU", "Use%"];
const RANK_LEGEND_HEADER: [&str; 2] = ["Rank", "Use%"];
const AVG_POSITION: usize = 1;
const ALL_POSITION: usize = 0;

Expand All @@ -32,6 +33,13 @@ static CPU_LEGEND_HEADER_LENS: Lazy<Vec<u16>> = Lazy::new(|| {
.collect::<Vec<_>>()
});

static RANK_LEGEND_HEADER_LENS: Lazy<Vec<u16>> = Lazy::new(|| {
RANK_LEGEND_HEADER
.iter()
.map(|entry| entry.len() as u16)
.collect::<Vec<_>>()
});

pub trait CpuGraphWidget {
fn draw_cpu<B: Backend>(
&self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, widget_id: u64,
Expand Down Expand Up @@ -420,13 +428,19 @@ impl CpuGraphWidget for Painter {
.saturating_sub(start_position);
let show_avg_cpu = app_state.app_config_fields.show_average_cpu;

let sort_cpu_hist = app_state.app_config_fields.sort_cpu_hist;
let legend_header_lens = match sort_cpu_hist {
false => &CPU_LEGEND_HEADER_LENS,
true => &RANK_LEGEND_HEADER_LENS,
};

// Calculate widths
if recalculate_column_widths {
cpu_widget_state.table_width_state.desired_column_widths = vec![6, 4];
cpu_widget_state.table_width_state.calculated_column_widths = get_column_widths(
draw_loc.width,
&[None, None],
&(CPU_LEGEND_HEADER_LENS
&(legend_header_lens
.iter()
.map(|width| Some(*width))
.collect::<Vec<_>>()),
Expand Down Expand Up @@ -506,6 +520,11 @@ impl CpuGraphWidget for Painter {
self.colours.border_style
};

let legend_header = match sort_cpu_hist {
false => &CPU_LEGEND_HEADER,
true => &RANK_LEGEND_HEADER,
};

// Draw
f.render_stateful_widget(
Table::new(cpu_rows)
Expand All @@ -515,7 +534,7 @@ impl CpuGraphWidget for Painter {
.border_style(border_and_title_style),
)
.header(
Row::new(CPU_LEGEND_HEADER.to_vec())
Row::new(legend_header.to_vec())
.style(self.colours.table_header_style)
.bottom_margin(table_gap),
)
Expand Down
6 changes: 5 additions & 1 deletion src/data_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ pub fn convert_cpu_data_points(
.map(|(itx, cpu_usage)| ConvertedCpuData {
cpu_name: if let Some(cpu_harvest) = current_data.cpu_harvest.get(itx) {
if let Some(cpu_count) = cpu_harvest.cpu_count {
format!("{}{}", cpu_harvest.cpu_prefix, cpu_count)
let cpu_prefix = match sort_cpu_hist {
false => &cpu_harvest.cpu_prefix,
true => "Rank",
};
format!("{}{}", cpu_prefix, cpu_count)
} else {
cpu_harvest.cpu_prefix.to_string()
}
Expand Down

0 comments on commit dc6d508

Please sign in to comment.