refactors: unwraps() removed, and/or map_or() used

This commit is contained in:
Jack Wills
2022-09-07 02:53:28 +00:00
parent 6c1aebd500
commit eb25744472
5 changed files with 41 additions and 33 deletions
+33 -24
View File
@@ -313,9 +313,7 @@ impl AppData {
/// So can display nicely and evenly /// So can display nicely and evenly
pub fn get_width(&self) -> Columns { pub fn get_width(&self) -> Columns {
let mut output = Columns::new(); let mut output = Columns::new();
// Think this is causing issues
let count = |x: &String| x.chars().count(); let count = |x: &String| x.chars().count();
// let count = |_:&String|10;
for container in &self.containers.items { for container in &self.containers.items {
let cpu_count = count( let cpu_count = count(
@@ -412,7 +410,7 @@ impl AppData {
} }
/// Update, or insert, containers /// Update, or insert, containers
pub fn update_containers(&mut self, containers: &[ContainerSummary]) { pub fn update_containers(&mut self, containers: &mut [ContainerSummary]) {
let all_ids = self.get_all_ids(); let all_ids = self.get_all_ids();
if !containers.is_empty() && self.containers.state.selected().is_none() { if !containers.is_empty() && self.containers.state.selected().is_none() {
@@ -437,47 +435,58 @@ impl AppData {
} }
} }
for i in containers.iter() { for i in containers.iter_mut() {
if let Some(id) = i.id.as_ref() { if let Some(id) = i.id.as_ref() {
// maybe if no name then continue? // maybe if no name then continue?
let name = i.names.as_ref().map_or("".to_owned(), |f|f.get(0).map_or("".to_owned(), |f|{ let name = i.names.as_mut().map_or("".to_owned(), |n| {
let mut n = f.clone(); n.get_mut(0).map_or("".to_owned(), |f| {
if n.starts_with('/') { if f.starts_with('/') {
n.remove(0); f.remove(0);
} }
n f.to_string()
})); })
});
let state = State::from(i.state.as_ref().map_or("dead".to_owned(), |f|f.trim().to_owned())); let state = State::from(
let status = i.status.as_ref().map_or("".to_owned(), |f| f.trim().to_owned()); i.state
let image = i.image.as_ref().map_or("".to_owned(), |f|f.clone()); .as_ref()
.map_or("dead".to_owned(), |f| f.trim().to_owned()),
);
let status = i
.status
.as_ref()
.map_or("".to_owned(), |f| f.trim().to_owned());
let image = i.image.as_ref().map_or("".to_owned(), |f| f.clone());
if let Some(current_container) = self.get_container_by_id(id) { if let Some(current_container) = self.get_container_by_id(id) {
if current_container.name != name { if current_container.name != name {
current_container.name = name; current_container.name = name;
}; };
if current_container.status != status { if current_container.status != status {
current_container.status = status; current_container.status = status;
}; };
if current_container.state != state { 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 // Update the list state, needs to be None if the gen_vec returns an empty vec
match state { match state {
State::Removing | State::Restarting | State::Unknown => { State::Removing | State::Restarting | State::Unknown => {
current_container.docker_controls.state.select(None); current_container.docker_controls.state.select(None);
} }
_ => current_container.docker_controls.start(), _ => current_container.docker_controls.start(),
}; };
current_container.state = state; current_container.state = state;
}; };
if current_container.image != image { if current_container.image != image {
// limit image name to 64 chars?
// current_container.image = image.chars().into_iter().take(64).collect(); // current_container.image = image.chars().into_iter().take(64).collect();
current_container.image = image; current_container.image = image;
}; };
} else { } 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); let mut container = ContainerItem::new(id.clone(), status, image, state, name);
container.logs.end(); container.logs.end();
self.containers.items.push(container); self.containers.items.push(container);
} }
@@ -507,7 +516,7 @@ impl AppData {
} }
if container.logs.state.selected().is_none() if container.logs.state.selected().is_none()
|| container.logs.state.selected().unwrap_or_default() + 1 == current_len || container.logs.state.selected().map_or(1, |f| f + 1) == current_len
{ {
container.logs.end(); container.logs.end();
} }
-1
View File
@@ -27,6 +27,5 @@ impl fmt::Display for AppError {
write!(f, "Unbale to {}able mouse capture", reason) write!(f, "Unbale to {}able mouse capture", reason)
} }
} }
} }
} }
+2 -2
View File
@@ -28,7 +28,7 @@ use ui::{create_ui, GuiState};
fn setup_tracing() { fn setup_tracing() {
tracing_subscriber::fmt().with_max_level(Level::INFO).init(); tracing_subscriber::fmt().with_max_level(Level::INFO).init();
// TODO write to file? // TODO write to file?
} }
#[tokio::main] #[tokio::main]
@@ -100,7 +100,7 @@ async fn main() {
.unwrap_or(()); .unwrap_or(());
} else { } else {
loop { loop {
// TODO this needs to be improved to display something useful // TODO this needs to be improved to display something useful
info!("in debug mode"); info!("in debug mode");
tokio::time::sleep(std::time::Duration::from_millis(5000)).await; tokio::time::sleep(std::time::Duration::from_millis(5000)).await;
} }
+2 -2
View File
@@ -279,8 +279,8 @@ pub fn chart<B: Backend>(
.style(Style::default().fg(Color::Cyan)) .style(Style::default().fg(Color::Cyan))
.graph_type(GraphType::Line) .graph_type(GraphType::Line)
.data(&mem.0)]; .data(&mem.0)];
let cpu_stats = CpuStats::new(cpu.0.last().unwrap_or(&(0.00, 0.00)).1); let cpu_stats = CpuStats::new(cpu.0.last().map_or(0.00, |f| f.1));
let mem_stats = ByteStats::new(mem.0.last().unwrap_or(&(0.0, 0.0)).1 as u64); let mem_stats = ByteStats::new(mem.0.last().map_or(0, |f| f.1 as u64));
let cpu_chart = make_chart(&cpu.2, "cpu", cpu_dataset, &cpu_stats, &cpu.1); let cpu_chart = make_chart(&cpu.2, "cpu", cpu_dataset, &cpu_stats, &cpu.1);
let mem_chart = make_chart(&mem.2, "memory", mem_dataset, &mem_stats, &mem.1); let mem_chart = make_chart(&mem.2, "memory", mem_dataset, &mem_stats, &mem.1);
+1 -1
View File
@@ -63,7 +63,7 @@ pub async fn create_ui(
LeaveAlternateScreen, LeaveAlternateScreen,
DisableMouseCapture DisableMouseCapture
)?; )?;
terminal.show_cursor().unwrap_or(()); terminal.show_cursor()?;
if let Err(err) = res { if let Err(err) = res {
println!("{}", err); println!("{}", err);