wip: sort_by
This commit is contained in:
@@ -81,6 +81,7 @@ impl<T> StatefulList<T> {
|
||||
}
|
||||
|
||||
/// States of the container
|
||||
// / impl ord
|
||||
#[derive(Clone, Debug, PartialEq, PartialOrd)]
|
||||
pub enum State {
|
||||
Dead,
|
||||
@@ -92,6 +93,17 @@ pub enum State {
|
||||
Unknown,
|
||||
}
|
||||
|
||||
// impl Ord for State {
|
||||
// fn cmp(&self, other: &Self) -> Ordering {
|
||||
// match (self, other) {
|
||||
// (Self::Dead)
|
||||
// // (_, Foo::B) => Ordering::Less,
|
||||
// // (Foo::A { val: l }, Foo::A { val: r }) => l.cmp(&r),
|
||||
// // (Foo::B, _) => Ordering::Greater,
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
impl State {
|
||||
pub fn get_color(&self) -> Color {
|
||||
match self {
|
||||
@@ -102,7 +114,18 @@ impl State {
|
||||
_ => Color::Red,
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn as_text(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Dead => "dead",
|
||||
Self::Exited => "exited",
|
||||
Self::Paused => "paused",
|
||||
Self::Removing => "removing",
|
||||
Self::Restarting => "restarting",
|
||||
Self::Running => "running",
|
||||
Self::Unknown => "unknown",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for State {
|
||||
fn from(input: &str) -> Self {
|
||||
|
||||
@@ -16,8 +16,30 @@ pub struct AppData {
|
||||
pub containers: StatefulList<ContainerItem>,
|
||||
pub init: bool,
|
||||
pub show_error: bool,
|
||||
// todo
|
||||
sort_by: Header
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum SortedOrder{
|
||||
Asc,
|
||||
Desc
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Header{
|
||||
State,
|
||||
Status,
|
||||
Cpu,
|
||||
Memory,
|
||||
Id,
|
||||
Name,
|
||||
Image,
|
||||
Rx,
|
||||
Tx
|
||||
}
|
||||
|
||||
|
||||
impl AppData {
|
||||
/// Generate a default app_state
|
||||
pub fn default(args: CliArgs) -> Self {
|
||||
@@ -28,6 +50,7 @@ impl AppData {
|
||||
init: false,
|
||||
logs_parsed: false,
|
||||
show_error: false,
|
||||
sort_by: Header::Memory,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,6 +141,86 @@ 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))
|
||||
}
|
||||
}
|
||||
|
||||
/// Find the index of the currently selected single log line
|
||||
pub fn get_selected_log_index(&self) -> Option<usize> {
|
||||
let mut output = None;
|
||||
|
||||
Reference in New Issue
Block a user