refactor: Impl Copy where able to

This commit is contained in:
Jack Wills
2022-10-01 19:57:27 +00:00
parent 15597dbe69
commit e76878f424
14 changed files with 138 additions and 118 deletions
+33 -32
View File
@@ -7,7 +7,6 @@ use tui::{
use super::Header;
const ONE_KB: f64 = 1000.0;
const ONE_MB: f64 = ONE_KB * 1000.0;
const ONE_GB: f64 = ONE_MB * 1000.0;
@@ -25,6 +24,7 @@ impl<T> StatefulList<T> {
items,
}
}
pub fn end(&mut self) {
let len = self.items.len();
if len > 0 {
@@ -83,7 +83,7 @@ impl<T> StatefulList<T> {
}
/// States of the container
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd)]
pub enum State {
Dead,
Exited,
@@ -95,7 +95,7 @@ pub enum State {
}
impl State {
pub const fn get_color(&self) -> Color {
pub const fn get_color(self) -> Color {
match self {
Self::Running => Color::Green,
Self::Removing => Color::LightRed,
@@ -105,15 +105,15 @@ impl State {
}
}
// Dirty way to create order for the state, rather than impl Ord
pub const fn order(&self) -> &'static str {
pub const fn order(self) -> u8 {
match self {
Self::Running => "a",
Self::Paused => "b",
Self::Restarting => "c",
Self::Removing => "d",
Self::Exited => "e",
Self::Dead => "f",
Self::Unknown => "g",
Self::Running => 0,
Self::Paused => 1,
Self::Restarting => 2,
Self::Removing => 3,
Self::Exited => 4,
Self::Dead => 5,
Self::Unknown => 6,
}
}
}
@@ -162,7 +162,7 @@ impl fmt::Display for State {
}
/// Items for the container control list
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub enum DockerControls {
Pause,
Unpause,
@@ -172,7 +172,7 @@ pub enum DockerControls {
}
impl DockerControls {
pub const fn get_color(&self) -> Color {
pub const fn get_color(self) -> Color {
match self {
Self::Start => Color::Green,
Self::Stop => Color::Red,
@@ -182,7 +182,8 @@ impl DockerControls {
}
}
pub fn gen_vec(state: &State) -> Vec<Self> {
/// Docker commands available depending on the containers state
pub fn gen_vec(state: State) -> Vec<Self> {
match state {
State::Dead | State::Exited => vec![Self::Start, Self::Restart],
State::Paused => vec![Self::Unpause, Self::Stop],
@@ -213,7 +214,7 @@ pub trait Stats {
/// Struct for frequently updated CPU stats
/// So can use custom display formatter
/// Use trait Stats for use as generic in draw_chart function
#[derive(Clone, Debug)]
#[derive(Debug, Clone, Copy)]
pub struct CpuStats {
value: f64,
}
@@ -266,7 +267,7 @@ impl fmt::Display for CpuStats {
/// Struct for frequently updated memory usage stats
/// So can use custom display formatter
/// Use trait Stats for use as generic in draw_chart function
#[derive(Clone, Debug, Eq)]
#[derive(Debug, Clone, Copy, Eq)]
pub struct ByteStats {
value: u64,
}
@@ -303,10 +304,9 @@ impl Stats for ByteStats {
}
}
// convert from bytes to kB, MB, GB etc
/// convert from bytes to kB, MB, GB etc
impl fmt::Display for ByteStats {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// TODO these can be consts outside of this definition
let as_f64 = self.value as f64;
let p = match as_f64 {
x if x >= ONE_GB => format!("{y:.2} GB", y = as_f64 / ONE_GB),
@@ -318,6 +318,10 @@ impl fmt::Display for ByteStats {
}
}
pub type MemTuple = (Vec<(f64, f64)>, ByteStats, State);
pub type CpuTuple = (Vec<(f64, f64)>, CpuStats, State);
/// Info for each container
#[derive(Debug, Clone)]
pub struct ContainerItem {
@@ -336,13 +340,10 @@ pub struct ContainerItem {
pub status: String,
}
pub type MemTuple = (Vec<(f64, f64)>, ByteStats, State);
pub type CpuTuple = (Vec<(f64, f64)>, CpuStats, State);
impl ContainerItem {
/// Create a new container item
pub fn new(id: String, status: String, image: String, state: State, name: String) -> Self {
let mut docker_controls = StatefulList::new(DockerControls::gen_vec(&state));
let mut docker_controls = StatefulList::new(DockerControls::gen_vec(state));
docker_controls.start();
Self {
cpu_stats: VecDeque::with_capacity(60),
@@ -361,18 +362,18 @@ impl ContainerItem {
}
}
/// Find the max value in the last 30 items in the cpu stats vec
/// Find the max value in the cpu stats VecDeque
fn max_cpu_stats(&self) -> CpuStats {
match self.cpu_stats.iter().max() {
Some(value) => value.clone(),
Some(value) => *value,
None => CpuStats::new(0.0),
}
}
/// Find the max value in the last 30 items in the mem stats vec
/// Find the max value in the mem stats VecDeque
fn max_mem_stats(&self) -> ByteStats {
match self.mem_stats.iter().max() {
Some(value) => value.clone(),
Some(value) => *value,
None => ByteStats::new(0),
}
}
@@ -382,11 +383,11 @@ impl ContainerItem {
self.cpu_stats
.iter()
.enumerate()
.map(|i| (i.0 as f64, i.1.value))
.map(|i| (i.0 as f64, i.1.value as f64))
.collect::<Vec<_>>()
}
/// Convert mem stats into a vec for the charts function
/// Convert mem stats into a Vec for the charts function
fn get_mem_dataset(&self) -> Vec<(f64, f64)> {
self.mem_stats
.iter()
@@ -400,7 +401,7 @@ impl ContainerItem {
(
self.get_cpu_dataset(),
self.max_cpu_stats(),
self.state.clone(),
self.state,
)
}
@@ -409,7 +410,7 @@ impl ContainerItem {
(
self.get_mem_dataset(),
self.max_mem_stats(),
self.state.clone(),
self.state,
)
}
@@ -421,7 +422,7 @@ impl ContainerItem {
}
/// Container information panel headings + widths, for nice pretty formatting
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub struct Columns {
pub state: (Header, usize),
pub status: (Header, usize),
@@ -450,4 +451,4 @@ impl Columns {
net_tx: (Header::Tx, 5),
}
}
}
}
+48 -34
View File
@@ -9,7 +9,7 @@ use crate::{app_error::AppError, parse_args::CliArgs, ui::log_sanitizer};
pub use container_state::*;
/// Global app_state, stored in an Arc<Mutex>
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct AppData {
args: CliArgs,
error: Option<AppError>,
@@ -20,13 +20,13 @@ pub struct AppData {
sorted_by: Option<(Header, SortedOrder)>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum SortedOrder {
Asc,
Desc,
}
#[derive(Debug, Clone, PartialEq, Hash, Eq)]
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
pub enum Header {
State,
Status,
@@ -58,20 +58,19 @@ impl fmt::Display for Header {
}
impl AppData {
pub fn get_sorted(&self) -> Option<(Header, SortedOrder)> {
self.sorted_by.clone()
pub const fn get_sorted(&self) -> Option<(Header, SortedOrder)> {
self.sorted_by
}
/// Change the sorted order, also set the selected container state to match new order
pub fn set_sorted(&mut self, x: Option<(Header, SortedOrder)>) {
self.sorted_by = x;
let id = self.get_selected_container_id();
self.sort_containers();
self.containers.state.select(
self.containers
.items
.iter()
.position(|i| Some(i.id.clone()) == id),
.position(|i| Some(i.id.clone()) == self.get_selected_container_id()),
);
}
/// Generate a default app_state
@@ -106,8 +105,7 @@ impl AppData {
.state
.selected()
{
output =
Some(self.containers.items[index].docker_controls.items[control_index].clone());
output = Some(self.containers.items[index].docker_controls.items[control_index]);
}
}
output
@@ -116,34 +114,42 @@ impl AppData {
/// Change selected choice of docker commands of selected container
pub fn docker_command_next(&mut self) {
if let Some(index) = self.containers.state.selected() {
self.containers.items[index].docker_controls.next();
if let Some(i) = self.containers.items.get_mut(index) {
i.docker_controls.next();
}
}
}
/// Change selected choice of docker commands of selected container
pub fn docker_command_previous(&mut self) {
if let Some(index) = self.containers.state.selected() {
self.containers.items[index].docker_controls.previous();
if let Some(i) = self.containers.items.get_mut(index) {
i.docker_controls.previous();
}
}
}
/// Change selected choice of docker commands of selected container
pub fn docker_command_start(&mut self) {
if let Some(index) = self.containers.state.selected() {
self.containers.items[index].docker_controls.start();
if let Some(i) = self.containers.items.get_mut(index) {
i.docker_controls.start();
}
}
}
/// Change selected choice of docker commands of selected container
pub fn docker_command_end(&mut self) {
if let Some(index) = self.containers.state.selected() {
self.containers.items[index].docker_controls.end();
if let Some(i) = self.containers.items.get_mut(index) {
i.docker_controls.end();
}
}
}
/// return single app_state error
pub fn get_error(&self) -> Option<AppError> {
self.error.clone()
pub const fn get_error(&self) -> Option<AppError> {
self.error
}
/// remove single app_state error
@@ -177,19 +183,19 @@ impl AppData {
/// Sort the containers vec, based on a heading, either ascending or descending
pub fn sort_containers(&mut self) {
if let Some((head, so)) = self.sorted_by.as_ref() {
if let Some((head, ord)) = self.sorted_by.as_ref() {
match head {
Header::State => match so {
Header::State => match ord {
SortedOrder::Desc => self
.containers
.items
.sort_by(|a, b| a.state.order().cmp(b.state.order())),
.sort_by(|a, b| a.state.order().cmp(&b.state.order())),
SortedOrder::Asc => self
.containers
.items
.sort_by(|a, b| b.state.order().cmp(a.state.order())),
.sort_by(|a, b| b.state.order().cmp(&a.state.order())),
},
Header::Status => match so {
Header::Status => match ord {
SortedOrder::Asc => self
.containers
.items
@@ -199,7 +205,7 @@ impl AppData {
.items
.sort_by(|a, b| b.status.cmp(&a.status)),
},
Header::Cpu => match so {
Header::Cpu => match ord {
SortedOrder::Asc => self
.containers
.items
@@ -209,7 +215,7 @@ impl AppData {
.items
.sort_by(|a, b| b.cpu_stats.back().cmp(&a.cpu_stats.back())),
},
Header::Memory => match so {
Header::Memory => match ord {
SortedOrder::Asc => self
.containers
.items
@@ -219,25 +225,25 @@ impl AppData {
.items
.sort_by(|a, b| b.mem_stats.back().cmp(&a.mem_stats.back())),
},
Header::Id => match so {
Header::Id => match ord {
SortedOrder::Asc => self.containers.items.sort_by(|a, b| a.id.cmp(&b.id)),
SortedOrder::Desc => self.containers.items.sort_by(|a, b| b.id.cmp(&a.id)),
},
Header::Image => match so {
Header::Image => match ord {
SortedOrder::Asc => self.containers.items.sort_by(|a, b| a.image.cmp(&b.image)),
SortedOrder::Desc => {
self.containers.items.sort_by(|a, b| b.image.cmp(&a.image));
}
},
Header::Name => match so {
Header::Name => match ord {
SortedOrder::Asc => self.containers.items.sort_by(|a, b| a.name.cmp(&b.name)),
SortedOrder::Desc => self.containers.items.sort_by(|a, b| b.name.cmp(&a.name)),
},
Header::Rx => match so {
Header::Rx => match ord {
SortedOrder::Asc => self.containers.items.sort_by(|a, b| a.rx.cmp(&b.rx)),
SortedOrder::Desc => self.containers.items.sort_by(|a, b| b.rx.cmp(&a.rx)),
},
Header::Tx => match so {
Header::Tx => match ord {
SortedOrder::Asc => self.containers.items.sort_by(|a, b| a.tx.cmp(&b.tx)),
SortedOrder::Desc => self.containers.items.sort_by(|a, b| b.tx.cmp(&a.tx)),
},
@@ -268,28 +274,36 @@ impl AppData {
/// select next selected log line
pub fn log_next(&mut self) {
if let Some(index) = self.get_selected_log_index() {
self.containers.items[index].logs.next();
if let Some(i) = self.containers.items.get_mut(index) {
i.logs.next();
}
}
}
/// select previous selected log line
pub fn log_previous(&mut self) {
if let Some(index) = self.get_selected_log_index() {
self.containers.items[index].logs.previous();
if let Some(i) = self.containers.items.get_mut(index) {
i.logs.previous();
}
}
}
/// select last selected log line
pub fn log_end(&mut self) {
if let Some(index) = self.get_selected_log_index() {
self.containers.items[index].logs.end();
if let Some(i) = self.containers.items.get_mut(index) {
i.logs.end();
}
}
}
/// select first selected log line
pub fn log_start(&mut self) {
if let Some(index) = self.get_selected_log_index() {
self.containers.items[index].logs.start();
if let Some(i) = self.containers.items.get_mut(index) {
i.logs.start();
}
}
}
@@ -470,7 +484,7 @@ impl AppData {
current_container.status = status;
};
if current_container.state != state {
current_container.docker_controls.items = DockerControls::gen_vec(&state);
current_container.docker_controls.items = DockerControls::gen_vec(state);
// Update the list state, needs to be None if the gen_vec returns an empty vec
match state {
@@ -488,7 +502,7 @@ impl AppData {
};
} else {
// limit image name to 64 chars?
// let mut container = ContainerItem::new(id.clone(), status, image.chars().into_iter().take(64).collect(), state, name);
// let mut container = ContainerItem::new(id.clone(), status, image.chars().into_iter().take(64).collect(), state, name);
let mut container = ContainerItem::new(id.clone(), status, image, state, name);
container.logs.end();
self.containers.items.push(container);
@@ -511,7 +525,7 @@ impl AppData {
let lines = if color {
log_sanitizer::colorize_logs(i)
} else if raw {
log_sanitizer::raw(i.clone())
log_sanitizer::raw(i)
} else {
log_sanitizer::remove_ansi(i)
};