Add CPU row allocation system to more evenly distribute it (aka simple division)

This commit is contained in:
ClementTsang 2020-03-04 23:13:00 -05:00
parent 32ca9edf11
commit c69362000d
2 changed files with 74 additions and 59 deletions

View File

@ -30,6 +30,8 @@ Features of bottom include:
- Maximizing of widgets of interest to take up the entire window.
- Basic mode
More details about each widget and compatibility can be found [here](./docs/widgets.md).
## Config files

View File

@ -1643,75 +1643,88 @@ impl Painter {
}
let num_cpus = cpu_data.len();
let remaining_height = draw_loc.height as usize;
const REQUIRED_COLUMNS: usize = 4;
if draw_loc.height > 0 {
let remaining_height = draw_loc.height as usize;
const REQUIRED_COLUMNS: usize = 4;
let chunk_vec =
vec![Constraint::Percentage((100 / REQUIRED_COLUMNS) as u16); REQUIRED_COLUMNS];
let chunks = Layout::default()
.constraints(chunk_vec.as_ref())
.direction(Direction::Horizontal)
.split(draw_loc);
let chunk_vec =
vec![Constraint::Percentage((100 / REQUIRED_COLUMNS) as u16); REQUIRED_COLUMNS];
let chunks = Layout::default()
.constraints(chunk_vec.as_ref())
.direction(Direction::Horizontal)
.split(draw_loc);
// +9 due to 3 + 4 + 2 columns for the name & space + percentage + bar bounds
let margin_space = 2;
let remaining_width = max(
0,
draw_loc.width as i64 - ((9 + margin_space) * REQUIRED_COLUMNS - margin_space) as i64,
) as usize;
// +9 due to 3 + 4 + 2 columns for the name & space + percentage + bar bounds
let margin_space = 2;
let remaining_width = max(
0,
draw_loc.width as i64
- ((9 + margin_space) * REQUIRED_COLUMNS - margin_space) as i64,
) as usize;
let bar_length = remaining_width / REQUIRED_COLUMNS;
let bar_length = remaining_width / REQUIRED_COLUMNS;
// CPU (and RAM) percent bars are, uh, "heavily" inspired from htop.
let cpu_bars = (0..num_cpus)
.map(|cpu_index| {
let use_percentage = if let Some(cpu_usage) = cpu_data[cpu_index].cpu_data.last() {
cpu_usage.1
} else {
0.0
};
let num_bars = calculate_basic_use_bars(use_percentage, bar_length);
format!(
"{:3}[{}{}{:3.0}%]\n",
if app_state.app_config_fields.show_average_cpu {
if cpu_index == 0 {
"AVG".to_string()
} else {
(cpu_index - 1).to_string()
}
} else {
cpu_index.to_string()
},
"|".repeat(num_bars),
" ".repeat(bar_length - num_bars),
use_percentage.round(),
)
})
.collect::<Vec<_>>();
for (current_row, chunk) in chunks.iter().enumerate() {
let start_index = (current_row * remaining_height) as usize;
let end_index = min(start_index + remaining_height, num_cpus);
let cpu_column: Vec<Text<'_>> = (start_index..end_index)
// CPU (and RAM) percent bars are, uh, "heavily" inspired from htop.
let cpu_bars = (0..num_cpus)
.map(|cpu_index| {
Text::Styled(
(&cpu_bars[cpu_index]).into(),
self.colours.cpu_colour_styles
[cpu_index as usize % self.colours.cpu_colour_styles.len()],
let use_percentage =
if let Some(cpu_usage) = cpu_data[cpu_index].cpu_data.last() {
cpu_usage.1
} else {
0.0
};
let num_bars = calculate_basic_use_bars(use_percentage, bar_length);
format!(
"{:3}[{}{}{:3.0}%]\n",
if app_state.app_config_fields.show_average_cpu {
if cpu_index == 0 {
"AVG".to_string()
} else {
(cpu_index - 1).to_string()
}
} else {
cpu_index.to_string()
},
"|".repeat(num_bars),
" ".repeat(bar_length - num_bars),
use_percentage.round(),
)
})
.collect::<Vec<_>>();
let margined_loc = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(100)].as_ref())
.horizontal_margin(1)
.split(*chunk);
let mut row_counter = num_cpus;
let mut start_index = 0;
for (itx, chunk) in chunks.iter().enumerate() {
let to_divide = REQUIRED_COLUMNS - itx;
let how_many_cpus = min(
remaining_height,
(row_counter / to_divide) + (if row_counter % to_divide == 0 { 0 } else { 1 }),
);
row_counter -= how_many_cpus;
let end_index = min(start_index + how_many_cpus, num_cpus);
let cpu_column: Vec<Text<'_>> = (start_index..end_index)
.map(|cpu_index| {
Text::Styled(
(&cpu_bars[cpu_index]).into(),
self.colours.cpu_colour_styles
[cpu_index as usize % self.colours.cpu_colour_styles.len()],
)
})
.collect::<Vec<_>>();
Paragraph::new(cpu_column.iter())
.block(Block::default())
.render(f, margined_loc[0]);
start_index += how_many_cpus;
let margined_loc = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(100)].as_ref())
.horizontal_margin(1)
.split(*chunk);
Paragraph::new(cpu_column.iter())
.block(Block::default())
.render(f, margined_loc[0]);
}
}
}