From a722731c6a77e00d1fb13967b51400aa34e72213 Mon Sep 17 00:00:00 2001 From: Jack Wills <32690432+mrjackwills@users.noreply.github.com> Date: Fri, 5 Jan 2024 14:14:55 +0000 Subject: [PATCH] refactor: string_wrapper .get() return &str --- src/app_data/container_state.rs | 91 +++++++++++++++++---------------- src/app_data/mod.rs | 24 ++++----- src/ui/draw_blocks.rs | 2 +- 3 files changed, 59 insertions(+), 58 deletions(-) diff --git a/src/app_data/container_state.rs b/src/app_data/container_state.rs index a081afa..247d6ff 100644 --- a/src/app_data/container_state.rs +++ b/src/app_data/container_state.rs @@ -47,6 +47,52 @@ impl PartialOrd for ContainerId { } } +/// TODO - use string_wrapper for ContainerId? +/// ContainerName and ContainerImage are simple structs, used so can implement custom fmt functions to them +macro_rules! string_wrapper { + ($name:ident) => { + #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] + pub struct $name(String); + + impl From for $name { + fn from(value: String) -> Self { + Self(value) + } + } + + impl$name { + pub fn get(&self) -> &str { + self.0.as_str() + } + + pub fn set(&mut self, value: String) { + self.0 = value; + } + } + + impl std::fmt::Display for $name { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if self.0.chars().count() >= 30 { + write!( + f, + "{}…", + self.0.chars().take(29).collect::() + ) + } else { + write!( + f, + "{}", + self.0 + ) + } + } + } + }; +} + +string_wrapper!(ContainerName); +string_wrapper!(ContainerImage); + #[derive(Debug, Clone)] pub struct StatefulList { pub state: ListState, @@ -428,51 +474,6 @@ impl Logs { } } -/// ContainerName and ContainerImage are simple structs, used so can implement custom fmt functions to them -macro_rules! string_wrapper { - ($name:ident) => { - #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] - pub struct $name(String); - - impl From for $name { - fn from(value: String) -> Self { - Self(value) - } - } - - impl$name { - pub fn get(&self) -> String { - self.0.clone() - } - - pub fn set(&mut self, value: String) { - self.0 = value; - } - } - - impl std::fmt::Display for $name { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - if self.0.chars().count() >= 30 { - write!( - f, - "{}…", - self.0.chars().take(29).collect::() - ) - } else { - write!( - f, - "{}", - self.0 - ) - } - } - } - }; -} - -string_wrapper!(ContainerName); -string_wrapper!(ContainerImage); - /// Info for each container #[derive(Debug, Clone)] pub struct ContainerItem { diff --git a/src/app_data/mod.rs b/src/app_data/mod.rs index ab3caf7..a9fcadc 100644 --- a/src/app_data/mod.rs +++ b/src/app_data/mod.rs @@ -171,52 +171,52 @@ impl AppData { .state .order() .cmp(&item_ord.1.state.order()) - .then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())), + .then_with(|| item_ord.0.name.get().cmp(item_ord.1.name.get())), Header::Status => item_ord .0 .status .cmp(&item_ord.1.status) - .then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())), + .then_with(|| item_ord.0.name.get().cmp(item_ord.1.name.get())), Header::Cpu => item_ord .0 .cpu_stats .back() .cmp(&item_ord.1.cpu_stats.back()) - .then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())), + .then_with(|| item_ord.0.name.get().cmp(item_ord.1.name.get())), Header::Memory => item_ord .0 .mem_stats .back() .cmp(&item_ord.1.mem_stats.back()) - .then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())), + .then_with(|| item_ord.0.name.get().cmp(item_ord.1.name.get())), Header::Id => item_ord .0 .id .cmp(&item_ord.1.id) - .then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())), + .then_with(|| item_ord.0.name.get().cmp(item_ord.1.name.get())), Header::Image => item_ord .0 .image .get() - .cmp(&item_ord.1.image.get()) - .then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())), + .cmp(item_ord.1.image.get()) + .then_with(|| item_ord.0.name.get().cmp(item_ord.1.name.get())), Header::Rx => item_ord .0 .rx .cmp(&item_ord.1.rx) - .then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())), + .then_with(|| item_ord.0.name.get().cmp(item_ord.1.name.get())), Header::Tx => item_ord .0 .tx .cmp(&item_ord.1.tx) - .then_with(|| item_ord.0.name.get().cmp(&item_ord.1.name.get())), + .then_with(|| item_ord.0.name.get().cmp(item_ord.1.name.get())), Header::Name => item_ord .0 .name .get() - .cmp(&item_ord.1.name.get()) + .cmp(item_ord.1.name.get()) .then_with(|| item_ord.0.id.cmp(&item_ord.1.id)), } }; @@ -225,7 +225,7 @@ impl AppData { self.containers.items.sort_by(|a, b| { a.created .cmp(&b.created) - .then_with(|| a.name.get().cmp(&b.name.get())) + .then_with(|| a.name.get().cmp(b.name.get())) }); } } @@ -509,7 +509,7 @@ impl AppData { /// Get the Id and State for the currently selected container - used by the exec check method pub fn get_selected_container_id_state_name(&self) -> Option<(ContainerId, State, String)> { self.get_selected_container() - .map(|i| (i.id.clone(), i.state, i.name.get())) + .map(|i| (i.id.clone(), i.state, i.name.get().to_owned())) } /// Update container mem, cpu, & network stats, in single function so only need to call .lock() once diff --git a/src/ui/draw_blocks.rs b/src/ui/draw_blocks.rs index a3acb47..8bbc176 100644 --- a/src/ui/draw_blocks.rs +++ b/src/ui/draw_blocks.rs @@ -13,7 +13,7 @@ use ratatui::{ use std::{default::Default, time::Instant}; use std::{fmt::Display, sync::Arc}; -use crate::app_data::{ContainerItem, Header, SortedOrder, ContainerName}; +use crate::app_data::{ContainerItem, ContainerName, Header, SortedOrder}; use crate::{ app_data::{AppData, ByteStats, Columns, CpuStats, State, Stats}, app_error::AppError,