Multiple Files

At the moment, we have everything in just one file. However, this can be impractical if we want to expand our app further.

Let’s start by creating a number of different files to represent the various concepts we covered in the previous section:

$ tree .
├── Cargo.toml
├── LICENSE
└── src
   ├── app.rs
   ├── event.rs
   ├── main.rs
   ├── tui.rs
   ├── ui.rs
   └── update.rs

If you want to explore the code on your own, you can check out the completed source code here: https://github.com/ratatui-org/ratatui-book/tree/main/code/ratatui-counter-app

Let’s go ahead and declare these files as modules in src/main.rs

/// Application.
pub mod app;

/// Terminal events handler.
pub mod event;

/// Widget renderer.
pub mod ui;

/// Terminal user interface.
pub mod tui;

/// Application updater.
pub mod update;

We are going to use anyhow in this section of the tutorial.

cargo add anyhow

Tip

Instead of anyhow you can also use eyre or color-eyre.

- use anyhow::Result;
+ use color_eyre::eyre::Result;

You’ll need to add color-eyre and remove anyhow:

cargo remove anyhow
cargo add color-eyre

If you are using color_eyre, you’ll also want to add color_eyre::install()? to the beginning of your main() function:

use color_eyre::eyre::Result;

fn main() -> Result<()> {
    color_eyre::install()?;
    // ...
    Ok(())
}

color_eyre is an error report handler for colorful, consistent, and well formatted error reports for all kinds of errors. Check out the section for setting up panic hooks with color-eyre.

Now we are ready to start refactoring our app.