wip: sort_by

This commit is contained in:
Jack Wills
2022-07-22 03:29:23 +00:00
parent cf7e02dde9
commit dc4a62910c
5 changed files with 161 additions and 3 deletions
+23
View File
@@ -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,6 +114,17 @@ 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 {
+103
View File
@@ -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;
+30 -2
View File
@@ -1,6 +1,6 @@
use bollard::{
container::{ListContainersOptions, LogsOptions, StartContainerOptions, Stats, StatsOptions},
Docker,
Docker, models::ContainerSummary,
};
use futures_util::{future::join_all, StreamExt};
use parking_lot::Mutex;
@@ -8,7 +8,7 @@ use std::sync::Arc;
use tokio::{sync::mpsc::Receiver, task::JoinHandle};
use crate::{
app_data::{AppData, DockerControls},
app_data::{AppData, DockerControls, SortedOrder, Header},
app_error::AppError,
parse_args::CliArgs,
ui::GuiState,
@@ -123,6 +123,26 @@ impl DockerData {
}
}
// pub fn sort_containers(i: &mut [ContainerSummary], so: SortedOrder, header: Header) -> &[ContainerSummary] {
// match header {
// Header::State => {
// match so {
// SortedOrder::Asc => i.sort_by(|a,b|b.state.cmp(&a.state)),
// SortedOrder::Desc => i.sort_by(|a,b|a.state.cmp(&b.state)),
// }
// },
// Header::Image => {
// match so {
// SortedOrder::Asc => i.sort_by(|a,b|b.image.cmp(&a.image)),
// SortedOrder::Desc => i.sort_by(|a,b|a.image.cmp(&b.image)),
// }
// },
// _ => ()
// }
// i
// }
/// Get all current containers, handle into ContainerItem in the app_data struct rather than here
/// Just make sure that items sent are guaranteed to have an id
/// return Vec<(is_running, id)>
@@ -143,7 +163,14 @@ impl DockerData {
.filter(|i| i.id.is_some())
.for_each(|c| output.push(c.to_owned()));
// containers.so
// let a = Self::sort_containers(&mut output, SortedOrder::Asc, Header::State);
self.app_data.lock().update_containers(&output);
// self.app_data.lock().sort_containers(SortedOrder::Asc, Header::State);
output
.iter()
.filter_map(|i| {
@@ -214,6 +241,7 @@ impl DockerData {
};
self.update_all_container_stats(&all_ids).await;
}
/// Animate the loading icon
+1
View File
@@ -1,3 +1,4 @@
#![allow(unused)]
use app_data::AppData;
use app_error::AppError;
use bollard::Docker;
+3
View File
@@ -14,6 +14,7 @@ use tui::{
Frame,
};
use crate::app_data::{SortedOrder, Header};
use crate::{
app_data::{AppData, ByteStats, Columns, CpuStats, State, Stats},
app_error::AppError,
@@ -123,6 +124,8 @@ pub fn draw_containers<B: Backend>(
widths: &Columns,
) {
let block = generate_block(app_data, area, gui_state, SelectablePanel::Containers);
app_data.lock().sort_containers(SortedOrder::Asc);
let items = app_data
.lock()