document main.rs

This commit is contained in:
Christoph J. Scherr 2024-03-04 15:59:33 +01:00
parent f616555fa2
commit f20fe5b3bf
Signed by: cscherrNT
GPG key ID: 8E2B45BC51A27EA7

View file

@ -30,29 +30,44 @@ fn run_app<B: Backend>(
return Ok(()); return Ok(());
} }
// draw the TUI using the `tui` library
terminal.draw(|f| app.ui(f))?; terminal.draw(|f| app.ui(f))?;
let timeout = tick_rate let timeout = tick_rate
.checked_sub(last_tick.elapsed()) .checked_sub(last_tick.elapsed()) // substraction, is None if would underflow (be negative)
.unwrap_or(Duration::ZERO); .unwrap_or(Duration::ZERO); // if it was `None`, it was longer than `tick_rate` since the
// `last_tick`. We set the timeout to `DURATION::ZERO` so
// that `event::poll` will return instantly with success
// wait up to 100 ms for the timeout Duration. This ensures that we do not loop very fast,
// which would cause high cpu load. If the Duration is less than 100 ms, we enter the if
// clause. An event describes some kind of user interaction: Mouse actions, Key Presses, or
// Resizing of the Terminal
if event::poll(timeout)? { if event::poll(timeout)? {
// we only care about pressed keys
if let Event::Key(key) = event::read()? { if let Event::Key(key) = event::read()? {
match key.code { match key.code {
// exit the application if the user presses the 'q' key
KeyCode::Char('q') => { KeyCode::Char('q') => {
return Ok(()); return Ok(());
} }
// otherwise, let the app handle it the key that was pressed
key => app.on_key(key), key => app.on_key(key),
} }
} }
} }
// has it been longer since the last tick than the duration of the `tick_rate`?
if last_tick.elapsed() >= tick_rate { if last_tick.elapsed() >= tick_rate {
// if so, we want to substract with NOW when we calculate the new timeout.
last_tick = Instant::now(); last_tick = Instant::now();
} }
} }
} }
// entry point into the program
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
// Parse the cli arguments
let mut app = App::parse(); let mut app = App::parse();
app.init_app(); app.init_app();