Skip to content

Commit

Permalink
refactor: string_wrapper .get() return &str
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjackwills committed Jan 5, 2024
1 parent 69af9be commit a722731
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 58 deletions.
91 changes: 46 additions & 45 deletions src/app_data/container_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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::<String>()
)
} else {
write!(
f,
"{}",
self.0
)
}
}
}
};
}

string_wrapper!(ContainerName);
string_wrapper!(ContainerImage);

#[derive(Debug, Clone)]
pub struct StatefulList<T> {
pub state: ListState,
Expand Down Expand Up @@ -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<String> 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::<String>()
)
} else {
write!(
f,
"{}",
self.0
)
}
}
}
};
}

string_wrapper!(ContainerName);
string_wrapper!(ContainerImage);

/// Info for each container
#[derive(Debug, Clone)]
pub struct ContainerItem {
Expand Down
24 changes: 12 additions & 12 deletions src/app_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
};
Expand All @@ -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()))
});
}
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ui/draw_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a722731

Please # to comment.