Skip to content

Commit

Permalink
🐛 fix sort order for image size (#42)
Browse files Browse the repository at this point in the history
Also some refactoring of the structures passed between the runtime and the components
  • Loading branch information
pyaillet authored Dec 26, 2023
1 parent 53cd801 commit 3e38720
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 96 deletions.
30 changes: 15 additions & 15 deletions src/components/containers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use ratatui::{
};
use tokio::sync::mpsc::UnboundedSender;

use crate::utils::table;
use crate::{action::Action, utils::centered_rect};
use crate::{
components::Component,
runtime::{delete_container, get_container, list_containers},
};
use crate::{runtime::ContainerSummary, utils::table};

use super::ComponentInit;

Expand Down Expand Up @@ -69,7 +69,7 @@ pub enum SortColumn {
pub struct Containers {
all: bool,
state: TableState,
containers: Vec<[String; 4]>,
containers: Vec<ContainerSummary>,
show_popup: Popup,
action_tx: Option<UnboundedSender<Action>>,
sort_by: SortColumn,
Expand Down Expand Up @@ -122,11 +122,8 @@ impl Containers {
fn get_selected_container_info(&self) -> Option<(String, String)> {
self.state
.selected()
.and_then(|i| self.containers.get(i))
.and_then(|c| {
c.first()
.and_then(|cid| c.get(1).map(|cname| (cid.to_owned(), cname.to_owned())))
})
.and_then(|i| self.containers.get(i).cloned())
.map(|c| (c.id, c.name))
}

fn draw_popup(&self, f: &mut Frame<'_>) {
Expand Down Expand Up @@ -238,15 +235,15 @@ impl Containers {

fn sort(&mut self) {
self.containers.sort_by(|a, b| {
let (idx, o) = match &self.sort_by {
SortColumn::Id(o) => (0, o),
SortColumn::Name(o) => (1, o),
SortColumn::Image(o) => (2, o),
SortColumn::Status(o) => (3, o),
let (cmp_result, o) = match &self.sort_by {
SortColumn::Id(o) => (a.id.cmp(&b.id), o),
SortColumn::Name(o) => (a.name.cmp(&b.name), o),
SortColumn::Image(o) => (a.image.cmp(&b.image), o),
SortColumn::Status(o) => (a.status.cmp(&b.status), o),
};
match o {
SortOrder::Asc => a[idx].cmp(&b[idx]),
SortOrder::Desc => b[idx].cmp(&a[idx]),
SortOrder::Asc => cmp_result,
SortOrder::Desc => cmp_result.reverse(),
}
});
}
Expand Down Expand Up @@ -385,7 +382,10 @@ impl Component for Containers {
if self.all { "All" } else { "Running" }
),
["Id", "Name", "Image", "Status"],
self.containers.clone(),
self.containers
.iter()
.map(|c| (*c).clone().into())
.collect(),
&CONTAINER_CONSTRAINTS,
);
f.render_stateful_widget(t, rects[0], &mut self.state);
Expand Down
27 changes: 12 additions & 15 deletions src/components/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tokio::sync::mpsc::UnboundedSender;

use crate::action::Action;
use crate::components::Component;
use crate::runtime::{delete_image, get_image, list_images};
use crate::runtime::{delete_image, get_image, list_images, ImageSummary};
use crate::utils::{centered_rect, table};

const IMAGE_CONSTRAINTS: [Constraint; 4] = [
Expand All @@ -28,7 +28,7 @@ enum Popup {

pub struct Images {
state: TableState,
images: Vec<[String; 4]>,
images: Vec<ImageSummary>,
show_popup: Popup,
action_tx: Option<UnboundedSender<Action>>,
sort_by: SortColumn,
Expand Down Expand Up @@ -94,11 +94,8 @@ impl Images {
fn get_selected_image_info(&self) -> Option<(String, String)> {
self.state
.selected()
.and_then(|i| self.images.get(i))
.and_then(|c| {
c.first()
.and_then(|id| c.get(1).map(|tag| (id.to_owned(), tag.to_owned())))
})
.and_then(|i| self.images.get(i).cloned())
.map(|c| (c.id, c.name))
}

fn draw_popup(&self, f: &mut Frame<'_>) {
Expand Down Expand Up @@ -131,15 +128,15 @@ impl Images {

fn sort(&mut self) {
self.images.sort_by(|a, b| {
let (idx, o) = match &self.sort_by {
SortColumn::Id(o) => (0, o),
SortColumn::Name(o) => (1, o),
SortColumn::Size(o) => (2, o),
SortColumn::Age(o) => (3, o),
let (cmp_result, o) = match &self.sort_by {
SortColumn::Id(o) => (a.id.cmp(&b.id), o),
SortColumn::Name(o) => (a.name.cmp(&b.name), o),
SortColumn::Size(o) => (a.size.cmp(&b.size), o),
SortColumn::Age(o) => (a.created.cmp(&b.created), o),
};
match o {
SortOrder::Asc => a[idx].cmp(&b[idx]),
SortOrder::Desc => b[idx].cmp(&a[idx]),
SortOrder::Asc => cmp_result,
SortOrder::Desc => cmp_result.reverse(),
}
});
}
Expand Down Expand Up @@ -229,7 +226,7 @@ impl Component for Images {
let t = table(
self.get_name().to_string(),
["Id", "Name", "Size", "Age"],
self.images.clone(),
self.images.iter().map(|i| (*i).clone().into()).collect(),
&IMAGE_CONSTRAINTS,
);
f.render_stateful_widget(t, rects[0], &mut self.state);
Expand Down
27 changes: 12 additions & 15 deletions src/components/networks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tokio::sync::mpsc::UnboundedSender;

use crate::action::Action;
use crate::components::Component;
use crate::runtime::{delete_network, get_network, list_networks};
use crate::runtime::{delete_network, get_network, list_networks, NetworkSummary};
use crate::utils::{centered_rect, table};

const NETWORK_CONSTRAINTS: [Constraint; 4] = [
Expand Down Expand Up @@ -41,7 +41,7 @@ pub enum SortColumn {

pub struct Networks {
state: TableState,
networks: Vec<[String; 4]>,
networks: Vec<NetworkSummary>,
show_popup: Popup,
action_tx: Option<UnboundedSender<Action>>,
sort_by: SortColumn,
Expand Down Expand Up @@ -93,11 +93,8 @@ impl Networks {
fn get_selected_network_info(&self) -> Option<(String, String)> {
self.state
.selected()
.and_then(|i| self.networks.get(i))
.and_then(|n| match (n.first(), n.get(1)) {
(Some(id), Some(name)) => Some((id.to_string(), name.to_string())),
_ => None,
})
.and_then(|i| self.networks.get(i).cloned())
.map(|n| (n.id, n.name))
}

fn draw_popup(&self, f: &mut Frame<'_>) {
Expand Down Expand Up @@ -130,15 +127,15 @@ impl Networks {

fn sort(&mut self) {
self.networks.sort_by(|a, b| {
let (idx, o) = match &self.sort_by {
SortColumn::Id(o) => (0, o),
SortColumn::Name(o) => (1, o),
SortColumn::Driver(o) => (2, o),
SortColumn::Age(o) => (3, o),
let (cmp_result, o) = match &self.sort_by {
SortColumn::Id(o) => (a.id.cmp(&b.id), o),
SortColumn::Name(o) => (a.name.cmp(&b.name), o),
SortColumn::Driver(o) => (a.driver.cmp(&b.driver), o),
SortColumn::Age(o) => (a.created.cmp(&b.created), o),
};
match o {
SortOrder::Asc => a[idx].cmp(&b[idx]),
SortOrder::Desc => b[idx].cmp(&a[idx]),
SortOrder::Asc => cmp_result,
SortOrder::Desc => cmp_result.reverse(),
}
});
}
Expand Down Expand Up @@ -234,7 +231,7 @@ impl Component for Networks {
let t = table(
self.get_name().to_string(),
["Id", "Name", "Driver", "Age"],
self.networks.clone(),
self.networks.iter().map(|n| (*n).clone().into()).collect(),
&NETWORK_CONSTRAINTS,
);
f.render_stateful_widget(t, rects[0], &mut self.state);
Expand Down
23 changes: 11 additions & 12 deletions src/components/volumes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tokio::sync::mpsc::UnboundedSender;

use crate::action::Action;
use crate::components::Component;
use crate::runtime::{delete_volume, get_volume, list_volumes};
use crate::runtime::{delete_volume, get_volume, list_volumes, VolumeSummary};
use crate::utils::{centered_rect, table};

const VOLUME_CONSTRAINTS: [Constraint; 4] = [
Expand Down Expand Up @@ -41,7 +41,7 @@ pub enum SortColumn {

pub struct Volumes {
state: TableState,
volumes: Vec<[String; 4]>,
volumes: Vec<VolumeSummary>,
show_popup: Popup,
action_tx: Option<UnboundedSender<Action>>,
sort_by: SortColumn,
Expand Down Expand Up @@ -94,8 +94,7 @@ impl Volumes {
self.state
.selected()
.and_then(|i| self.volumes.get(i))
.and_then(|v| v.first())
.cloned()
.map(|v| v.id.to_string())
}

fn draw_popup(&self, f: &mut Frame<'_>) {
Expand Down Expand Up @@ -128,15 +127,15 @@ impl Volumes {

fn sort(&mut self) {
self.volumes.sort_by(|a, b| {
let (idx, o) = match &self.sort_by {
SortColumn::Id(o) => (0, o),
SortColumn::Driver(o) => (1, o),
SortColumn::Size(o) => (2, o),
SortColumn::Age(o) => (3, o),
let (cmp_result, o) = match &self.sort_by {
SortColumn::Id(o) => (a.id.cmp(&b.id), o),
SortColumn::Driver(o) => (a.driver.cmp(&b.driver), o),
SortColumn::Size(o) => (a.size.cmp(&b.size), o),
SortColumn::Age(o) => (a.created.cmp(&b.created), o),
};
match o {
SortOrder::Asc => a[idx].cmp(&b[idx]),
SortOrder::Desc => b[idx].cmp(&a[idx]),
SortOrder::Asc => cmp_result,
SortOrder::Desc => cmp_result.reverse(),
}
});
}
Expand Down Expand Up @@ -228,7 +227,7 @@ impl Component for Volumes {
let t = table(
self.get_name().to_string(),
["Id", "Driver", "Size", "Age"],
self.volumes.clone(),
self.volumes.iter().map(|v| (*v).clone().into()).collect(),
&VOLUME_CONSTRAINTS,
);
f.render_stateful_widget(t, rects[0], &mut self.state);
Expand Down
Loading

0 comments on commit 3e38720

Please sign in to comment.