refactor: input sorted
This commit is contained in:
+8
-11
@@ -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<Header, Rect>
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum SortedOrder {
|
||||
Asc,
|
||||
Desc,
|
||||
@@ -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())),
|
||||
|
||||
@@ -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))),
|
||||
if let Some((h, order)) = locked_data.get_sorted().as_ref() {
|
||||
if &SortedOrder::Desc == order && h == &header {
|
||||
output = Some((header, SortedOrder::Asc))
|
||||
}
|
||||
} else {
|
||||
locked_data.set_sorted(Some((header, SortedOrder::Desc)))
|
||||
}
|
||||
locked_data.set_sorted(output)
|
||||
}
|
||||
|
||||
/// Handle any keyboard button events
|
||||
|
||||
+7
-41
@@ -142,20 +142,11 @@ pub fn draw_containers<B: Backend>(
|
||||
|
||||
let lines = Spans::from(vec![
|
||||
Span::styled(
|
||||
format!(
|
||||
"{:<width$}",
|
||||
i.state.to_string(),
|
||||
width = widths.state.1
|
||||
),
|
||||
format!("{:<width$}", i.state.to_string(), width = widths.state.1),
|
||||
state_style,
|
||||
),
|
||||
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<B: Backend>(
|
||||
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<B: Backend>(
|
||||
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)),
|
||||
),
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user