From a98e8b7260f00dfa882ded23fd24fdbe941cdbb9 Mon Sep 17 00:00:00 2001 From: Jack Wills <32690432+mrjackwills@users.noreply.github.com> Date: Sun, 16 Oct 2022 00:00:41 +0000 Subject: [PATCH] chore: cargo fmt --- src/docker_data/mod.rs | 37 +++++++++++-------------------------- src/input_handler/mod.rs | 10 +++++----- src/main.rs | 38 +++++++++++++++++--------------------- src/ui/draw_blocks.rs | 10 +++++++--- src/ui/gui_state.rs | 16 +++++++++------- src/ui/mod.rs | 4 ---- 6 files changed, 49 insertions(+), 66 deletions(-) diff --git a/src/docker_data/mod.rs b/src/docker_data/mod.rs index ed1d4d5..f80c516 100644 --- a/src/docker_data/mod.rs +++ b/src/docker_data/mod.rs @@ -316,7 +316,7 @@ impl DockerData { self.gui_state.lock().remove_loading(loading_uuid); } - // Initialize docker container data, before any messages are received + /// Initialize docker container data, before any messages are received async fn initialise_container_data(&mut self) { self.gui_state.lock().status_push(Status::Init); let loading_uuid = Uuid::new_v4(); @@ -341,7 +341,7 @@ impl DockerData { self.stop_loading_spin(&loading_spin, loading_uuid); } - /// Set the global error as the dockererror, and set gui_state to errro + /// Set the global error as the docker error, and set gui_state to error fn set_error(&mut self, error: DockerControls) { self.app_data .lock() @@ -349,13 +349,13 @@ impl DockerData { self.gui_state.lock().status_push(Status::Error); } - /// Execute docker commands, will start and stop the loading spinner + /// Execute a docker command, will start and stop the loading spinner, and set correct error async fn exec_docker( &mut self, - docker_fn: impl Future>, - uuid: Uuid, + docker_fn: impl Future> + Send, control: DockerControls, ) { + let uuid = Uuid::new_v4(); let loading_spin = self.loading_spin(uuid).await; if docker_fn.await.is_err() { self.set_error(control); @@ -366,21 +366,15 @@ impl DockerData { /// Handle incoming messages, container controls & all container information update async fn message_handler(&mut self) { while let Some(message) = self.receiver.recv().await { - let loading_uuid = Uuid::new_v4(); let docker = Arc::clone(&self.docker); match message { DockerMessage::Pause(id) => { - self.exec_docker( - docker.pause_container(id.get()), - loading_uuid, - DockerControls::Pause, - ) - .await; + self.exec_docker(docker.pause_container(id.get()), DockerControls::Pause) + .await; } DockerMessage::Restart(id) => { self.exec_docker( docker.restart_container(id.get(), None), - loading_uuid, DockerControls::Restart, ) .await; @@ -388,26 +382,17 @@ impl DockerData { DockerMessage::Start(id) => { self.exec_docker( docker.start_container(id.get(), None::>), - loading_uuid, DockerControls::Start, ) .await; } DockerMessage::Stop(id) => { - self.exec_docker( - docker.stop_container(id.get(), None), - loading_uuid, - DockerControls::Stop, - ) - .await; + self.exec_docker(docker.stop_container(id.get(), None), DockerControls::Stop) + .await; } DockerMessage::Unpause(id) => { - self.exec_docker( - docker.unpause_container(id.get()), - loading_uuid, - DockerControls::Unpause, - ) - .await; + self.exec_docker(docker.unpause_container(id.get()), DockerControls::Unpause) + .await; self.update_everything().await; } DockerMessage::Update => self.update_everything().await, diff --git a/src/input_handler/mod.rs b/src/input_handler/mod.rs index 9cc6208..fb10171 100644 --- a/src/input_handler/mod.rs +++ b/src/input_handler/mod.rs @@ -69,7 +69,7 @@ impl InputHandler { .lock() .status_contains(&[Status::Error, Status::Help]); if !error_or_help { - self.mouse_press(mouse_event) + self.mouse_press(mouse_event); } } } @@ -105,7 +105,7 @@ impl InputHandler { } }; - // If the info box sleep handle is currently being executed, as in m is pressed twice within a 4000ms window + // If the info box sleep handle is currently being executed, as in 'm' is pressed twice within a 4000ms window // then cancel the first handle, as a new handle will be invoked if let Some(info_sleep_timer) = self.info_sleep.as_ref() { info_sleep_timer.abort(); @@ -134,21 +134,21 @@ impl InputHandler { } /// Send a quit message to docker, to abort all spawns, if an error is return, set is_running to false here instead + /// If gui_status is Error or Init, then just set the is_running to false immediately, for a quicker exit async fn quit(&self) { let error_init = self .gui_state .lock() .status_contains(&[Status::Error, Status::Init]); - if error_init { + if error_init || self.docker_sender.send(DockerMessage::Quit).await.is_err() { self.is_running.store(false, Ordering::SeqCst); - } else if self.docker_sender.send(DockerMessage::Quit).await.is_err() { - self.is_running.store(false, Ordering::SeqCst) } } /// Handle any keyboard button events #[allow(clippy::too_many_lines)] async fn button_press(&mut self, key_code: KeyCode) { + // TODO - refactor this to a single call, maybe return Error, Help or Normal let contains_error = self.gui_state.lock().status_contains(&[Status::Error]); let contains_help = self.gui_state.lock().status_contains(&[Status::Help]); diff --git a/src/main.rs b/src/main.rs index 9d03204..6f69c8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,29 +45,25 @@ async fn main() { let (docker_sx, docker_rx) = tokio::sync::mpsc::channel(16); // Create docker daemon handler, and only spawn up the docker data handler if ping returns non-error - match Docker::connect_with_socket_defaults() { - Ok(docker) => match docker.ping().await { - Ok(_) => { - let docker = Arc::new(docker); - let is_running = Arc::clone(&is_running); - tokio::spawn(DockerData::init( - args, - docker_app_data, - docker, - docker_gui_state, - docker_rx, - is_running, - )); - } - Err(_) => { - app_data.lock().set_error(AppError::DockerConnect); - docker_gui_state.lock().status_push(Status::DockerConnect) - } - }, - Err(_) => { + if let Ok(docker) = Docker::connect_with_socket_defaults() { + if docker.ping().await.is_ok() { + let docker = Arc::new(docker); + let is_running = Arc::clone(&is_running); + tokio::spawn(DockerData::init( + args, + docker_app_data, + docker, + docker_gui_state, + docker_rx, + is_running, + )); + } else { app_data.lock().set_error(AppError::DockerConnect); - docker_gui_state.lock().status_push(Status::DockerConnect) + docker_gui_state.lock().status_push(Status::DockerConnect); } + } else { + app_data.lock().set_error(AppError::DockerConnect); + docker_gui_state.lock().status_push(Status::DockerConnect); } let input_app_data = Arc::clone(&app_data); diff --git a/src/ui/draw_blocks.rs b/src/ui/draw_blocks.rs index e827cdb..4ca9be5 100644 --- a/src/ui/draw_blocks.rs +++ b/src/ui/draw_blocks.rs @@ -51,7 +51,9 @@ fn generate_block<'a>( gui_state: &Arc>, panel: SelectablePanel, ) -> Block<'a> { - gui_state.lock().update_map(Region::Panel(panel), area); + gui_state + .lock() + .update_heading_map(Region::Panel(panel), area); let current_selected_panel = gui_state.lock().selected_panel; let title = match panel { SelectablePanel::Containers => { @@ -466,7 +468,9 @@ pub fn heading_bar( // draw the actual header blocks for (index, (paragraph, header, _)) in header_data.into_iter().enumerate() { let rect = headers_section[index]; - gui_state.lock().update_map(Region::Header(header), rect); + gui_state + .lock() + .update_heading_map(Region::Header(header), rect); f.render_widget(paragraph, rect); } } @@ -501,7 +505,7 @@ fn max_line_width(text: &str) -> usize { } /// Draw the help box in the centre of the screen -/// TODO this is message, should make every line it's own renderable span +/// TODO should make every line it's own renderable span pub fn help_box(f: &mut Frame<'_, B>) { let title = format!(" {} ", VERSION); diff --git a/src/ui/gui_state.rs b/src/ui/gui_state.rs index 8a9bc93..9fc0a0a 100644 --- a/src/ui/gui_state.rs +++ b/src/ui/gui_state.rs @@ -173,7 +173,7 @@ impl fmt::Display for Loading { } } -/// The application can be in these four states +/// The application gui state can be in multiple of these four states at the same time /// Various functions (e.g input handler), operate differently depending upon current Status #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] pub enum Status { @@ -237,7 +237,7 @@ impl GuiState { } /// Insert, or updates header area panel into heading_map - pub fn update_map(&mut self, region: Region, area: Rect) { + pub fn update_heading_map(&mut self, region: Region, area: Rect) { match region { Region::Header(header) => self .heading_map @@ -252,16 +252,19 @@ impl GuiState { }; } - pub fn status_push(&mut self, status: Status) { - self.status.insert(status); + /// Check if the current gui_status contains any of the given status' + pub fn status_contains(&self, status: &[Status]) -> bool { + status.iter().any(|i| self.status.contains(i)) } + /// Remove a gui_status into the current gui_status hashset pub fn status_del(&mut self, status: Status) { self.status.remove(&status); } - pub fn status_contains(&self, status: &[Status]) -> bool { - status.iter().any(|i| self.status.contains(i)) + /// Insert a gui_status into the current gui_status hashset + pub fn status_push(&mut self, status: Status) { + self.status.insert(status); } /// Change to next selectable panel @@ -281,7 +284,6 @@ impl GuiState { } /// If is_loading has any entries, return the current loading_icon, else an emtpy string - // Option? pub fn get_loading(&mut self) -> String { if self.is_loading.is_empty() { String::from(" ") diff --git a/src/ui/mod.rs b/src/ui/mod.rs index e93a671..a3d0517 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -83,10 +83,7 @@ async fn run_app( update_duration: Duration, ) -> Result<(), AppError> { let input_poll_rate = std::time::Duration::from_millis(75); - - // Check for docker connect errors before attempting to draw the gui let status_dockerconnect = gui_state.lock().status_contains(&[Status::DockerConnect]); - if status_dockerconnect { let mut seconds = 5; loop { @@ -139,7 +136,6 @@ async fn run_app( break; } } - // } } Ok(()) }