In this section, we will walk through the process of refactoring the application to set ourselves up
better for bigger projects. Not all of these changes are ratatui specific, and are generally good
coding practices to follow.
We are still going to keep everything in one file for this section, but we are going to split the
previous functionality into separate functions.
The first thing you might consider doing is reorganizing imports with qualified names.
Typedefs and Type Aliases
By defining custom types and aliases, we can simplify our code and make it more expressive.
By defining an App struct, we can encapsulate our application state and make it more structured.
counter holds the current value of our counter.
should_quit is a flag that indicates whether the application should exit its main loop.
Breaking up main()
We can extract significant parts of the main() function into separate smaller functions, e.g.
startup(), shutdown(), ui(), update(), run().
startup() is responsible for initializing the terminal.
shutdown() cleans up the terminal.
ui() handles rendering of our application state.
update() processes user input and updates our application state.
run() contains our main application loop.
Each function now has a specific task, making our main application logic more organized and easier
By making our code more organized, modular, and readable, we not only make it easier for others to
understand and work with but also set the stage for future enhancements and extensions.
Here’s the full code for reference:
Here’s a flow chart representation of the various steps in the program: