From b8f5792d1865d3a398cd7f23aa9473a55dc6ea44 Mon Sep 17 00:00:00 2001 From: Jack Wills <32690432+mrjackwills@users.noreply.github.com> Date: Wed, 1 Mar 2023 19:18:36 +0000 Subject: [PATCH] refactor: dead code removed --- src/docker_data/mod.rs | 7 +----- src/main.rs | 7 ++---- src/ui/mod.rs | 51 ++++++++++++++++++++++-------------------- 3 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/docker_data/mod.rs b/src/docker_data/mod.rs index 8917654..4e7923b 100644 --- a/src/docker_data/mod.rs +++ b/src/docker_data/mod.rs @@ -3,15 +3,11 @@ use bollard::{ service::ContainerSummary, Docker, }; -use crossterm::{event::DisableMouseCapture, execute}; use futures_util::StreamExt; use parking_lot::Mutex; use std::{ collections::HashMap, - sync::{ - atomic::{AtomicBool, Ordering}, - Arc, - }, + sync::{atomic::AtomicBool, Arc}, }; use tokio::{sync::mpsc::Receiver, task::JoinHandle}; use uuid::Uuid; @@ -438,7 +434,6 @@ impl DockerData { spawns: Arc::new(Mutex::new(HashMap::new())), }; inner.initialise_container_data().await; - inner.message_handler().await; } } diff --git a/src/main.rs b/src/main.rs index 7f4ed7f..d2ee241 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ clippy::similar_names )] // Only allow when debugging -#![allow(unused)] +// #![allow(unused)] use app_data::AppData; use app_error::AppError; @@ -27,10 +27,7 @@ use docker_data::DockerData; use input_handler::InputMessages; use parking_lot::Mutex; use parse_args::CliArgs; -use std::{ - io::Write, - sync::{atomic::AtomicBool, Arc}, -}; +use std::sync::{atomic::AtomicBool, Arc}; use tokio::sync::mpsc::{Receiver, Sender}; use tracing::{info, Level}; diff --git a/src/ui/mod.rs b/src/ui/mod.rs index adcfb17..58dd9a0 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -6,8 +6,7 @@ use crossterm::{ }; use parking_lot::Mutex; use std::{ - io::{self, Stdout, Write}, - process::Stdio, + io::{self, Stdout}, sync::{atomic::Ordering, Arc}, }; use std::{sync::atomic::AtomicBool, time::Instant}; @@ -35,12 +34,13 @@ pub struct Ui { docker_sx: Sender, gui_state: Arc>, is_running: Arc, + now: Instant, sender: Sender, terminal: Terminal>, } impl Ui { - /// Create a new Ui struct, and execute the drawing loops + /// Create a new Ui struct, and execute the drawing loop pub async fn create( app_data: Arc>, docker_sx: Sender, @@ -48,35 +48,40 @@ impl Ui { is_running: Arc, sender: Sender, ) { - if let Ok(mut terminal) = Self::start_terminal() { + if let Ok(terminal) = Self::setup_terminal() { let mut ui = Self { app_data, docker_sx, gui_state, is_running, + now: Instant::now(), sender, terminal, }; if let Err(e) = ui.draw_ui().await { error!("{e}"); } - if let Err(e) = ui.end_terminal() { + if let Err(e) = ui.reset_terminal() { error!("{e}"); }; + } else { + error!("Terminal Error"); } } - // Setup the terminal for full-screen drawing mode, with mouse capture - fn start_terminal() -> io::Result>> { + /// Setup the terminal for full-screen drawing mode, with mouse capture + fn setup_terminal() -> io::Result>> { enable_raw_mode()?; let mut stdout = io::stdout(); - execute!(stdout, EnableMouseCapture, EnterAlternateScreen)?; + execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?; let backend = CrosstermBackend::new(stdout); Terminal::new(backend) } /// reset the terminal back to default settings - pub fn end_terminal(&mut self) -> Result<()> { + pub fn reset_terminal(&mut self) -> Result<()> { + self.terminal.clear()?; + disable_raw_mode()?; execute!( self.terminal.backend_mut(), @@ -90,20 +95,19 @@ impl Ui { /// Draw the the error message ui, for 5 seconds, with a countdown fn err_loop(&mut self) -> Result<(), AppError> { let mut seconds = 5; - let mut now = Instant::now(); loop { - // This is a fix for a weird bug on Linux + WSL which will output mouse movement to the stdout + // This is a fix for a weird bug on Linux + WSL which will output mouse movement to stdout std::thread::spawn(|| { execute!(io::stdout(), EnableMouseCapture).unwrap_or(()); execute!(io::stdout(), DisableMouseCapture).unwrap_or(()); }); - if now.elapsed() >= std::time::Duration::from_secs(1) { + if self.now.elapsed() >= std::time::Duration::from_secs(1) { seconds -= 1; - now = Instant::now(); + self.now = Instant::now(); } - - if seconds < 1 { + + if seconds < 1 { break; } @@ -120,14 +124,14 @@ impl Ui { /// The loop for drawing the main UI to the terminal async fn gui_loop(&mut self) -> Result<(), AppError> { - let input_poll_rate = std::time::Duration::from_millis(100); + let input_poll_rate = std::time::Duration::from_millis(1); let update_duration = std::time::Duration::from_millis(u64::from(self.app_data.lock().args.docker_interval)); - let mut now = Instant::now(); + while self.is_running.load(Ordering::SeqCst) { if self .terminal - .draw(|frame| ui(frame, &self.app_data, &self.gui_state)) + .draw(|frame| ui_frame(frame, &self.app_data, &self.gui_state)) .is_err() { return Err(AppError::Terminal); @@ -151,19 +155,18 @@ impl Ui { } } - if now.elapsed() >= update_duration { + if self.now.elapsed() >= update_duration { self.docker_sx .send(DockerMessage::Update) .await .unwrap_or(()); - now = Instant::now(); + self.now = Instant::now(); } } Ok(()) } - - /// Draw either the Error, or main oxker ui, to the terminal + /// Draw either the Error, or main oxker ui, to the terminal async fn draw_ui(&mut self) -> Result<(), AppError> { let status_dockerconnect = self .gui_state @@ -180,7 +183,7 @@ impl Ui { } /// Draw the main ui to a frame of the terminal -fn ui( +fn ui_frame( f: &mut Frame<'_, B>, app_data: &Arc>, gui_state: &Arc>, @@ -232,7 +235,7 @@ fn ui( vec![Constraint::Percentage(100)] }; - // Split into 3, containers+controls, logs, then graphs + // Split into 2, logs, and optional charts let lower_main = Layout::default() .direction(Direction::Vertical) .constraints(lower_split.as_ref())