mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-27 15:44:17 +02:00
refactor: don't draw header if too short
This commit is contained in:
parent
c296b8bf5a
commit
2e51590bf5
@ -117,9 +117,7 @@ impl TableComponentState {
|
|||||||
|
|
||||||
/// Calculates widths for the columns for this table.
|
/// Calculates widths for the columns for this table.
|
||||||
///
|
///
|
||||||
/// * `total_width` is the, well, total width available. **NOTE:** This function automatically
|
/// * `total_width` is the, well, total width available.
|
||||||
/// takes away 2 from the width as part of the left/right
|
|
||||||
/// bounds.
|
|
||||||
/// * `left_to_right` is a boolean whether to go from left to right if true, or right to left if
|
/// * `left_to_right` is a boolean whether to go from left to right if true, or right to left if
|
||||||
/// false.
|
/// false.
|
||||||
///
|
///
|
||||||
@ -128,9 +126,7 @@ impl TableComponentState {
|
|||||||
use itertools::Either;
|
use itertools::Either;
|
||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
if total_width > 2 {
|
let mut total_width_left = total_width;
|
||||||
let initial_width = total_width - 2;
|
|
||||||
let mut total_width_left = initial_width;
|
|
||||||
|
|
||||||
let column_widths = &mut self.calculated_widths;
|
let column_widths = &mut self.calculated_widths;
|
||||||
*column_widths = vec![0; self.columns.len()];
|
*column_widths = vec![0; self.columns.len()];
|
||||||
@ -151,7 +147,7 @@ impl TableComponentState {
|
|||||||
let soft_limit = max(
|
let soft_limit = max(
|
||||||
if let Some(max_percentage) = max_percentage {
|
if let Some(max_percentage) = max_percentage {
|
||||||
// Rust doesn't have an `into()` or `try_into()` for floats to integers???
|
// Rust doesn't have an `into()` or `try_into()` for floats to integers???
|
||||||
((*max_percentage * f32::from(initial_width)).ceil()) as u16
|
((*max_percentage * f32::from(total_width)).ceil()) as u16
|
||||||
} else {
|
} else {
|
||||||
*desired
|
*desired
|
||||||
},
|
},
|
||||||
@ -196,7 +192,6 @@ impl TableComponentState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Updates the position if possible, and if there is a valid change, returns the new position.
|
/// Updates the position if possible, and if there is a valid change, returns the new position.
|
||||||
pub fn update_position(&mut self, change: i64, num_entries: usize) -> Option<usize> {
|
pub fn update_position(&mut self, change: i64, num_entries: usize) -> Option<usize> {
|
||||||
|
@ -19,7 +19,6 @@ use crate::{
|
|||||||
|
|
||||||
pub struct TextTable<'a> {
|
pub struct TextTable<'a> {
|
||||||
pub table_gap: u16,
|
pub table_gap: u16,
|
||||||
pub table_height_offset: u16,
|
|
||||||
pub is_force_redraw: bool,
|
pub is_force_redraw: bool,
|
||||||
pub recalculate_column_widths: bool,
|
pub recalculate_column_widths: bool,
|
||||||
|
|
||||||
@ -100,16 +99,54 @@ impl<'a> TextTable<'a> {
|
|||||||
&self, f: &mut Frame<'_, B>, draw_loc: Rect, state: &mut TableComponentState,
|
&self, f: &mut Frame<'_, B>, draw_loc: Rect, state: &mut TableComponentState,
|
||||||
table_data: &TableData,
|
table_data: &TableData,
|
||||||
) -> Rect {
|
) -> Rect {
|
||||||
let table_gap = if draw_loc.height < TABLE_GAP_HEIGHT_LIMIT {
|
let margined_draw_loc = Layout::default()
|
||||||
|
.constraints([Constraint::Percentage(100)])
|
||||||
|
.horizontal_margin(if self.is_on_widget || self.draw_border {
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
1
|
||||||
|
})
|
||||||
|
.direction(Direction::Horizontal)
|
||||||
|
.split(draw_loc)[0];
|
||||||
|
|
||||||
|
let disk_block = if self.draw_border {
|
||||||
|
let title = self.generate_title(
|
||||||
|
draw_loc,
|
||||||
|
state.current_scroll_position.saturating_add(1),
|
||||||
|
table_data.data.len(),
|
||||||
|
);
|
||||||
|
|
||||||
|
Block::default()
|
||||||
|
.title(title)
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_style(self.border_style)
|
||||||
|
} else if self.is_on_widget {
|
||||||
|
Block::default()
|
||||||
|
.borders(SIDE_BORDERS)
|
||||||
|
.border_style(self.border_style)
|
||||||
|
} else {
|
||||||
|
Block::default().borders(Borders::NONE)
|
||||||
|
};
|
||||||
|
|
||||||
|
let (inner_width, inner_height) = {
|
||||||
|
let inner = disk_block.inner(margined_draw_loc);
|
||||||
|
(inner.width, inner.height)
|
||||||
|
};
|
||||||
|
|
||||||
|
if inner_width == 0 || inner_height == 0 {
|
||||||
|
f.render_widget(disk_block, margined_draw_loc);
|
||||||
|
margined_draw_loc
|
||||||
|
} else {
|
||||||
|
let show_header = inner_height > 1;
|
||||||
|
let header_height = if show_header { 1 } else { 0 };
|
||||||
|
let table_gap = if !show_header || draw_loc.height < TABLE_GAP_HEIGHT_LIMIT {
|
||||||
0
|
0
|
||||||
} else {
|
} else {
|
||||||
self.table_gap
|
self.table_gap
|
||||||
};
|
};
|
||||||
|
|
||||||
let sliced_vec = {
|
let sliced_vec = {
|
||||||
let num_rows = usize::from(
|
let num_rows = usize::from(inner_height.saturating_sub(table_gap + header_height));
|
||||||
(draw_loc.height + 1 - table_gap).saturating_sub(self.table_height_offset),
|
|
||||||
);
|
|
||||||
let start = get_start_position(
|
let start = get_start_position(
|
||||||
num_rows,
|
num_rows,
|
||||||
&state.scroll_direction,
|
&state.scroll_direction,
|
||||||
@ -141,7 +178,7 @@ impl<'a> TextTable<'a> {
|
|||||||
app::WidthBounds::Hard(_width) => {}
|
app::WidthBounds::Hard(_width) => {}
|
||||||
});
|
});
|
||||||
|
|
||||||
state.calculate_column_widths(draw_loc.width, self.left_to_right);
|
state.calculate_column_widths(inner_width, self.left_to_right);
|
||||||
}
|
}
|
||||||
|
|
||||||
let columns = &state.columns;
|
let columns = &state.columns;
|
||||||
@ -158,43 +195,21 @@ impl<'a> TextTable<'a> {
|
|||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
let title = self.generate_title(
|
let widget = {
|
||||||
draw_loc,
|
let mut table = Table::new(disk_rows)
|
||||||
state.current_scroll_position.saturating_add(1),
|
.block(disk_block)
|
||||||
table_data.data.len(),
|
.highlight_style(self.highlighted_text_style)
|
||||||
);
|
.style(self.text_style);
|
||||||
|
|
||||||
let disk_block = if self.draw_border {
|
if show_header {
|
||||||
Block::default()
|
table = table.header(header);
|
||||||
.title(title)
|
}
|
||||||
.borders(Borders::ALL)
|
|
||||||
.border_style(self.border_style)
|
table
|
||||||
} else if self.is_on_widget {
|
|
||||||
Block::default()
|
|
||||||
.borders(SIDE_BORDERS)
|
|
||||||
.border_style(self.border_style)
|
|
||||||
} else {
|
|
||||||
Block::default().borders(Borders::NONE)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let margined_draw_loc = Layout::default()
|
|
||||||
.constraints([Constraint::Percentage(100)])
|
|
||||||
.horizontal_margin(if self.is_on_widget || self.draw_border {
|
|
||||||
0
|
|
||||||
} else {
|
|
||||||
1
|
|
||||||
})
|
|
||||||
.direction(Direction::Horizontal)
|
|
||||||
.split(draw_loc)[0];
|
|
||||||
|
|
||||||
// Draw!
|
|
||||||
f.render_stateful_widget(
|
f.render_stateful_widget(
|
||||||
Table::new(disk_rows)
|
widget.widths(
|
||||||
.block(disk_block)
|
|
||||||
.header(header)
|
|
||||||
.highlight_style(self.highlighted_text_style)
|
|
||||||
.style(self.text_style)
|
|
||||||
.widths(
|
|
||||||
&(widths
|
&(widths
|
||||||
.iter()
|
.iter()
|
||||||
.map(|w| Constraint::Length(*w))
|
.map(|w| Constraint::Length(*w))
|
||||||
@ -206,6 +221,7 @@ impl<'a> TextTable<'a> {
|
|||||||
|
|
||||||
margined_draw_loc
|
margined_draw_loc
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Truncates text if it is too long, and adds an ellipsis at the end if needed.
|
/// Truncates text if it is too long, and adds an ellipsis at the end if needed.
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
app::{layout_manager::WidgetDirection, App},
|
app::{layout_manager::WidgetDirection, App, CpuWidgetState},
|
||||||
canvas::{
|
canvas::{
|
||||||
components::{GraphData, TimeGraph},
|
components::{GraphData, TimeGraph},
|
||||||
drawing_utils::{get_column_widths, get_start_position, should_hide_x_label},
|
drawing_utils::{get_column_widths, get_start_position, should_hide_x_label},
|
||||||
@ -115,25 +115,12 @@ impl Painter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_cpu_graph<B: Backend>(
|
fn generate_points<'a>(
|
||||||
&self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, widget_id: u64,
|
&self, cpu_widget_state: &CpuWidgetState, cpu_data: &'a [ConvertedCpuData],
|
||||||
) {
|
show_avg_cpu: bool,
|
||||||
const Y_BOUNDS: [f64; 2] = [0.0, 100.5];
|
) -> Vec<GraphData<'a>> {
|
||||||
const Y_LABELS: [Cow<'static, str>; 2] = [Cow::Borrowed(" 0%"), Cow::Borrowed("100%")];
|
|
||||||
|
|
||||||
if let Some(cpu_widget_state) = app_state.cpu_state.widget_states.get_mut(&widget_id) {
|
|
||||||
let cpu_data = &app_state.canvas_data.cpu_data;
|
|
||||||
let border_style = self.get_border_style(widget_id, app_state.current_widget.widget_id);
|
|
||||||
let x_bounds = [0, cpu_widget_state.current_display_time];
|
|
||||||
let hide_x_labels = should_hide_x_label(
|
|
||||||
app_state.app_config_fields.hide_time,
|
|
||||||
app_state.app_config_fields.autohide_time,
|
|
||||||
&mut cpu_widget_state.autohide_timer,
|
|
||||||
draw_loc,
|
|
||||||
);
|
|
||||||
let show_avg_cpu = app_state.app_config_fields.show_average_cpu;
|
|
||||||
let show_avg_offset = if show_avg_cpu { AVG_POSITION } else { 0 };
|
let show_avg_offset = if show_avg_cpu { AVG_POSITION } else { 0 };
|
||||||
let points = {
|
|
||||||
let current_scroll_position = cpu_widget_state.scroll_state.current_scroll_position;
|
let current_scroll_position = cpu_widget_state.scroll_state.current_scroll_position;
|
||||||
if current_scroll_position == ALL_POSITION {
|
if current_scroll_position == ALL_POSITION {
|
||||||
// This case ensures the other cases cannot have the position be equal to 0.
|
// This case ensures the other cases cannot have the position be equal to 0.
|
||||||
@ -164,8 +151,8 @@ impl Painter {
|
|||||||
self.colours.avg_colour_style
|
self.colours.avg_colour_style
|
||||||
} else {
|
} else {
|
||||||
let offset_position = current_scroll_position - 1; // Because of the all position
|
let offset_position = current_scroll_position - 1; // Because of the all position
|
||||||
self.colours.cpu_colour_styles[(offset_position - show_avg_offset)
|
self.colours.cpu_colour_styles
|
||||||
% self.colours.cpu_colour_styles.len()]
|
[(offset_position - show_avg_offset) % self.colours.cpu_colour_styles.len()]
|
||||||
};
|
};
|
||||||
|
|
||||||
vec![GraphData {
|
vec![GraphData {
|
||||||
@ -176,7 +163,30 @@ impl Painter {
|
|||||||
} else {
|
} else {
|
||||||
vec![]
|
vec![]
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
fn draw_cpu_graph<B: Backend>(
|
||||||
|
&self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, widget_id: u64,
|
||||||
|
) {
|
||||||
|
const Y_BOUNDS: [f64; 2] = [0.0, 100.5];
|
||||||
|
const Y_LABELS: [Cow<'static, str>; 2] = [Cow::Borrowed(" 0%"), Cow::Borrowed("100%")];
|
||||||
|
|
||||||
|
if let Some(cpu_widget_state) = app_state.cpu_state.widget_states.get_mut(&widget_id) {
|
||||||
|
let cpu_data = &app_state.canvas_data.cpu_data;
|
||||||
|
let border_style = self.get_border_style(widget_id, app_state.current_widget.widget_id);
|
||||||
|
let x_bounds = [0, cpu_widget_state.current_display_time];
|
||||||
|
let hide_x_labels = should_hide_x_label(
|
||||||
|
app_state.app_config_fields.hide_time,
|
||||||
|
app_state.app_config_fields.autohide_time,
|
||||||
|
&mut cpu_widget_state.autohide_timer,
|
||||||
|
draw_loc,
|
||||||
|
);
|
||||||
|
|
||||||
|
let points = self.generate_points(
|
||||||
|
&cpu_widget_state,
|
||||||
|
cpu_data,
|
||||||
|
app_state.app_config_fields.show_average_cpu,
|
||||||
|
);
|
||||||
|
|
||||||
// TODO: Maybe hide load avg if too long? Or maybe the CPU part.
|
// TODO: Maybe hide load avg if too long? Or maybe the CPU part.
|
||||||
let title = if cfg!(target_family = "unix") {
|
let title = if cfg!(target_family = "unix") {
|
||||||
|
@ -24,7 +24,6 @@ impl Painter {
|
|||||||
};
|
};
|
||||||
let margined_draw_loc = TextTable {
|
let margined_draw_loc = TextTable {
|
||||||
table_gap: app_state.app_config_fields.table_gap,
|
table_gap: app_state.app_config_fields.table_gap,
|
||||||
table_height_offset: self.table_height_offset,
|
|
||||||
is_force_redraw: app_state.is_force_redraw,
|
is_force_redraw: app_state.is_force_redraw,
|
||||||
recalculate_column_widths,
|
recalculate_column_widths,
|
||||||
header_style: self.colours.table_header_style,
|
header_style: self.colours.table_header_style,
|
||||||
|
@ -24,7 +24,6 @@ impl Painter {
|
|||||||
};
|
};
|
||||||
let margined_draw_loc = TextTable {
|
let margined_draw_loc = TextTable {
|
||||||
table_gap: app_state.app_config_fields.table_gap,
|
table_gap: app_state.app_config_fields.table_gap,
|
||||||
table_height_offset: self.table_height_offset,
|
|
||||||
is_force_redraw: app_state.is_force_redraw,
|
is_force_redraw: app_state.is_force_redraw,
|
||||||
recalculate_column_widths,
|
recalculate_column_widths,
|
||||||
header_style: self.colours.table_header_style,
|
header_style: self.colours.table_header_style,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user