Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rewrite column algorithm #227

Merged
merged 12 commits into from
Sep 10, 2020
Prev Previous commit
Next Next commit
Finish resizing stuff
  • Loading branch information
ClementTsang committed Sep 9, 2020
commit 78ac4d318ba1966ba4d89d0f968e212c5cce67c3
9 changes: 6 additions & 3 deletions src/app/data_harvester/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use sysinfo::{ProcessorExt, System, SystemExt};

#[derive(Default, Debug, Clone)]
pub struct CpuData {
pub cpu_name: String,
pub cpu_prefix: String,
pub cpu_count: Option<usize>,
pub cpu_usage: f64,
}

Expand All @@ -15,14 +16,16 @@ pub fn get_cpu_data_list(sys: &System, show_average_cpu: bool) -> CpuHarvest {

if show_average_cpu {
cpu_vec.push(CpuData {
cpu_name: "AVG".to_string(),
cpu_prefix: "AVG".to_string(),
cpu_count: None,
cpu_usage: avg_cpu_usage as f64,
});
}

for (itx, cpu) in cpu_data.iter().enumerate() {
cpu_vec.push(CpuData {
cpu_name: format!("CPU{}", itx),
cpu_prefix: "CPU".to_string(),
cpu_count: Some(itx),
cpu_usage: f64::from(cpu.get_cpu_usage()),
});
}
Expand Down
3 changes: 1 addition & 2 deletions src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ pub struct DisplayableData {
pub temp_sensor_data: Vec<Vec<String>>,
pub single_process_data: Vec<ConvertedProcessData>, // Contains single process data
pub finalized_process_data_map: HashMap<u64, Vec<ConvertedProcessData>>, // What's actually displayed
pub stringified_process_data_map:
HashMap<u64, Vec<(Vec<(String, Option<String>, bool)>, bool)>>, // Represents the row and whether it is disabled
pub stringified_process_data_map: HashMap<u64, Vec<(Vec<(String, Option<String>)>, bool)>>, // Represents the row and whether it is disabled
pub mem_label_percent: String,
pub swap_label_percent: String,
pub mem_label_frac: String,
Expand Down
14 changes: 6 additions & 8 deletions src/canvas/drawing_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,14 @@ pub fn get_column_widths(
}
}

// Redistribute remaining; this goes between flex evenly.
// Redistribute remaining.
while total_width_left > 0 {
for itx in spacing_priority {
if let Some(col) = hard_widths.get(*itx) {
if col.is_none() && column_widths[*itx] > 0 {
column_widths[*itx] += 1;
total_width_left -= 1;
if total_width_left == 0 {
break;
}
if column_widths[*itx] > 0 {
column_widths[*itx] += 1;
total_width_left -= 1;
if total_width_left == 0 {
break;
}
}
}
Expand Down
51 changes: 35 additions & 16 deletions src/canvas/widgets/cpu_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,42 @@ impl CpuGraphWidget for Painter {
.saturating_sub(start_position);
let show_avg_cpu = app_state.app_config_fields.show_average_cpu;

// Calculate widths
if recalculate_column_widths {
cpu_widget_state.table_width_state.desired_column_widths = vec![6, 4];
cpu_widget_state.table_width_state.calculated_column_widths = get_column_widths(
draw_loc.width,
&[None, None],
&[Some(3), Some(4)],
&[Some(0.5), Some(0.5)],
&(cpu_widget_state
.table_width_state
.desired_column_widths
.iter()
.map(|width| Some(*width))
.collect::<Vec<_>>()),
&[1, 0],
);
}

let dcw = &cpu_widget_state.table_width_state.desired_column_widths;
let ccw = &cpu_widget_state.table_width_state.calculated_column_widths;
let cpu_rows = sliced_cpu_data.iter().enumerate().filter_map(|(itx, cpu)| {
let cpu_string_row: Vec<Cow<'_, str>> = vec![
Cow::Borrowed(&cpu.cpu_name),
Cow::Borrowed(&cpu.legend_value),
];
let truncated_name: Cow<'_, str> =
if let (Some(desired_column_width), Some(calculated_column_width)) =
(dcw.get(0), ccw.get(0))
{
if *desired_column_width > *calculated_column_width {
Cow::Borrowed(&cpu.short_cpu_name)
} else {
Cow::Borrowed(&cpu.cpu_name)
}
} else {
Cow::Borrowed(&cpu.cpu_name)
};

let cpu_string_row: Vec<Cow<'_, str>> =
vec![truncated_name, Cow::Borrowed(&cpu.legend_value)];

if cpu_string_row.is_empty() {
offset_scroll_index += 1;
Expand Down Expand Up @@ -336,18 +367,6 @@ impl CpuGraphWidget for Painter {
}
});

// Calculate widths
if recalculate_column_widths {
cpu_widget_state.table_width_state.calculated_column_widths = get_column_widths(
draw_loc.width,
&[None, None],
&[Some(3), Some(4)],
&[Some(0.5), Some(0.5)],
&[Some(6), Some(4)],
&[1, 0],
);
}

// Note we don't set highlight_style, as it should always be shown for this widget.
let border_and_title_style = if is_on_widget {
self.colours.highlighted_border_style
Expand Down
42 changes: 40 additions & 2 deletions src/canvas/widgets/disk_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ impl DiskTableWidget for Painter {
.saturating_sub(start_position),
));
let sliced_vec = &disk_data[start_position..];
let disk_rows = sliced_vec.iter().map(|disk| Row::Data(disk.iter()));

// Calculate widths
let hard_widths = [None, None, Some(4), Some(6), Some(6), Some(7), Some(7)];
if recalculate_column_widths {
disk_widget_state.table_width_state.desired_column_widths = {
let mut column_widths = DISK_HEADERS_LENS.clone();
Expand All @@ -80,7 +80,7 @@ impl DiskTableWidget for Painter {
};
disk_widget_state.table_width_state.calculated_column_widths = get_column_widths(
draw_loc.width,
&[None, None, Some(4), Some(6), Some(6), Some(7), Some(7)],
&hard_widths,
&(DISK_HEADERS_LENS
.iter()
.map(|w| Some(*w))
Expand All @@ -96,6 +96,44 @@ impl DiskTableWidget for Painter {
);
}

let dcw = &disk_widget_state.table_width_state.desired_column_widths;
let ccw = &disk_widget_state.table_width_state.calculated_column_widths;
let disk_rows =
sliced_vec.iter().map(|disk_row| {
let truncated_data = disk_row.iter().zip(&hard_widths).enumerate().map(
|(itx, (entry, width))| {
if width.is_none() {
if let (Some(desired_col_width), Some(calculated_col_width)) =
(dcw.get(itx), ccw.get(itx))
{
if *desired_col_width > *calculated_col_width
&& *calculated_col_width > 0
{
if entry.len() > *calculated_col_width as usize
&& *calculated_col_width > 1
{
// Truncate with ellipsis
let (first, _last) =
entry.split_at(*calculated_col_width as usize - 1);
format!("{}…", first)
} else {
entry.clone()
}
} else {
entry.clone()
}
} else {
entry.clone()
}
} else {
entry.clone()
}
},
);

Row::Data(truncated_data)
});

// TODO: This seems to be bugged? The selected text style gets "stuck"? I think this gets fixed with tui 0.10?
let (border_and_title_style, highlight_style) = if is_on_widget {
(
Expand Down
83 changes: 47 additions & 36 deletions src/canvas/widgets/process_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl ProcessTableWidget for Painter {
let processed_sliced_vec = sliced_vec.iter().map(|(data, disabled)| {
(
data.iter()
.map(|(entry, _alternative, _to_shorten)| entry)
.map(|(entry, _alternative)| entry)
.collect::<Vec<_>>(),
disabled,
)
Expand All @@ -225,6 +225,31 @@ impl ProcessTableWidget for Painter {
);

// Calculate widths
let hard_widths = if proc_widget_state.is_grouped {
vec![
Some(7),
None,
Some(8),
Some(8),
Some(8),
Some(8),
Some(7),
Some(8),
]
} else {
vec![
Some(7),
None,
Some(8),
Some(8),
Some(8),
Some(8),
Some(7),
Some(8),
None,
]
};

if recalculate_column_widths {
let mut column_widths = process_headers
.iter()
Expand Down Expand Up @@ -290,17 +315,7 @@ impl ProcessTableWidget for Painter {
proc_widget_state.table_width_state.calculated_column_widths =
get_column_widths(
draw_loc.width,
&[
Some(7),
None,
Some(8),
Some(8),
Some(8),
Some(8),
Some(7),
Some(8),
None,
],
&hard_widths,
&soft_widths_min,
&soft_widths_max,
&(proc_widget_state
Expand All @@ -311,50 +326,46 @@ impl ProcessTableWidget for Painter {
.collect::<Vec<_>>()),
&column_bias,
);

debug!(
"DCW: {:?}",
proc_widget_state.table_width_state.desired_column_widths
);
debug!(
"CCW: {:?}",
proc_widget_state.table_width_state.calculated_column_widths
);
}

let dcw = &proc_widget_state.table_width_state.desired_column_widths;
let ccw = &proc_widget_state.table_width_state.calculated_column_widths;

let process_rows = sliced_vec.iter().cloned().map(|(data, disabled)| {
let truncated_data = data.into_iter().enumerate().map(
|(itx, (entry, alternative, to_shorten))| {
if let Some(calculated_col_width) = ccw.get(itx) {
if to_shorten {
if entry.len() > *calculated_col_width as usize
let process_rows = sliced_vec.iter().map(|(data, disabled)| {
let truncated_data = data.iter().zip(&hard_widths).enumerate().map(
|(itx, ((entry, alternative), width))| {
if let (Some(desired_col_width), Some(calculated_col_width)) =
(dcw.get(itx), ccw.get(itx))
{
if width.is_none() {
if *desired_col_width > *calculated_col_width
&& *calculated_col_width > 0
{
if let Some(alternative) = alternative {
alternative
} else if *calculated_col_width > 3 {
alternative.clone()
} else if entry.len() > *calculated_col_width as usize
&& *calculated_col_width > 1
{
// Truncate with ellipsis
let (first, _last) =
entry.split_at(*calculated_col_width as usize - 3);
format!("{}...", first)
entry.split_at(*calculated_col_width as usize - 1);
format!("{}", first)
} else {
entry
entry.clone()
}
} else {
entry
entry.clone()
}
} else {
entry
entry.clone()
}
} else {
entry
entry.clone()
}
},
);

if disabled {
if *disabled {
Row::StyledData(truncated_data, self.colours.disabled_text_style)
} else {
Row::Data(truncated_data)
Expand Down
42 changes: 40 additions & 2 deletions src/canvas/widgets/temp_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ impl TempTableWidget for Painter {
.saturating_sub(start_position),
));
let sliced_vec = &temp_sensor_data[start_position..];
let temperature_rows = sliced_vec.iter().map(|temp_row| Row::Data(temp_row.iter()));

// Calculate widths
let hard_widths = [None, None];
if recalculate_column_widths {
temp_widget_state.table_width_state.desired_column_widths = {
let mut column_widths = TEMP_HEADERS_LENS.clone();
Expand All @@ -80,7 +80,7 @@ impl TempTableWidget for Painter {
};
temp_widget_state.table_width_state.calculated_column_widths = get_column_widths(
draw_loc.width,
&[None, None],
&hard_widths,
&(TEMP_HEADERS_LENS
.iter()
.map(|width| Some(*width))
Expand All @@ -96,6 +96,44 @@ impl TempTableWidget for Painter {
);
}

let dcw = &temp_widget_state.table_width_state.desired_column_widths;
let ccw = &temp_widget_state.table_width_state.calculated_column_widths;
let temperature_rows =
sliced_vec.iter().map(|temp_row| {
let truncated_data = temp_row.iter().zip(&hard_widths).enumerate().map(
|(itx, (entry, width))| {
if width.is_none() {
if let (Some(desired_col_width), Some(calculated_col_width)) =
(dcw.get(itx), ccw.get(itx))
{
if *desired_col_width > *calculated_col_width
&& *calculated_col_width > 0
{
if entry.len() > *calculated_col_width as usize
&& *calculated_col_width > 1
{
// Truncate with ellipsis
let (first, _last) =
entry.split_at(*calculated_col_width as usize - 1);
format!("{}…", first)
} else {
entry.clone()
}
} else {
entry.clone()
}
} else {
entry.clone()
}
} else {
entry.clone()
}
},
);

Row::Data(truncated_data)
});

let (border_and_title_style, highlight_style) = if is_on_widget {
(
self.colours.highlighted_border_style,
Expand Down
Loading