Skip to content

Commit

Permalink
fix broken ltr calculation and CPU hiding
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementTsang committed Sep 27, 2022
1 parent 841473f commit ef60e22
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
7 changes: 0 additions & 7 deletions src/app/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ pub enum CursorDirection {
Right,
}

/// Meant for canvas operations involving table column widths.
#[derive(Default)]
pub struct CanvasTableWidthState {
pub desired_column_widths: Vec<u16>,
pub calculated_column_widths: Vec<u16>,
}

#[derive(PartialEq)]
pub enum KillSignal {
Cancel,
Expand Down
43 changes: 30 additions & 13 deletions src/app/widgets/cpu_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ impl ColumnHeader for CpuWidgetColumn {
impl DataToCell<CpuWidgetColumn> for CpuWidgetData {
fn to_cell<'a>(&'a self, column: &CpuWidgetColumn, calculated_width: u16) -> Option<Text<'a>> {
// FIXME: Adjust based on column widths

const CPU_HIDE_BREAKPOINT: u16 = 5;

// This is a bit of a hack, but apparently we can avoid having to do any fancy checks
// of showing the "All" on a specific column if the other is hidden by just always
// showing it on the CPU (first) column - if there isn't room for it, it will just collapse
// down.
//
// This is the same for the use percentages - we just *always* show them, and *always* hide the CPU column if
// it is too small.
match &self.data {
CpuWidgetDataType::All => match column {
CpuWidgetColumn::CPU => Some(truncate_text("All", calculated_width)),
Expand All @@ -66,19 +74,28 @@ impl DataToCell<CpuWidgetColumn> for CpuWidgetData {
data,
last_entry,
} => match column {
CpuWidgetColumn::CPU => match data_type {
CpuDataType::Avg => Some(truncate_text("AVG", calculated_width)),
CpuDataType::Cpu(index) => {
let index_str = index.to_string();
let text = if calculated_width < 5 {
truncate_text(&index_str, calculated_width)
} else {
truncate_text(&concat_string!("CPU", index_str), calculated_width)
};

Some(text)
CpuWidgetColumn::CPU => {
if calculated_width == 0 {
None
} else {
match data_type {
CpuDataType::Avg => Some(truncate_text("AVG", calculated_width)),
CpuDataType::Cpu(index) => {
let index_str = index.to_string();
let text = if calculated_width < CPU_HIDE_BREAKPOINT {
truncate_text(&index_str, calculated_width)
} else {
truncate_text(
&concat_string!("CPU", index_str),
calculated_width,
)
};

Some(text)
}
}
}
},
}
CpuWidgetColumn::Use => Some(truncate_text(
&format!("{:.0}%", last_entry.round()),
calculated_width,
Expand Down
21 changes: 7 additions & 14 deletions src/components/data_table/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ where
use itertools::Either;

let mut total_width_left = total_width;
let mut calculated_widths = Vec::with_capacity(self.len());
let mut calculated_widths = vec![0; self.len()];
let columns = if left_to_right {
Either::Left(self.iter())
Either::Left(self.iter().zip(calculated_widths.iter_mut()))
} else {
Either::Right(self.iter().rev())
Either::Right(self.iter().zip(calculated_widths.iter_mut()).rev())
};

let mut num_columns = 0;
for column in columns {
for (column, calculated_width) in columns {
if column.is_hidden() {
continue;
}
Expand All @@ -184,13 +184,12 @@ where
min_width,
);
let space_taken = min(min(soft_limit, *desired), total_width_left);
debug!("col - {:?}, soft_limit: {soft_limit}, desired: {desired}, twl: {total_width_left}, space_taken: {space_taken}", column.header());

if min_width > space_taken || min_width == 0 {
break;
} else if space_taken > 0 {
total_width_left = total_width_left.saturating_sub(space_taken + 1);
calculated_widths.push(space_taken);
*calculated_width = space_taken;
num_columns += 1;
}
}
Expand All @@ -200,7 +199,7 @@ where
break;
} else if min_width > 0 {
total_width_left = total_width_left.saturating_sub(min_width + 1);
calculated_widths.push(min_width);
*calculated_width = min_width;
num_columns += 1;
}
}
Expand All @@ -210,15 +209,13 @@ where
break;
} else if min_width > 0 {
total_width_left = total_width_left.saturating_sub(min_width + 1);
calculated_widths.push(min_width);
*calculated_width = min_width;
num_columns += 1;
}
}
}
}

calculated_widths.shrink_to_fit();

if num_columns > 0 {
// Redistribute remaining.
let mut num_dist = num_columns;
Expand All @@ -243,10 +240,6 @@ where
}
}

if !left_to_right {
calculated_widths.reverse();
}

calculated_widths
}
}
1 change: 1 addition & 0 deletions src/components/data_table/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use concat_string::concat_string;
use tui::{
backend::Backend,
layout::{Constraint, Direction, Layout, Rect},
style::Style,
text::{Span, Spans, Text},
widgets::{Block, Borders, Row, Table},
Frame,
Expand Down

0 comments on commit ef60e22

Please sign in to comment.