From f5504c47c54c9bf017ce8a2c9f0321ae3d9214ac Mon Sep 17 00:00:00 2001 From: Jack Wills <32690432+mrjackwills@users.noreply.github.com> Date: Sat, 23 Jul 2022 01:15:58 +0000 Subject: [PATCH] refactor: input sorted --- src/app_data/container_state.rs | 2 +- src/app_data/mod.rs | 21 ++++++-------- src/docker_data/mod.rs | 6 ++-- src/input_handler/mod.rs | 16 +++++------ src/ui/draw_blocks.rs | 50 ++++++--------------------------- 5 files changed, 29 insertions(+), 66 deletions(-) diff --git a/src/app_data/container_state.rs b/src/app_data/container_state.rs index d370618..a7806f3 100644 --- a/src/app_data/container_state.rs +++ b/src/app_data/container_state.rs @@ -117,7 +117,7 @@ impl State { _ => Color::Red, } } - // Dirty way to create order for the state, rather than impl Ord + // Dirty way to create order for the state, rather than impl Ord pub fn order(&self) -> &'static str { match self { Self::Running => "a", diff --git a/src/app_data/mod.rs b/src/app_data/mod.rs index a790f43..9c4d71f 100644 --- a/src/app_data/mod.rs +++ b/src/app_data/mod.rs @@ -1,8 +1,6 @@ use bollard::models::ContainerSummary; use core::fmt; -use std::{ - time::{SystemTime, UNIX_EPOCH}, -}; +use std::time::{SystemTime, UNIX_EPOCH}; use tui::widgets::ListItem; mod container_state; @@ -20,10 +18,9 @@ pub struct AppData { pub init: bool, pub show_error: bool, sorted_by: Option<(Header, SortedOrder)>, - // heading_map: HashMap } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub enum SortedOrder { Asc, Desc, @@ -86,7 +83,7 @@ impl AppData { init: false, logs_parsed: false, show_error: false, - sorted_by: None, + sorted_by: None, } } @@ -182,11 +179,11 @@ impl AppData { if let Some((head, so)) = self.sorted_by.as_ref() { match head { Header::State => match so { - SortedOrder::Asc => self + SortedOrder::Desc => self .containers .items .sort_by(|a, b| a.state.order().cmp(b.state.order())), - SortedOrder::Desc => self + SortedOrder::Asc => self .containers .items .sort_by(|a, b| b.state.order().cmp(a.state.order())), @@ -202,21 +199,21 @@ impl AppData { .sort_by(|a, b| b.status.cmp(&a.status)), }, Header::Cpu => match so { - SortedOrder::Desc => self + SortedOrder::Asc => self .containers .items .sort_by(|a, b| a.cpu_stats.back().cmp(&b.cpu_stats.back())), - SortedOrder::Asc => self + SortedOrder::Desc => self .containers .items .sort_by(|a, b| b.cpu_stats.back().cmp(&a.cpu_stats.back())), }, Header::Memory => match so { - SortedOrder::Desc => self + SortedOrder::Asc => self .containers .items .sort_by(|a, b| a.mem_stats.back().cmp(&b.mem_stats.back())), - SortedOrder::Asc => self + SortedOrder::Desc => self .containers .items .sort_by(|a, b| b.mem_stats.back().cmp(&a.mem_stats.back())), diff --git a/src/docker_data/mod.rs b/src/docker_data/mod.rs index 184ddec..498aac8 100644 --- a/src/docker_data/mod.rs +++ b/src/docker_data/mod.rs @@ -191,10 +191,10 @@ impl DockerData { output } - // async fn stop(&self) { - // self.docker. + // async fn stop(&self) { + // self.docker. - // } + // } /// Update all logs, spawn each container into own tokio::spawn thread async fn init_all_logs(&mut self, all_ids: &[(bool, String)]) { diff --git a/src/input_handler/mod.rs b/src/input_handler/mod.rs index 7eb229a..3a64389 100644 --- a/src/input_handler/mod.rs +++ b/src/input_handler/mod.rs @@ -115,16 +115,16 @@ impl InputHandler { self.mouse_capture = !self.mouse_capture; } + /// Sort containers based on a given header, switch asc to desc if already sorted, else always desc fn sort(&self, header: Header) { + let mut output = Some((header.to_owned(), SortedOrder::Desc)); let mut locked_data = self.app_data.lock(); - if let Some((_, order)) = locked_data.get_sorted().as_ref() { - match order { - SortedOrder::Asc => locked_data.set_sorted(Some((header, SortedOrder::Desc))), - _ => locked_data.set_sorted(Some((header, SortedOrder::Asc))), - } - } else { - locked_data.set_sorted(Some((header, SortedOrder::Desc))) + if let Some((h, order)) = locked_data.get_sorted().as_ref() { + if &SortedOrder::Desc == order && h == &header { + output = Some((header, SortedOrder::Asc)) + } } + locked_data.set_sorted(output) } /// Handle any keyboard button events @@ -260,7 +260,7 @@ impl InputHandler { )); if let Some(header) = header_intersects { - self.sort(header); + self.sort(header); } self.gui_state.lock().panel_intersect(Rect::new( diff --git a/src/ui/draw_blocks.rs b/src/ui/draw_blocks.rs index 56db981..9230372 100644 --- a/src/ui/draw_blocks.rs +++ b/src/ui/draw_blocks.rs @@ -142,20 +142,11 @@ pub fn draw_containers( let lines = Spans::from(vec![ Span::styled( - format!( - "{:width$}", - MARGIN, - i.status, - width =&widths.status.1 - ), + format!("{}{:>width$}", MARGIN, i.status, width = &widths.status.1), state_style, ), Span::styled( @@ -168,12 +159,7 @@ pub fn draw_containers( state_style, ), Span::styled( - format!( - "{}{:>width$}", - MARGIN, - mems, - width = &widths.mem.1 - ), + format!("{}{:>width$}", MARGIN, mems, width = &widths.mem.1), state_style, ), Span::styled( @@ -186,39 +172,19 @@ pub fn draw_containers( blue, ), Span::styled( - format!( - "{}{:>width$}", - MARGIN, - i.name, - width = widths.name.1 - ), + format!("{}{:>width$}", MARGIN, i.name, width = widths.name.1), blue, ), Span::styled( - format!( - "{}{:>width$}", - MARGIN, - i.image, - width = widths.image.1 - ), + format!("{}{:>width$}", MARGIN, i.image, width = widths.image.1), blue, ), Span::styled( - format!( - "{}{:>width$}", - MARGIN, - i.net_rx, - width = widths.net_rx.1 - ), + format!("{}{:>width$}", MARGIN, i.net_rx, width = widths.net_rx.1), Style::default().fg(Color::Rgb(255, 233, 193)), ), Span::styled( - format!( - "{}{:>width$}", - MARGIN, - i.net_tx, - width = widths.net_tx.1 - ), + format!("{}{:>width$}", MARGIN, i.net_tx, width = widths.net_tx.1), Style::default().fg(Color::Rgb(205, 140, 140)), ), ]); @@ -407,7 +373,7 @@ pub fn draw_heading_bar( if let Some((a, b)) = sorted_by.as_ref() { if x == a { match b { - SortedOrder::Asc => suffix = " ⌃", + SortedOrder::Asc => suffix = " ⌃", SortedOrder::Desc => suffix = " ⌄", } suffix_margin = 2;