wip: sort by
This commit is contained in:
+136
-96
@@ -1,4 +1,5 @@
|
||||
use bollard::models::ContainerSummary;
|
||||
use core::fmt;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use tui::widgets::ListItem;
|
||||
|
||||
@@ -16,31 +17,58 @@ pub struct AppData {
|
||||
pub containers: StatefulList<ContainerItem>,
|
||||
pub init: bool,
|
||||
pub show_error: bool,
|
||||
// todo
|
||||
sort_by: Header
|
||||
sorted_by: Option<(Header, SortedOrder)>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum SortedOrder{
|
||||
Asc,
|
||||
Desc
|
||||
pub enum SortedOrder {
|
||||
Asc,
|
||||
Desc,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Header{
|
||||
State,
|
||||
Status,
|
||||
Cpu,
|
||||
Memory,
|
||||
Id,
|
||||
Name,
|
||||
Image,
|
||||
Rx,
|
||||
Tx
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Header {
|
||||
State,
|
||||
Status,
|
||||
Cpu,
|
||||
Memory,
|
||||
Id,
|
||||
Name,
|
||||
Image,
|
||||
Rx,
|
||||
Tx,
|
||||
}
|
||||
|
||||
/// Convert errors into strings to display
|
||||
impl fmt::Display for Header {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let disp = match self {
|
||||
Self::State => "state",
|
||||
Self::Status => "status",
|
||||
Self::Cpu => "cpu",
|
||||
Self::Memory => "memory/limit",
|
||||
Self::Id => "id",
|
||||
Self::Name => "name",
|
||||
Self::Image => "image",
|
||||
Self::Rx => "↓ rx",
|
||||
Self::Tx => "↑ tx",
|
||||
};
|
||||
write!(f, "{:>x$}", disp, x = f.width().unwrap_or(1))
|
||||
}
|
||||
}
|
||||
|
||||
impl AppData {
|
||||
pub fn get_sorted(&self) -> Option<(Header, SortedOrder)> {
|
||||
self.sorted_by.clone()
|
||||
}
|
||||
|
||||
/// Change the sorted order, also set the selected pointer!
|
||||
pub fn set_sorted(&mut self, x: Option<(Header, SortedOrder)>) {
|
||||
self.sorted_by = x;
|
||||
let id = self.get_selected_container_id();
|
||||
self.sort_containers();
|
||||
self.containers.state.select(self.containers.items.iter().position(|i| Some(i.id.to_owned()) == id));
|
||||
}
|
||||
/// Generate a default app_state
|
||||
pub fn default(args: CliArgs) -> Self {
|
||||
Self {
|
||||
@@ -50,7 +78,7 @@ impl AppData {
|
||||
init: false,
|
||||
logs_parsed: false,
|
||||
show_error: false,
|
||||
sort_by: Header::Memory,
|
||||
sorted_by: Some((Header::Memory, SortedOrder::Asc)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,85 +169,97 @@ impl AppData {
|
||||
output
|
||||
}
|
||||
|
||||
|
||||
pub fn sort_containers(&mut self, so: SortedOrder) {
|
||||
|
||||
// State,
|
||||
// Status,
|
||||
// Cpu,
|
||||
// Memory,
|
||||
// Id,
|
||||
// Name,
|
||||
// Image,
|
||||
// Rx,
|
||||
// Tx
|
||||
match self.sort_by {
|
||||
Header::State => {
|
||||
match so {
|
||||
SortedOrder::Asc => self.containers.items.sort_by(|a,b|a.state.as_text().cmp(b.state.as_text())),
|
||||
SortedOrder::Desc => self.containers.items.sort_by(|a,b|b.state.as_text().cmp(a.state.as_text())),
|
||||
}
|
||||
|
||||
},
|
||||
Header::Status => {
|
||||
match so {
|
||||
SortedOrder::Asc => self.containers.items.sort_by(|a,b|a.status.cmp(&b.status)),
|
||||
SortedOrder::Desc => self.containers.items.sort_by(|a,b|b.status.cmp(&a.status)),
|
||||
}
|
||||
},
|
||||
|
||||
Header::Status => {
|
||||
match so {
|
||||
SortedOrder::Asc => self.containers.items.sort_by(|a,b|a.status.cmp(&b.status)),
|
||||
SortedOrder::Desc => self.containers.items.sort_by(|a,b|b.status.cmp(&a.status)),
|
||||
}
|
||||
},
|
||||
|
||||
Header::Cpu => {
|
||||
match so {
|
||||
SortedOrder::Desc =>
|
||||
self.containers.items.sort_by(|a,b|a.cpu_stats.back().cmp(&b.cpu_stats.back())),
|
||||
SortedOrder::Asc => self.containers.items.sort_by(|a,b|b.cpu_stats.back().cmp(&a.cpu_stats.back()))
|
||||
}
|
||||
},
|
||||
|
||||
Header::Memory => {
|
||||
match so {
|
||||
SortedOrder::Desc =>
|
||||
self.containers.items.sort_by(|a,b|a.mem_stats.back().cmp(&b.mem_stats.back())),
|
||||
SortedOrder::Asc => self.containers.items.sort_by(|a,b|b.mem_stats.back().cmp(&a.mem_stats.back()))
|
||||
}
|
||||
},
|
||||
Header::Image => {
|
||||
match so {
|
||||
SortedOrder::Asc => self.containers.items.sort_by(|a,b|a.image.cmp(&b.image)),
|
||||
SortedOrder::Desc => self.containers.items.sort_by(|a,b|b.image.cmp(&a.image)),
|
||||
}
|
||||
},
|
||||
Header::Name => {
|
||||
match so {
|
||||
SortedOrder::Asc => self.containers.items.sort_by(|a,b|a.name.cmp(&b.name)),
|
||||
SortedOrder::Desc => self.containers.items.sort_by(|a,b|b.name.cmp(&a.name)),
|
||||
}
|
||||
},
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// match so {
|
||||
// SortedOrder::Asc => self.containers.items.sort_by(|a,b|b.name.cmp(&a.name)),
|
||||
// SortedOrder::Desc => self.containers.items.sort_by(|a,b|a.name.cmp(&b.name))
|
||||
// }
|
||||
// }
|
||||
|
||||
pub fn sort_by_id(&mut self, so: SortedOrder) {
|
||||
match so {
|
||||
SortedOrder::Asc => self.containers.items.sort_by(|a,b|b.id.cmp(&a.id)),
|
||||
SortedOrder::Desc => self.containers.items.sort_by(|a,b|a.id.cmp(&b.id))
|
||||
}
|
||||
}
|
||||
/// Sort the containers vec, based on a heading, either ascending or descending
|
||||
pub fn sort_containers(&mut self) {
|
||||
if let Some((head, so)) = self.sorted_by.as_ref() {
|
||||
match head {
|
||||
Header::State => match so {
|
||||
SortedOrder::Asc => self
|
||||
.containers
|
||||
.items
|
||||
.sort_by(|a, b| a.state.as_text().cmp(b.state.as_text())),
|
||||
SortedOrder::Desc => self
|
||||
.containers
|
||||
.items
|
||||
.sort_by(|a, b| b.state.as_text().cmp(a.state.as_text())),
|
||||
},
|
||||
Header::Status => match so {
|
||||
SortedOrder::Asc => self
|
||||
.containers
|
||||
.items
|
||||
.sort_by(|a, b| a.status.cmp(&b.status)),
|
||||
SortedOrder::Desc => self
|
||||
.containers
|
||||
.items
|
||||
.sort_by(|a, b| b.status.cmp(&a.status)),
|
||||
},
|
||||
Header::Status => match so {
|
||||
SortedOrder::Asc => self
|
||||
.containers
|
||||
.items
|
||||
.sort_by(|a, b| a.status.cmp(&b.status)),
|
||||
SortedOrder::Desc => self
|
||||
.containers
|
||||
.items
|
||||
.sort_by(|a, b| b.status.cmp(&a.status)),
|
||||
},
|
||||
Header::Cpu => match so {
|
||||
SortedOrder::Desc => self
|
||||
.containers
|
||||
.items
|
||||
.sort_by(|a, b| a.cpu_stats.back().cmp(&b.cpu_stats.back())),
|
||||
SortedOrder::Asc => self
|
||||
.containers
|
||||
.items
|
||||
.sort_by(|a, b| b.cpu_stats.back().cmp(&a.cpu_stats.back())),
|
||||
},
|
||||
Header::Memory => match so {
|
||||
SortedOrder::Desc => self
|
||||
.containers
|
||||
.items
|
||||
.sort_by(|a, b| a.mem_stats.back().cmp(&b.mem_stats.back())),
|
||||
SortedOrder::Asc => self
|
||||
.containers
|
||||
.items
|
||||
.sort_by(|a, b| b.mem_stats.back().cmp(&a.mem_stats.back())),
|
||||
},
|
||||
Header::Id => match so {
|
||||
SortedOrder::Asc => self.containers.items.sort_by(|a, b| a.id.cmp(&b.id)),
|
||||
SortedOrder::Desc => self.containers.items.sort_by(|a, b| b.id.cmp(&a.id)),
|
||||
},
|
||||
Header::Image => match so {
|
||||
SortedOrder::Asc => self.containers.items.sort_by(|a, b| a.image.cmp(&b.image)),
|
||||
SortedOrder::Desc => {
|
||||
self.containers.items.sort_by(|a, b| b.image.cmp(&a.image))
|
||||
}
|
||||
},
|
||||
Header::Name => match so {
|
||||
SortedOrder::Asc => self.containers.items.sort_by(|a, b| a.name.cmp(&b.name)),
|
||||
SortedOrder::Desc => self.containers.items.sort_by(|a, b| b.name.cmp(&a.name)),
|
||||
},
|
||||
Header::Rx => match so {
|
||||
SortedOrder::Asc => self
|
||||
.containers
|
||||
.items
|
||||
.sort_by(|a, b| a.net_rx.cmp(&b.net_rx)),
|
||||
SortedOrder::Desc => self
|
||||
.containers
|
||||
.items
|
||||
.sort_by(|a, b| b.net_rx.cmp(&a.net_rx)),
|
||||
},
|
||||
Header::Tx => match so {
|
||||
SortedOrder::Asc => self
|
||||
.containers
|
||||
.items
|
||||
.sort_by(|a, b| a.net_tx.cmp(&b.net_tx)),
|
||||
SortedOrder::Desc => self
|
||||
.containers
|
||||
.items
|
||||
.sort_by(|a, b| b.net_tx.cmp(&a.net_tx)),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Find the index of the currently selected single log line
|
||||
pub fn get_selected_log_index(&self) -> Option<usize> {
|
||||
|
||||
Reference in New Issue
Block a user