Skip to content

Commit

Permalink
Efficient rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
JustLinuxUser committed Feb 2, 2025
1 parent a6ccaf4 commit 04b4977
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
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

0 comments on commit 04b4977

Please sign in to comment.