pub async fn run_app(app: &mut App) -> Result<()>Expand description
Runs the application in a terminal-based user interface with an event loop.
This function initializes a terminal using the Crossterm backend, clears its
contents, and enters an event loop. In this loop, it draws the application’s
current state to the terminal and waits for user input events. When a key
event occurs, it processes the event and checks if the application should
exit. The loop exits when the handle_key function signals to terminate.
§Arguments
app- A mutable reference to the application’s state, which is used and updated throughout the event loop.
§Returns
Result<()>- ReturnsOk(())if the application runs and exits successfully. Returns an error if any issues are encountered during initialization, drawing, or event handling.
§Errors
This function can return an error in the following situations:
- If the terminal backend cannot be initialized.
- If the terminal fails to clear its contents or render the UI.
- If there is an error reading input events or processing them.
§Notes
- The terminal clearing and rendering are achieved using the
TerminalandCrosstermBackendfrom thetui(Terminal User Interface) library. - The event loop uses a polling mechanism to periodically check for input
using
event::poll, blocking for 250 milliseconds before timing out. - Pressing specific keys, as determined by the
handle_keyfunction, can terminate the event loop.