diff --git a/clock-tui/src/bin/main.rs b/clock-tui/src/bin/main.rs index d605093..89b0ae6 100644 --- a/clock-tui/src/bin/main.rs +++ b/clock-tui/src/bin/main.rs @@ -30,29 +30,44 @@ fn run_app( return Ok(()); } + // draw the TUI using the `tui` library terminal.draw(|f| app.ui(f))?; let timeout = tick_rate - .checked_sub(last_tick.elapsed()) - .unwrap_or(Duration::ZERO); + .checked_sub(last_tick.elapsed()) // substraction, is None if would underflow (be negative) + .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)? { + // we only care about pressed keys if let Event::Key(key) = event::read()? { match key.code { + // exit the application if the user presses the 'q' key KeyCode::Char('q') => { return Ok(()); } + // otherwise, let the app handle it the key that was pressed 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 so, we want to substract with NOW when we calculate the new timeout. last_tick = Instant::now(); } } } +// entry point into the program fn main() -> Result<(), Box> { + // Parse the cli arguments let mut app = App::parse(); app.init_app();