Function run_app

Source
pub fn run_app<W: Widget, E: Error, FW: FnMut(Event) -> Result<Option<W>, E>>(
    func: FW,
) -> Result<()>
Expand description

Runs a tuviv app

This sets up all the terminal

  • It takes a function (or closure) that:

    • Takes a crossterm::event::Event as input
    • Returns:
      • Ok(Some(W)) on sucess - thus rendering a widget
      • Ok(None) for a graceful closing of the application
      • Err(e) for an error - which is printed to the stderr by tuviv
  • The generics are:

    • W: the widget this returns
    • E: the error type. Use Infallible if FW doesn’t fail
    • FW: the funtion
  • A simple example function would be:

run_app(|event| {
   // Allows for the app to be quit
   if let Event::Key(key) = event {
       if let KeyCode::Char('q') = key.code {
           return Ok(None);
       }
   }
   // Create a widget
   let w = Paragraph::label("Press q to to quit").centered();

   // This app never fails, so we return Infallible (`!`)
   Ok::<_, Infallible>(w)
})