refactor: input sorted
This commit is contained in:
@@ -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",
|
||||
|
||||
+9
-12
@@ -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,
|
||||
@@ -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())),
|
||||
|
||||
@@ -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)]) {
|
||||
|
||||
@@ -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(
|
||||
|
||||
+8
-42
@@ -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)),
|
||||
),
|
||||
]);
|
||||
@@ -407,7 +373,7 @@ pub fn draw_heading_bar<B: Backend>(
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user