mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-23 21:55:11 +02:00
Added space, fixed div by 0 error.
This commit is contained in:
parent
1a54bb45fb
commit
8baa04f976
143
src/canvas.rs
143
src/canvas.rs
@ -1559,78 +1559,75 @@ impl Painter {
|
|||||||
// the desired lengths.
|
// the desired lengths.
|
||||||
|
|
||||||
let num_cpus = cpu_data.len();
|
let num_cpus = cpu_data.len();
|
||||||
let remaining_height = (draw_loc.height - 2) as usize;
|
if draw_loc.height > 2 {
|
||||||
let required_columns = (num_cpus / remaining_height)
|
let remaining_height = (draw_loc.height - 2) as usize;
|
||||||
+ (if num_cpus % remaining_height == 0 {
|
let required_columns = (num_cpus / remaining_height)
|
||||||
0
|
+ (if num_cpus % remaining_height == 0 {
|
||||||
} else {
|
0
|
||||||
1
|
} else {
|
||||||
});
|
1
|
||||||
|
});
|
||||||
|
|
||||||
// debug!(
|
if required_columns > 0 {
|
||||||
// "Num cpus: {}, remaining height: {}, required columns: {}",
|
let chunk_vec =
|
||||||
// num_cpus, remaining_height, required_columns
|
vec![Constraint::Percentage((100 / required_columns) as u16); required_columns];
|
||||||
// );
|
let chunks = Layout::default()
|
||||||
|
.constraints(chunk_vec.as_ref())
|
||||||
|
.direction(Direction::Horizontal)
|
||||||
|
.margin(1)
|
||||||
|
.split(draw_loc);
|
||||||
|
|
||||||
if required_columns > 0 {
|
let num_spaces = 3;
|
||||||
let chunk_vec =
|
// +10 due to 4 + 4 + 2 columns for the name & space + percentage + bar bounds
|
||||||
vec![Constraint::Percentage((100 / required_columns) as u16); required_columns];
|
let allowed_width = max(
|
||||||
let chunks = Layout::default()
|
0,
|
||||||
.constraints(chunk_vec.as_ref())
|
draw_loc.width as i64
|
||||||
.direction(Direction::Horizontal)
|
- 2
|
||||||
.margin(1)
|
- (num_spaces * (required_columns - 1) + 10 * required_columns) as i64,
|
||||||
.split(draw_loc);
|
) as usize;
|
||||||
|
|
||||||
let num_spaces = 3;
|
let bar_length = allowed_width / required_columns;
|
||||||
// +9 due to 3 + 4 + 2 columns for the CPU name + percentage + bar bounds
|
|
||||||
let allowed_width = max(
|
|
||||||
0,
|
|
||||||
draw_loc.width as i64
|
|
||||||
- 2
|
|
||||||
- (num_spaces * (required_columns - 1) + 9 * required_columns) as i64,
|
|
||||||
) as usize;
|
|
||||||
|
|
||||||
let bar_length = allowed_width / required_columns;
|
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 cpu_index == 0 && app_state.app_config_fields.show_average_cpu {
|
|
||||||
"AVG".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 + (draw_loc.height - 2) as usize, 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 cpu_index == 0 && app_state.app_config_fields.show_average_cpu {
|
||||||
|
"AVG".to_string()
|
||||||
|
} else {
|
||||||
|
cpu_index.to_string()
|
||||||
|
},
|
||||||
|
"|".repeat(num_bars),
|
||||||
|
" ".repeat(bar_length - num_bars),
|
||||||
|
use_percentage.round(),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
Paragraph::new(cpu_column.iter())
|
|
||||||
.block(Block::default())
|
for (current_row, chunk) in chunks.iter().enumerate() {
|
||||||
.render(f, *chunk);
|
let start_index = (current_row * remaining_height) as usize;
|
||||||
|
let end_index = min(start_index + (draw_loc.height - 2) as usize, 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, *chunk);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1654,8 +1651,8 @@ impl Painter {
|
|||||||
.margin(1)
|
.margin(1)
|
||||||
.split(draw_loc);
|
.split(draw_loc);
|
||||||
|
|
||||||
// +9 due to 3 + 4 + 2 columns for the mem name + percentage + bar bounds
|
// +10 due to 4 + 4 + 2 columns for the name & space + percentage + bar bounds
|
||||||
let bar_length = max(0, draw_loc.width as i64 - 2 - 9 as i64) as usize;
|
let bar_length = max(0, draw_loc.width as i64 - 2 - 10 as i64) as usize;
|
||||||
let ram_use_percentage = if let Some(mem) = mem_data.last() {
|
let ram_use_percentage = if let Some(mem) = mem_data.last() {
|
||||||
mem.1
|
mem.1
|
||||||
} else {
|
} else {
|
||||||
@ -1669,15 +1666,13 @@ impl Painter {
|
|||||||
let num_bars_ram = calculate_basic_use_bars(ram_use_percentage, bar_length);
|
let num_bars_ram = calculate_basic_use_bars(ram_use_percentage, bar_length);
|
||||||
let num_bars_swap = calculate_basic_use_bars(swap_use_percentage, bar_length);
|
let num_bars_swap = calculate_basic_use_bars(swap_use_percentage, bar_length);
|
||||||
let mem_label = format!(
|
let mem_label = format!(
|
||||||
"{:3}[{}{}{:3.0}%]\n",
|
"RAM [{}{}{:3.0}%]\n",
|
||||||
"RAM",
|
|
||||||
"|".repeat(num_bars_ram),
|
"|".repeat(num_bars_ram),
|
||||||
" ".repeat(bar_length - num_bars_ram),
|
" ".repeat(bar_length - num_bars_ram),
|
||||||
ram_use_percentage.round(),
|
ram_use_percentage.round(),
|
||||||
);
|
);
|
||||||
let swap_label = format!(
|
let swap_label = format!(
|
||||||
"{:3}[{}{}{:3.0}%]\n",
|
"SWP [{}{}{:3.0}%]\n",
|
||||||
"SWP",
|
|
||||||
"|".repeat(num_bars_swap),
|
"|".repeat(num_bars_swap),
|
||||||
" ".repeat(bar_length - num_bars_swap),
|
" ".repeat(bar_length - num_bars_swap),
|
||||||
swap_use_percentage.round(),
|
swap_use_percentage.round(),
|
||||||
@ -1719,7 +1714,7 @@ impl Painter {
|
|||||||
.margin(1)
|
.margin(1)
|
||||||
.split(draw_loc);
|
.split(draw_loc);
|
||||||
|
|
||||||
// +9 due to 3 + 4 + 2 columns for the mem name + percentage + bar bounds
|
// +9 due to 3 + 4 + 2 columns for the name & space + percentage + bar bounds
|
||||||
let bar_length = max(0, draw_loc.width as i64 - 2 - 9 as i64) as usize;
|
let bar_length = max(0, draw_loc.width as i64 - 2 - 9 as i64) as usize;
|
||||||
let rx_use_percentage = if let Some(rx) = rx_data.last() {
|
let rx_use_percentage = if let Some(rx) = rx_data.last() {
|
||||||
rx.1
|
rx.1
|
||||||
@ -1734,15 +1729,13 @@ impl Painter {
|
|||||||
let num_bars_rx = calculate_basic_use_bars(rx_use_percentage, bar_length);
|
let num_bars_rx = calculate_basic_use_bars(rx_use_percentage, bar_length);
|
||||||
let num_bars_tx = calculate_basic_use_bars(tx_use_percentage, bar_length);
|
let num_bars_tx = calculate_basic_use_bars(tx_use_percentage, bar_length);
|
||||||
let rx_label = format!(
|
let rx_label = format!(
|
||||||
"{:3}[{}{}{:3.0}%]\n",
|
"RX [{}{}{:3.0}%]\n",
|
||||||
"RX",
|
|
||||||
"|".repeat(num_bars_rx),
|
"|".repeat(num_bars_rx),
|
||||||
" ".repeat(bar_length - num_bars_rx),
|
" ".repeat(bar_length - num_bars_rx),
|
||||||
rx_use_percentage.round(),
|
rx_use_percentage.round(),
|
||||||
);
|
);
|
||||||
let tx_label = format!(
|
let tx_label = format!(
|
||||||
"{:3}[{}{}{:3.0}%]\n",
|
"TX [{}{}{:3.0}%]\n",
|
||||||
"TX",
|
|
||||||
"|".repeat(num_bars_tx),
|
"|".repeat(num_bars_tx),
|
||||||
" ".repeat(bar_length - num_bars_tx),
|
" ".repeat(bar_length - num_bars_tx),
|
||||||
tx_use_percentage.round(),
|
tx_use_percentage.round(),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user