Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion tui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ use ratatui::{
},
Terminal,
};
use running_command::TERMINAL_UPDATED;
use state::AppState;
use std::{
io::{stdout, Result, Stdout},
path::PathBuf,
sync::atomic::Ordering,
time::Duration,
};

Expand Down Expand Up @@ -79,9 +81,14 @@ fn main() -> Result<()> {

fn run(terminal: &mut Terminal<CrosstermBackend<Stdout>>, 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;
}

Expand All @@ -104,5 +111,6 @@ fn run(terminal: &mut Terminal<CrosstermBackend<Stdout>>, state: &mut AppState)
}
_ => {}
}
terminal.draw(|frame| state.draw(frame)).unwrap();
}
}
9 changes: 8 additions & 1 deletion tui/src/running_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -158,6 +161,7 @@ impl FloatContent for RunningCommand {
}
}
}
pub static TERMINAL_UPDATED: AtomicBool = AtomicBool::new(true);

impl RunningCommand {
pub fn new(commands: &[&Command]) -> Self {
Expand Down Expand Up @@ -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<Mutex<Vec<u8>>> = 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();
Expand All @@ -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);
})
};

Expand Down