refactor: use map_or_else

This commit is contained in:
Jack Wills
2022-10-02 00:31:01 +00:00
parent a7b8df6b21
commit f5fc446295
5 changed files with 38 additions and 33 deletions
+6 -6
View File
@@ -66,12 +66,12 @@ impl AppData {
pub fn set_sorted(&mut self, x: Option<(Header, SortedOrder)>) { pub fn set_sorted(&mut self, x: Option<(Header, SortedOrder)>) {
self.sorted_by = x; self.sorted_by = x;
self.sort_containers(); self.sort_containers();
self.containers.state.select( self.containers
self.containers .state
.items .select(self.containers.items.iter().position(|i| {
.iter() self.get_selected_container_id()
.position(|i| Some(i.id.clone()) == self.get_selected_container_id()), .map_or_else(|| false, |id| i.id == id)
); }));
} }
/// Generate a default app_state /// Generate a default app_state
pub fn default(args: CliArgs) -> Self { pub fn default(args: CliArgs) -> Self {
+2 -3
View File
@@ -78,9 +78,8 @@ impl DockerData {
.cpu_stats .cpu_stats
.cpu_usage .cpu_usage
.percpu_usage .percpu_usage
.clone() .as_ref()
.unwrap_or_default() .map_or_else(|| 0, |i| i.len()) as u64
.len() as u64
}) as f64; }) as f64;
if system_delta > 0.0 && cpu_delta > 0.0 { if system_delta > 0.0 && cpu_delta > 0.0 {
cpu_percentage = (cpu_delta / system_delta) * online_cpus * 100.0; cpu_percentage = (cpu_delta / system_delta) * online_cpus * 100.0;
+1 -1
View File
@@ -137,7 +137,7 @@ impl InputHandler {
} }
/// Handle any keyboard button events /// Handle any keyboard button events
#[allow(clippy::too_many_lines)] #[allow(clippy::too_many_lines)]
async fn button_press(&mut self, key_code: KeyCode) { async fn button_press(&mut self, key_code: KeyCode) {
let show_error = self.app_data.lock().show_error; let show_error = self.app_data.lock().show_error;
let show_info = self.gui_state.lock().show_help; let show_info = self.gui_state.lock().show_help;
+22 -22
View File
@@ -443,7 +443,10 @@ pub fn heading_bar<B: Backend>(
let column_width = usize::from(area.width) - info_width; let column_width = usize::from(area.width) - info_width;
let column_width = if column_width > 0 { column_width } else { 1 }; let column_width = if column_width > 0 { column_width } else { 1 };
let splits = if has_containers { let splits = if has_containers {
vec![Constraint::Min(column_width.try_into().unwrap_or_default()), Constraint::Min(info_width.try_into().unwrap_or_default())] vec![
Constraint::Min(column_width.try_into().unwrap_or_default()),
Constraint::Min(info_width.try_into().unwrap_or_default()),
]
} else { } else {
vec![Constraint::Percentage(100)] vec![Constraint::Percentage(100)]
}; };
@@ -541,19 +544,20 @@ pub fn help_box<B: Backend>(f: &mut Frame<'_, B>) {
.border_type(BorderType::Rounded) .border_type(BorderType::Rounded)
.border_style(Style::default().fg(Color::Black)); .border_style(Style::default().fg(Color::Black));
let area = popup( let area = popup(lines, max_line_width, f.size(), BoxLocation::MiddleCentre);
lines,
max_line_width,
f.size(),
BoxLocation::MiddleCentre,
);
let split_popup = Layout::default() let split_popup = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.constraints( .constraints(
[ [
Constraint::Max(NAME_TEXT.lines().count().try_into().unwrap_or_default()), Constraint::Max(NAME_TEXT.lines().count().try_into().unwrap_or_default()),
Constraint::Max(description_text.lines().count().try_into().unwrap_or_default()), Constraint::Max(
description_text
.lines()
.count()
.try_into()
.unwrap_or_default(),
),
Constraint::Max(help_text.lines().count().try_into().unwrap_or_default()), Constraint::Max(help_text.lines().count().try_into().unwrap_or_default()),
] ]
.as_ref(), .as_ref(),
@@ -605,12 +609,7 @@ pub fn error<B: Backend>(f: &mut Frame<'_, B>, error: AppError, seconds: Option<
.block(block) .block(block)
.alignment(Alignment::Center); .alignment(Alignment::Center);
let area = popup( let area = popup(lines, max_line_width, f.size(), BoxLocation::MiddleCentre);
lines,
max_line_width,
f.size(),
BoxLocation::MiddleCentre,
);
f.render_widget(Clear, area); f.render_widget(Clear, area);
f.render_widget(paragraph, area); f.render_widget(paragraph, area);
} }
@@ -634,12 +633,7 @@ pub fn info<B: Backend>(f: &mut Frame<'_, B>, text: String) {
.block(block) .block(block)
.alignment(Alignment::Center); .alignment(Alignment::Center);
let area = popup( let area = popup(lines, max_line_width, f.size(), BoxLocation::BottomRight);
lines,
max_line_width,
f.size(),
BoxLocation::BottomRight,
);
f.render_widget(Clear, area); f.render_widget(Clear, area);
f.render_widget(paragraph, area); f.render_widget(paragraph, area);
} }
@@ -658,8 +652,14 @@ fn popup(text_lines: usize, text_width: usize, r: Rect, box_location: BoxLocatio
1 1
}; };
let v_constraints = box_location.get_vertical_constraints(blank_vertical.try_into().unwrap_or_default(), text_lines.try_into().unwrap_or_default()); let v_constraints = box_location.get_vertical_constraints(
let h_constraints = box_location.get_horizontal_constraints(blank_horizontal.try_into().unwrap_or_default(), text_width.try_into().unwrap_or_default()); blank_vertical.try_into().unwrap_or_default(),
text_lines.try_into().unwrap_or_default(),
);
let h_constraints = box_location.get_horizontal_constraints(
blank_horizontal.try_into().unwrap_or_default(),
text_width.try_into().unwrap_or_default(),
);
let indexes = box_location.get_indexes(); let indexes = box_location.get_indexes();
+7 -1
View File
@@ -168,7 +168,13 @@ fn ui<B: Backend>(
// Split into 3, containers+controls, logs, then graphs // Split into 3, containers+controls, logs, then graphs
let upper_main = Layout::default() let upper_main = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.constraints([Constraint::Max(height.try_into().unwrap_or_default()), Constraint::Percentage(50)].as_ref()) .constraints(
[
Constraint::Max(height.try_into().unwrap_or_default()),
Constraint::Percentage(50),
]
.as_ref(),
)
.split(whole_layout[1]); .split(whole_layout[1]);
let top_split = if has_containers { let top_split = if has_containers {