mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-24 22:24:53 +02:00
Add CPU row allocation system to more evenly distribute it (aka simple division)
This commit is contained in:
parent
32ca9edf11
commit
c69362000d
@ -30,6 +30,8 @@ Features of bottom include:
|
|||||||
|
|
||||||
- Maximizing of widgets of interest to take up the entire window.
|
- 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).
|
More details about each widget and compatibility can be found [here](./docs/widgets.md).
|
||||||
|
|
||||||
## Config files
|
## Config files
|
||||||
|
131
src/canvas.rs
131
src/canvas.rs
@ -1643,75 +1643,88 @@ impl Painter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let num_cpus = cpu_data.len();
|
let num_cpus = cpu_data.len();
|
||||||
let remaining_height = draw_loc.height as usize;
|
if draw_loc.height > 0 {
|
||||||
const REQUIRED_COLUMNS: usize = 4;
|
let remaining_height = draw_loc.height as usize;
|
||||||
|
const REQUIRED_COLUMNS: usize = 4;
|
||||||
|
|
||||||
let chunk_vec =
|
let chunk_vec =
|
||||||
vec![Constraint::Percentage((100 / REQUIRED_COLUMNS) as u16); REQUIRED_COLUMNS];
|
vec![Constraint::Percentage((100 / REQUIRED_COLUMNS) as u16); REQUIRED_COLUMNS];
|
||||||
let chunks = Layout::default()
|
let chunks = Layout::default()
|
||||||
.constraints(chunk_vec.as_ref())
|
.constraints(chunk_vec.as_ref())
|
||||||
.direction(Direction::Horizontal)
|
.direction(Direction::Horizontal)
|
||||||
.split(draw_loc);
|
.split(draw_loc);
|
||||||
|
|
||||||
// +9 due to 3 + 4 + 2 columns for the name & space + percentage + bar bounds
|
// +9 due to 3 + 4 + 2 columns for the name & space + percentage + bar bounds
|
||||||
let margin_space = 2;
|
let margin_space = 2;
|
||||||
let remaining_width = max(
|
let remaining_width = max(
|
||||||
0,
|
0,
|
||||||
draw_loc.width as i64 - ((9 + margin_space) * REQUIRED_COLUMNS - margin_space) as i64,
|
draw_loc.width as i64
|
||||||
) as usize;
|
- ((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.
|
// CPU (and RAM) percent bars are, uh, "heavily" inspired from htop.
|
||||||
let cpu_bars = (0..num_cpus)
|
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)
|
|
||||||
.map(|cpu_index| {
|
.map(|cpu_index| {
|
||||||
Text::Styled(
|
let use_percentage =
|
||||||
(&cpu_bars[cpu_index]).into(),
|
if let Some(cpu_usage) = cpu_data[cpu_index].cpu_data.last() {
|
||||||
self.colours.cpu_colour_styles
|
cpu_usage.1
|
||||||
[cpu_index as usize % self.colours.cpu_colour_styles.len()],
|
} 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<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let margined_loc = Layout::default()
|
let mut row_counter = num_cpus;
|
||||||
.direction(Direction::Horizontal)
|
let mut start_index = 0;
|
||||||
.constraints([Constraint::Percentage(100)].as_ref())
|
for (itx, chunk) in chunks.iter().enumerate() {
|
||||||
.horizontal_margin(1)
|
let to_divide = REQUIRED_COLUMNS - itx;
|
||||||
.split(*chunk);
|
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())
|
start_index += how_many_cpus;
|
||||||
.block(Block::default())
|
|
||||||
.render(f, margined_loc[0]);
|
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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user