summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorClement Tsang <34804052+ClementTsang@users.noreply.github.com>2022-11-06 01:34:32 -0500
committerGitHub <noreply@github.com>2022-11-06 01:34:32 -0500
commit36837ae8acddf4ed19162c22114c30b07c2adbac (patch)
tree19c5524e343f8261b1dd76a9d7278ece44a9b5ae /src
parent4d9f5093b22a700244c4349a86d42b4d1ecbe8c6 (diff)
feature: basic sortable disk (#870)
Diffstat (limited to 'src')
-rw-r--r--src/app.rs68
-rw-r--r--src/app/widgets/cpu_graph.rs2
-rw-r--r--src/app/widgets/disk_table.rs102
-rw-r--r--src/bin/main.rs4
-rw-r--r--src/lib.rs7
5 files changed, 148 insertions, 35 deletions
diff --git a/src/app.rs b/src/app.rs
index 9029db5e..4bab7b2a 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -1229,6 +1229,12 @@ impl App {
self.awaiting_second_char = true;
self.second_char = Some('d');
}
+ } else if let Some(disk) = self
+ .disk_state
+ .get_mut_widget_state(self.current_widget.widget_id)
+ {
+ disk.table.set_sort_index(0);
+ disk.force_data_update();
}
}
'g' => {
@@ -1271,6 +1277,12 @@ impl App {
{
proc_widget_state.select_column(ProcWidget::MEM);
}
+ } else if let Some(disk) = self
+ .disk_state
+ .get_mut_widget_state(self.current_widget.widget_id)
+ {
+ disk.table.set_sort_index(1);
+ disk.force_data_update();
}
}
'p' => {
@@ -1301,6 +1313,12 @@ impl App {
{
proc_widget_state.select_column(ProcWidget::PROC_NAME_OR_CMD);
}
+ } else if let Some(disk) = self
+ .disk_state
+ .get_mut_widget_state(self.current_widget.widget_id)
+ {
+ disk.table.set_sort_index(3);
+ disk.force_data_update();
}
}
'?' => {
@@ -1320,7 +1338,12 @@ impl App {
{
temp.table.set_sort_index(1);
temp.force_data_update();
- self.is_force_redraw = true;
+ } else if let Some(disk) = self
+ .disk_state
+ .get_mut_widget_state(self.current_widget.widget_id)
+ {
+ disk.table.set_sort_index(4);
+ disk.force_data_update();
}
}
'+' => self.on_plus(),
@@ -1339,6 +1362,33 @@ impl App {
self.is_force_redraw = true;
}
}
+ 'u' => {
+ if let Some(disk) = self
+ .disk_state
+ .get_mut_widget_state(self.current_widget.widget_id)
+ {
+ disk.table.set_sort_index(2);
+ disk.force_data_update();
+ }
+ }
+ 'r' => {
+ if let Some(disk) = self
+ .disk_state
+ .get_mut_widget_state(self.current_widget.widget_id)
+ {
+ disk.table.set_sort_index(5);
+ disk.force_data_update();
+ }
+ }
+ 'w' => {
+ if let Some(disk) = self
+ .disk_state
+ .get_mut_widget_state(self.current_widget.widget_id)
+ {
+ disk.table.set_sort_index(6);
+ disk.force_data_update();
+ }
+ }
'I' => self.invert_sort(),
'%' => self.toggle_percentages(),
_ => {}
@@ -2597,12 +2647,22 @@ impl App {
}
}
BottomWidgetType::Temp => {
- if let Some(state) = self
+ if let Some(temp) = self
.temp_state
.get_mut_widget_state(self.current_widget.widget_id)
{
- if state.table.try_select_location(x, y).is_some() {
- state.force_data_update();
+ if temp.table.try_select_location(x, y).is_some() {
+ temp.force_data_update();
+ }
+ }
+ }
+ BottomWidgetType::Disk => {
+ if let Some(disk) = self
+ .disk_state
+ .get_mut_widget_state(self.current_widget.widget_id)
+ {
+ if disk.table.try_select_location(x, y).is_some() {
+ disk.force_data_update();
}
}
}
diff --git a/src/app/widgets/cpu_graph.rs b/src/app/widgets/cpu_graph.rs
index ca520ad5..5e895edc 100644
--- a/src/app/widgets/cpu_graph.rs
+++ b/src/app/widgets/cpu_graph.rs
@@ -195,7 +195,7 @@ impl CpuWidgetState {
}
}
- pub fn ingest_data(&mut self, data: &[CpuWidgetData]) {
+ pub fn update_table(&mut self, data: &[CpuWidgetData]) {
self.table.set_data(
data.iter()
.map(CpuWidgetTableData::from_cpu_widget_data)
diff --git a/src/app/widgets/disk_table.rs b/src/app/widgets/disk_table.rs
index 0749a0d6..5ca45b78 100644
--- a/src/app/widgets/disk_table.rs
+++ b/src/app/widgets/disk_table.rs
@@ -7,10 +7,10 @@ use crate::{
app::AppConfigFields,
canvas::canvas_colours::CanvasColours,
components::data_table::{
- Column, ColumnHeader, DataTable, DataTableColumn, DataTableProps, DataTableStyling,
- DataToCell,
+ ColumnHeader, DataTableColumn, DataTableProps, DataTableStyling, DataToCell, SortColumn,
+ SortDataTable, SortDataTableProps, SortOrder, SortsRow,
},
- utils::gen_util::{get_decimal_bytes, truncate_text},
+ utils::gen_util::{get_decimal_bytes, sort_partial_fn, truncate_text},
};
#[derive(Clone)]
@@ -69,13 +69,13 @@ pub enum DiskWidgetColumn {
impl ColumnHeader for DiskWidgetColumn {
fn text(&self) -> Cow<'static, str> {
match self {
- DiskWidgetColumn::Disk => "Disk",
- DiskWidgetColumn::Mount => "Mount",
- DiskWidgetColumn::Used => "Used",
- DiskWidgetColumn::Free => "Free",
- DiskWidgetColumn::Total => "Total",
- DiskWidgetColumn::IoRead => "R/s",
- DiskWidgetColumn::IoWrite => "W/s",
+ DiskWidgetColumn::Disk => "Disk(d)",
+ DiskWidgetColumn::Mount => "Mount(m)",
+ DiskWidgetColumn::Used => "Used(u)",
+ DiskWidgetColumn::Free => "Free(n)",
+ DiskWidgetColumn::Total => "Total(t)",
+ DiskWidgetColumn::IoRead => "R/s(r)",
+ DiskWidgetColumn::IoWrite => "W/s(w)",
}
.into()
}
@@ -114,38 +114,84 @@ impl DataToCell<DiskWidgetColumn> for DiskWidgetData {
}
pub struct DiskTableWidget {
- pub table: DataTable<DiskWidgetData, DiskWidgetColumn>,
+ pub table: SortDataTable<DiskWidgetData, DiskWidgetColumn>,
+ pub force_update_data: bool,
+}
+
+impl SortsRow for DiskWidgetColumn {
+ type DataType = DiskWidgetData;
+
+ fn sort_data(&self, data: &mut [Self::DataType], descending: bool) {
+ match self {
+ DiskWidgetColumn::Disk => {
+ data.sort_by(|a, b| sort_partial_fn(descending)(&a.name, &b.name));
+ }
+ DiskWidgetColumn::Mount => {
+ data.sort_by(|a, b| sort_partial_fn(descending)(&a.mount_point, &b.mount_point));
+ }
+ DiskWidgetColumn::Used => {
+ data.sort_by(|a, b| sort_partial_fn(descending)(&a.used_bytes, &b.used_bytes));
+ }
+ DiskWidgetColumn::Free => {
+ data.sort_by(|a, b| sort_partial_fn(descending)(&a.free_bytes, &b.free_bytes));
+ }
+ DiskWidgetColumn::Total => {
+ data.sort_by(|a, b| sort_partial_fn(descending)(&a.total_bytes, &b.total_bytes));
+ }
+ DiskWidgetColumn::IoRead => {
+ data.sort_by(|a, b| sort_partial_fn(descending)(&a.io_read, &b.io_read));
+ }
+ DiskWidgetColumn::IoWrite => {
+ data.sort_by(|a, b| sort_partial_fn(descending)(&a.io_write, &b.io_write));
+ }
+ }
+ }
}
impl DiskTableWidget {
pub fn new(config: &AppConfigFields, colours: &CanvasColours) -> Self {
- const COLUMNS: [Column<DiskWidgetColumn>; 7] = [
- Column::soft(DiskWidgetColumn::Disk, Some(0.2)),
- Column::soft(DiskWidgetColumn::Mount, Some(0.2)),
- Column::hard(DiskWidgetColumn::Used, 4),
- Column::hard(DiskWidgetColumn::Free, 6),
- Column::hard(DiskWidgetColumn::Total, 6),
- Column::hard(DiskWidgetColumn::IoRead, 7),
- Column::hard(DiskWidgetColumn::IoWrite, 7),
+ let columns = [
+ SortColumn::soft(DiskWidgetColumn::Disk, Some(0.2)),
+ SortColumn::soft(DiskWidgetColumn::Mount, Some(0.2)),
+ SortColumn::hard(DiskWidgetColumn::Used, 7).default_descending(),
+ SortColumn::hard(DiskWidgetColumn::Free, 7).default_descending(),
+ SortColumn::hard(DiskWidgetColumn::Total, 8).default_descending(),
+ SortColumn::hard(DiskWidgetColumn::IoRead, 9).default_descending(),
+ SortColumn::hard(DiskWidgetColumn::IoWrite, 10).default_descending(),
];
- let props = DataTableProps {
- title: Some(" Disks ".into()),
- table_gap: config.table_gap,
- left_to_right: true,
- is_basic: config.use_basic_mode,
- show_table_scroll_position: config.show_table_scroll_position,
- show_current_entry_when_unfocused: false,
+ let props = SortDataTableProps {
+ inner: DataTableProps {
+ title: Some(" Disks ".into()),
+ table_gap: config.table_gap,
+ left_to_right: true,
+ is_basic: config.use_basic_mode,
+ show_table_scroll_position: config.show_table_scroll_position,
+ show_current_entry_when_unfocused: false,
+ },
+ sort_index: 0,
+ order: SortOrder::Ascending,
};
let styling = DataTableStyling::from_colours(colours);
Self {
- table: DataTable::new(COLUMNS, props, styling),
+ table: SortDataTable::new_sortable(columns, props, styling),
+ force_update_data: false,
}
}
+ /// Forces an update of the data stored.
+ #[inline]
+ pub fn force_data_update(&mut self) {
+ self.force_update_data = true;
+ }
+
pub fn ingest_data(&mut self, data: &[DiskWidgetData]) {
- self.table.set_data(data.to_vec());
+ let mut data = data.to_vec();
+ if let Some(column) = self.table.columns.get(self.table.sort_index()) {
+ column.sort_by(&mut data, self.table.order());
+ }
+ self.table.set_data(data);
}
}
diff --git a/src/bin/main.rs b/src/bin/main.rs
index a5e87819..9c3e8f94 100644
--- a/src/bin/main.rs
+++ b/src/bin/main.rs
@@ -189,6 +189,10 @@ fn main() -> Result<()> {
// Disk
if app.used_widgets.use_disk {
app.converted_data.ingest_disk_data(&app.data_collection);
+
+ for disk in app.disk_state.widget_states.values_mut() {
+ disk.force_data_update();
+ }
}
// Temperatures
diff --git a/src/lib.rs b/src/lib.rs
index 8d5db159..2ed7306c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -347,7 +347,7 @@ pub fn update_data(app: &mut App) {
{
let data = &app.converted_data.cpu_data;
for cpu in app.cpu_state.widget_states.values_mut() {
- cpu.ingest_data(data)
+ cpu.update_table(data)
}
}
{
@@ -362,7 +362,10 @@ pub fn update_data(app: &mut App) {
{
let data = &app.converted_data.disk_data;
for disk in app.disk_state.widget_states.values_mut() {
- disk.ingest_data(data);
+ if disk.force_update_data {
+ disk.ingest_data(data);
+ disk.force_update_data = false;
+ }
}
}