refactor: input sorted

This commit is contained in:
Jack Wills
2022-07-23 01:15:58 +00:00
parent 3b69cc29fa
commit f5504c47c5
5 changed files with 29 additions and 66 deletions
+8 -11
View File
@@ -1,8 +1,6 @@
use bollard::models::ContainerSummary; use bollard::models::ContainerSummary;
use core::fmt; use core::fmt;
use std::{ use std::time::{SystemTime, UNIX_EPOCH};
time::{SystemTime, UNIX_EPOCH},
};
use tui::widgets::ListItem; use tui::widgets::ListItem;
mod container_state; mod container_state;
@@ -20,10 +18,9 @@ pub struct AppData {
pub init: bool, pub init: bool,
pub show_error: bool, pub show_error: bool,
sorted_by: Option<(Header, SortedOrder)>, sorted_by: Option<(Header, SortedOrder)>,
// heading_map: HashMap<Header, Rect>
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq)]
pub enum SortedOrder { pub enum SortedOrder {
Asc, Asc,
Desc, Desc,
@@ -182,11 +179,11 @@ impl AppData {
if let Some((head, so)) = self.sorted_by.as_ref() { if let Some((head, so)) = self.sorted_by.as_ref() {
match head { match head {
Header::State => match so { Header::State => match so {
SortedOrder::Asc => self SortedOrder::Desc => self
.containers .containers
.items .items
.sort_by(|a, b| a.state.order().cmp(b.state.order())), .sort_by(|a, b| a.state.order().cmp(b.state.order())),
SortedOrder::Desc => self SortedOrder::Asc => self
.containers .containers
.items .items
.sort_by(|a, b| b.state.order().cmp(a.state.order())), .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)), .sort_by(|a, b| b.status.cmp(&a.status)),
}, },
Header::Cpu => match so { Header::Cpu => match so {
SortedOrder::Desc => self SortedOrder::Asc => self
.containers .containers
.items .items
.sort_by(|a, b| a.cpu_stats.back().cmp(&b.cpu_stats.back())), .sort_by(|a, b| a.cpu_stats.back().cmp(&b.cpu_stats.back())),
SortedOrder::Asc => self SortedOrder::Desc => self
.containers .containers
.items .items
.sort_by(|a, b| b.cpu_stats.back().cmp(&a.cpu_stats.back())), .sort_by(|a, b| b.cpu_stats.back().cmp(&a.cpu_stats.back())),
}, },
Header::Memory => match so { Header::Memory => match so {
SortedOrder::Desc => self SortedOrder::Asc => self
.containers .containers
.items .items
.sort_by(|a, b| a.mem_stats.back().cmp(&b.mem_stats.back())), .sort_by(|a, b| a.mem_stats.back().cmp(&b.mem_stats.back())),
SortedOrder::Asc => self SortedOrder::Desc => self
.containers .containers
.items .items
.sort_by(|a, b| b.mem_stats.back().cmp(&a.mem_stats.back())), .sort_by(|a, b| b.mem_stats.back().cmp(&a.mem_stats.back())),
+6 -6
View File
@@ -115,16 +115,16 @@ impl InputHandler {
self.mouse_capture = !self.mouse_capture; 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) { fn sort(&self, header: Header) {
let mut output = Some((header.to_owned(), SortedOrder::Desc));
let mut locked_data = self.app_data.lock(); let mut locked_data = self.app_data.lock();
if let Some((_, order)) = locked_data.get_sorted().as_ref() { if let Some((h, order)) = locked_data.get_sorted().as_ref() {
match order { if &SortedOrder::Desc == order && h == &header {
SortedOrder::Asc => locked_data.set_sorted(Some((header, SortedOrder::Desc))), output = Some((header, SortedOrder::Asc))
_ => locked_data.set_sorted(Some((header, SortedOrder::Asc))),
} }
} else {
locked_data.set_sorted(Some((header, SortedOrder::Desc)))
} }
locked_data.set_sorted(output)
} }
/// Handle any keyboard button events /// Handle any keyboard button events
+7 -41
View File
@@ -142,20 +142,11 @@ pub fn draw_containers<B: Backend>(
let lines = Spans::from(vec![ let lines = Spans::from(vec![
Span::styled( Span::styled(
format!( format!("{:<width$}", i.state.to_string(), width = widths.state.1),
"{:<width$}",
i.state.to_string(),
width = widths.state.1
),
state_style, state_style,
), ),
Span::styled( Span::styled(
format!( format!("{}{:>width$}", MARGIN, i.status, width = &widths.status.1),
"{}{:>width$}",
MARGIN,
i.status,
width =&widths.status.1
),
state_style, state_style,
), ),
Span::styled( Span::styled(
@@ -168,12 +159,7 @@ pub fn draw_containers<B: Backend>(
state_style, state_style,
), ),
Span::styled( Span::styled(
format!( format!("{}{:>width$}", MARGIN, mems, width = &widths.mem.1),
"{}{:>width$}",
MARGIN,
mems,
width = &widths.mem.1
),
state_style, state_style,
), ),
Span::styled( Span::styled(
@@ -186,39 +172,19 @@ pub fn draw_containers<B: Backend>(
blue, blue,
), ),
Span::styled( Span::styled(
format!( format!("{}{:>width$}", MARGIN, i.name, width = widths.name.1),
"{}{:>width$}",
MARGIN,
i.name,
width = widths.name.1
),
blue, blue,
), ),
Span::styled( Span::styled(
format!( format!("{}{:>width$}", MARGIN, i.image, width = widths.image.1),
"{}{:>width$}",
MARGIN,
i.image,
width = widths.image.1
),
blue, blue,
), ),
Span::styled( Span::styled(
format!( format!("{}{:>width$}", MARGIN, i.net_rx, width = widths.net_rx.1),
"{}{:>width$}",
MARGIN,
i.net_rx,
width = widths.net_rx.1
),
Style::default().fg(Color::Rgb(255, 233, 193)), Style::default().fg(Color::Rgb(255, 233, 193)),
), ),
Span::styled( Span::styled(
format!( format!("{}{:>width$}", MARGIN, i.net_tx, width = widths.net_tx.1),
"{}{:>width$}",
MARGIN,
i.net_tx,
width = widths.net_tx.1
),
Style::default().fg(Color::Rgb(205, 140, 140)), Style::default().fg(Color::Rgb(205, 140, 140)),
), ),
]); ]);