main.rs

In this section, let’s just cover the contents of main.rs, build.rs and utils.rs.

The main.rs file is the entry point of the application. Here’s the complete main.rs file:

pub mod action;
pub mod app;
pub mod cli;
pub mod components;
pub mod config;
pub mod tui;
pub mod utils;

use clap::Parser;
use cli::Cli;
use color_eyre::eyre::Result;

use crate::{
  app::App,
  utils::{initialize_logging, initialize_panic_handler, version},
};

async fn tokio_main() -> Result<()> {
  initialize_logging()?;

  initialize_panic_handler()?;

  let args = Cli::parse();
  let mut app = App::new(args.tick_rate, args.frame_rate)?;
  app.run().await?;

  Ok(())
}

#[tokio::main]
async fn main() -> Result<()> {
  if let Err(e) = tokio_main().await {
    eprintln!("{} error: Something went wrong", env!("CARGO_PKG_NAME"));
    Err(e)
  } else {
    Ok(())
  }
}

In essence, the main function creates an instance of App and calls App.run(), which runs the “handle event -> update state -> draw” loop. We will talk more about this in a later section.

This main.rs file incorporates some key features that are not necessarily related to ratatui, but in my opinion, essential for any Terminal User Interface (TUI) program:

  • Command Line Argument Parsing (clap)
  • XDG Base Directory Specification
  • Logging
  • Panic Handler

These are described in more detail in the utils.rs section.