refactor: string_wrapper .get() return &str

This commit is contained in:
Jack Wills
2024-01-05 14:14:55 +00:00
parent 69af9be71f
commit a722731c6a
3 changed files with 59 additions and 58 deletions
+46 -45
View File
@@ -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,
@@ -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 {
+12 -12
View File
@@ -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