Hello world
This tutorial will lead you through creating a simple “Hello World” TUI app that displays some text in the middle of the screen and waits for the user to press q to exit. It demonstrates the necessary tasks that any application developed with Ratatui needs to undertake. We assume you have a basic understanding of the terminal, and have a text editor or Rust IDE. If you don’t have a preference, VSCode makes a good default choice.
You’re going to build the following:
The full code for this tutorial is available to view at https://github.com/ratatui/ratatui-website/tree/main/code/hello-world
Install Rust
The first step is to install Rust. See the Installation section of the official Rust Book for more
information. Most people use rustup
, a command line tool for managing Rust versions and associated
tools.
Once you’ve installed Rust, verify it’s installed by running:
You should see output similar to the following (the exact version, date and commit hash will vary):
Create a New Project
Let’s create a new Rust project. In the terminal, navigate to a folder where you will store your projects and run:
The cargo new
command creates a new folder called hello-ratatui
with a basic binary application
in it. You should see:
If you examine the folders and files created this will look like:
cargo new
created a default main.rs
with a small console program which prints “Hello, world!”.
Let’s build and execute the project. Run:
You should see:
The default main.rs
program is responsible for printing the last line. We’re going to replace it
with something a little bit more exciting.
Install Ratatui
First up, you need install the Ratatui crate into your project. You will also need to install a
backend. For this tutorial, use Crossterm as the backend as it’s compatible with most operating
systems. To install the latest version of the ratatui
and crossterm
crates into the project run:
Cargo will output the following (note the exact versions may be later than the ones in this tutorial).
If you examine the Cargo.toml
file, you should see the new crates have been added to the
dependencies section:
Create a TUI Application
Let’s replace the default console application code which cargo new
created with a Ratatui
application which displays a colored message in the middle of the screen and waits for the user to
press a key to exit.
Note: a full copy of the code is available below in the Run the application section.
Add Imports
First let’s add the module imports necessary to run your application.
- From
std
import theio
module. - From
ratatui
import:- the
crossterm::event
module (and the KeyEvent and KeyEventKind types). See the Crossterm docs for more information on these types. This is re-exported from the Crossterm crate. Stylize
, an extension trait which adds style shorthands to other types.Paragraph
widget, which is used to display text.DefaultTerminal
which provides the means to output to the terminal.
- the
In your editor, open src/main.rs
and add the following at the top of the file.
Initialize the Terminal
Next, add code to the main function to setup and restore the terminal state.
Our application needs to do a few things in order to setup the terminal for use:
- First, the application enters the alternate screen, which is a secondary screen which allows your application to render whatever it needs to, without disturbing the normal output of terminal apps in your shell.
- Next, the application enables raw mode, which turns off input and output processing by the terminal. This gives your application control over when to print characters to the screen.
- The app then creates a backend and [
Terminal
] and then clears the screen.
When the application is finished it needs to restore the terminal state by leaving the alternate screen and disabling raw mode.
Replace the existing main
function with code to setup and restore the terminal:
Run a Main Loop
The main part of an application is the main loop. The application repeatedly draws the ui and then handles any events which have occurred.
Create a method named run()
and call it from the main function.
Draw to the Terminal
The Terminal::draw
method is the main interaction point an app has with Ratatui. The draw
method accepts a closure (an anonymous method) with a single Frame
parameter, and renders the
entire screen. Your application will create an area that is the full size of the terminal window and
render a new Paragraph with white foreground text and a blue background.
Add the following code to the run
method:
If you’re wondering where to find the white()
and on_blue()
methods in the Ratatui doc, these
are defined in the Stylize
extension trait as style shorthands, rather than on the Paragraph
widget.
Handle Keyboard Events
After Ratatui has drawn a frame, your application needs to check to see if any events have occurred.
These are things like keyboard presses, mouse events, resizes, etc. If the user has pressed the q
key, the app should break out of the loop.
It’s important to check that the event kind is Press
otherwise Windows terminals will see each key
twice.
Add the following code to the run
method:
Run the Application
Your application should look like:
Make sure you save the file! Now you can run the app using:
You should see a TUI app with Hello Ratatui! (press 'q' to quit)
show up in your terminal as a TUI
app.
You can press q
to exit and go back to your terminal as it was before.
Congratulations! 🎉
You have written a “hello world” terminal user interface with Ratatui. The next sections will go into more detail about how Ratatui works.
Next Steps
The next tutorial, Counter App, introduces some more interactivity, and a more robust approach to arranging your application code.