diff --git a/tui/src/main.rs b/tui/src/main.rs index 11ace649..729b9c39 100644 --- a/tui/src/main.rs +++ b/tui/src/main.rs @@ -24,9 +24,11 @@ use ratatui::{ }, Terminal, }; +use running_command::TERMINAL_UPDATED; use state::AppState; use std::{ io::{stdout, Result, Stdout}, + sync::atomic::Ordering, time::Duration, }; @@ -59,9 +61,14 @@ fn main() -> Result<()> { fn run(terminal: &mut Terminal>, state: &mut AppState) -> Result<()> { loop { - terminal.draw(|frame| state.draw(frame)).unwrap(); // Wait for an event if !event::poll(Duration::from_millis(10))? { + if TERMINAL_UPDATED + .compare_exchange(true, false, Ordering::AcqRel, Ordering::Acquire) + .is_ok() + { + terminal.draw(|frame| state.draw(frame)).unwrap(); + } continue; } @@ -84,5 +91,6 @@ fn run(terminal: &mut Terminal>, state: &mut AppState) } _ => {} } + terminal.draw(|frame| state.draw(frame)).unwrap(); } } diff --git a/tui/src/running_command.rs b/tui/src/running_command.rs index 378eec4d..19d3d039 100644 --- a/tui/src/running_command.rs +++ b/tui/src/running_command.rs @@ -13,7 +13,10 @@ use ratatui::{ use std::{ fs::File, io::{Result, Write}, - sync::{Arc, Mutex}, + sync::{ + atomic::{AtomicBool, Ordering}, + Arc, Mutex, + }, thread::JoinHandle, }; use time::{macros::format_description, OffsetDateTime}; @@ -158,6 +161,7 @@ impl FloatContent for RunningCommand { } } } +pub static TERMINAL_UPDATED: AtomicBool = AtomicBool::new(true); impl RunningCommand { pub fn new(commands: &[&Command]) -> Self { @@ -217,6 +221,7 @@ impl RunningCommand { // A buffer, shared between the thread that reads the command output, and the main tread. // The main thread only reads the contents let command_buffer: Arc>> = Arc::new(Mutex::new(Vec::new())); + TERMINAL_UPDATED.store(true, Ordering::Release); let reader_handle = { // Arc is just a reference, so we can create an owned copy without any problem let command_buffer = command_buffer.clone(); @@ -233,8 +238,10 @@ impl RunningCommand { // done, to minimise the time it is opened let command_buffer = mutex.as_mut().unwrap(); command_buffer.extend_from_slice(&buf[0..size]); + TERMINAL_UPDATED.store(true, Ordering::Release); // The mutex is closed here automatically } + TERMINAL_UPDATED.store(true, Ordering::Release); }) };