ratatui
A Rust crate for cooking up terminal user interfaces (TUIs) 👨🍳🐀 https://ratatui.rs
Top Related Projects
Quick Overview
Ratatui is a modern, fast, and flexible terminal user interface (TUI) library for the Rust programming language. It provides a set of widgets and tools to build interactive command-line applications with a rich and responsive user interface.
Pros
- Flexibility: Ratatui offers a wide range of customizable widgets and layouts, allowing developers to create unique and visually appealing terminal applications.
- Performance: The library is built on top of the Crossterm terminal library, providing efficient and responsive rendering.
- Active Development: The project has an active community and is regularly maintained, with new features and improvements being added.
- Rust-based: As a Rust-based library, Ratatui benefits from the language's safety, performance, and concurrency features, making it a robust choice for building terminal applications.
Cons
- Learning Curve: Ratatui has a steeper learning curve compared to some other TUI libraries, especially for developers new to Rust.
- Limited Ecosystem: While the library is actively developed, the ecosystem of third-party widgets and plugins is not as extensive as some other TUI libraries.
- Dependency on Crossterm: Ratatui relies on the Crossterm library, which may not provide the same level of cross-platform support as some other terminal libraries.
- Lack of Documentation: The project's documentation, while improving, could still be more comprehensive and user-friendly for newcomers.
Code Examples
use ratatui::{
widgets::{Block, Borders, Paragraph, Wrap},
layout::{Constraint, Direction, Layout},
style::{Color, Modifier, Style},
text::{Span, Spans},
widgets::canvas::{Canvas, Line, Map, MapResolution, Rectangle},
Frame, Terminal,
};
fn main() {
let mut terminal = Terminal::new(CrosstermBackend::new()).unwrap();
terminal.clear().unwrap();
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(terminal.size());
let paragraph = Paragraph::new(Spans::from(vec![
Span::styled("This is a simple Ratatui example", Style::default().fg(Color::White)),
Span::raw("\n\nPress Ctrl+C to exit."),
]))
.block(Block::default().borders(Borders::ALL).title("Ratatui Example"))
.wrap(Wrap { trim: true });
let canvas = Canvas::default()
.paint(|ctx| {
ctx.draw(&Line {
x1: 0,
y1: 0,
x2: ctx.size().width,
y2: ctx.size().height,
color: Color::Rgb(255, 0, 0),
});
ctx.draw(&Rectangle {
x: 5,
y: 5,
width: 10,
height: 10,
color: Color::Rgb(0, 255, 0),
});
})
.block(Block::default().borders(Borders::ALL).title("Canvas"));
terminal.draw(|f| {
f.render_widget(paragraph, chunks[0]);
f.render_widget(canvas, chunks[1]);
}).unwrap();
}
This example demonstrates the usage of the Paragraph
and Canvas
widgets in Ratatui, creating a simple terminal application with a text block and a canvas for drawing shapes.
use ratatui::{
widgets::{Block, Borders, List, ListItem},
layout::{Constraint, Direction, Layout},
style::{Color, Modifier, Style},
text::{Span, Spans},
Frame, Terminal,
};
fn main() {
let mut terminal = Terminal::new(CrosstermBackend::new()).unwrap();
terminal.clear().unwrap();
let chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(30), Constraint::
Competitor Comparisons
Rust library to create a Good Game Easily
Pros of ggez/ggez
- ggez provides a more comprehensive set of features and functionality for game development, including support for 2D graphics, audio, input handling, and more.
- The ggez project has a larger and more active community, with more contributors and a wider range of available resources and documentation.
- ggez is designed to be more cross-platform, with support for Windows, macOS, and Linux, whereas Ratatui is primarily focused on the Linux platform.
Cons of ggez/ggez
- Ratatui is built on top of the Rust programming language, which may be more appealing to developers who prefer a more modern and type-safe language compared to the C-based approach of ggez.
- Ratatui's focus on the Linux platform may make it a better choice for developers who are primarily targeting that platform, as it may provide a more streamlined and optimized experience.
- The Ratatui project is generally more lightweight and minimalistic compared to the more feature-rich ggez, which may be a pro or a con depending on the specific needs of the project.
Code Comparison
Here's a brief comparison of the code for creating a simple window in both ggez and Ratatui:
ggez:
use ggez::event;
use ggez::graphics;
use ggez::Context;
use ggez::ContextBuilder;
fn main() {
let (mut ctx, event_loop) = ContextBuilder::new("my_game", "ggez")
.build()
.expect("Failed to build ggez context");
event::run(&mut ctx, event_loop, || {
graphics::clear(&mut ctx, graphics::Color::WHITE);
graphics::present(&mut ctx)
});
}
Ratatui:
use ratatui::backend::CrosstermBackend;
use ratatui::Terminal;
fn main() {
let stdout = std::io::stdout();
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend).unwrap();
terminal.draw(|f| {}).unwrap();
}
Data-oriented and data-driven game engine written in Rust
Pros of Amethyst
- Amethyst is a more mature and feature-rich game engine, with a larger community and more extensive documentation.
- Amethyst provides a more comprehensive set of tools and utilities for game development, including a built-in asset management system and networking capabilities.
- Amethyst has a more modular and flexible architecture, allowing for easier customization and extension of the engine.
Cons of Amethyst
- Amethyst has a steeper learning curve compared to Ratatui, especially for developers new to game development.
- Amethyst may be overkill for smaller or simpler projects, as it can be more complex and resource-intensive than necessary.
- Amethyst's focus on 3D game development may not be as well-suited for 2D or casual game projects.
Code Comparison
Ratatui:
fn main() {
let mut app = App::new();
app.run();
}
Amethyst:
fn main() {
let mut game = MyGame::default();
let mut game_data = GameDataBuilder::default()
.with_bundle(TransformBundle::default())?
.with_bundle(RenderBundle::new(Pipeline::build()))?;
let mut game_dispatcher = DispatcherBuilder::new()
.with_thread_local(RenderSystem::new())
.build();
game.run(game_data, game_dispatcher);
}
A refreshingly simple data-driven game engine built in Rust
Pros of Bevy
- Bevy is a modern, data-oriented, and highly performant game engine, making it a great choice for building complex and efficient games.
- The engine has a strong focus on ECS (Entity-Component-System) architecture, which promotes modularity and scalability.
- Bevy has a growing community and a wide range of plugins and resources available, making it easier to extend and customize.
Cons of Bevy
- Bevy has a steeper learning curve compared to Ratatui, especially for developers new to the ECS paradigm.
- The engine is still relatively young and may not have the same level of maturity and stability as some other game engines.
- The documentation, while improving, may not be as comprehensive as some developers would prefer.
Code Comparison
Bevy (main.rs):
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_system(player_movement_system)
.run();
}
fn player_movement_system(
mut player_query: Query<&mut Transform, With<Player>>,
keyboard_input: Res<Input<KeyCode>>,
) {
if let Ok(mut transform) = player_query.get_single_mut() {
let mut direction = Vec3::ZERO;
if keyboard_input.pressed(KeyCode::W) {
direction += Vec3::Y;
}
// ...
transform.translation += direction * PLAYER_SPEED * TIME_STEP;
}
}
Ratatui (main.rs):
fn main() {
let app = App::new()
.with_theme(Theme::default())
.with_state(AppState::default())
.with_event_handler(event_handler)
.run();
}
fn event_handler(state: &mut AppState, event: Event) {
match event {
Event::Input(input) => match input {
InputEvent::Key(key_event) => {
if key_event.code == KeyCode::Char('q') {
state.should_quit = true;
}
}
// ...
},
// ...
}
}
A simple and easy-to-use library to enjoy videogames programming
Pros of raylib
- Lightweight and cross-platform: raylib is a small, self-contained library that can be easily integrated into a wide range of projects, making it a versatile choice for game development.
- Extensive documentation and community support: raylib has comprehensive documentation and a thriving community, providing developers with ample resources and support.
- Supports multiple programming languages: raylib can be used with a variety of programming languages, including C, C++, D, Nim, Rust, and more, making it accessible to a broad range of developers.
Cons of raylib
- Limited GUI support: While raylib provides basic GUI functionality, it may not be as feature-rich or customizable as dedicated GUI libraries like ratatui.
- Steeper learning curve: Compared to ratatui, which is built on top of a well-established library (Crossterm), raylib may have a slightly steeper learning curve for developers who are new to game development.
Code Comparison
Here's a brief code comparison between raylib and ratatui:
raylib (C):
#include "raylib.h"
int main() {
InitWindow(800, 600, "raylib");
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Hello, raylib!", 320, 240, 20, LIGHTGRAY);
EndDrawing();
}
CloseWindow();
return 0;
}
ratatui (Rust):
use ratatui::{
backend::CrosstermBackend,
widgets::{Block, Borders, Paragraph, Text},
layout::{Constraint, Direction, Layout},
Terminal,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let backend = CrosstermBackend::new();
let mut terminal = Terminal::new(backend)?;
terminal.draw(|f| {
let size = f.size();
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(size);
let block = Block::default().title("ratatui").borders(Borders::ALL);
f.render_widget(block, chunks[0]);
f.render_widget(Paragraph::new(Text::raw("Hello, ratatui!")), chunks[1]);
})?;
Ok(())
}
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Table of Contents
Ratatui Website · Docs · Widget Examples · App Examples · Changelog
Breaking Changes · Contributing · Report a bug · Request a Feature
Ratatui (Ëræ.tÉËtu.i) is a Rust crate for cooking up terminal user interfaces (TUIs). It provides a simple and flexible way to create text-based user interfaces in the terminal, which can be used for command-line applications, dashboards, and other interactive console programs.
Quickstart
Ratatui has templates available to help you get started quickly. You can use the
cargo-generate
command to create a new project with Ratatui:
cargo install --locked cargo-generate
cargo generate ratatui/templates
Selecting the Hello World template produces the following application:
use color_eyre::Result;
use crossterm::event::{self, Event};
use ratatui::{DefaultTerminal, Frame};
fn main() -> Result<()> {
color_eyre::install()?;
let terminal = ratatui::init();
let result = run(terminal);
ratatui::restore();
result
}
fn run(mut terminal: DefaultTerminal) -> Result<()> {
loop {
terminal.draw(render)?;
if matches!(event::read()?, Event::Key(_)) {
break Ok(());
}
}
}
fn render(frame: &mut Frame) {
frame.render_widget("hello world", frame.area());
}
Documentation
- Docs - the full API documentation for the library on docs.rs.
- Ratatui Website - explains the library's concepts and provides step-by-step tutorials.
- Ratatui Forum - a place to ask questions and discuss the library.
- Widget Examples - a collection of examples that demonstrate how to use the library.
- App Examples - a collection of more complex examples that demonstrate how to build apps.
- ARCHITECTURE.md - explains the crate organization and modular workspace structure.
- Changelog - generated by git-cliff utilizing Conventional Commits.
- Breaking Changes - a list of breaking changes in the library.
You can also watch the EuroRust 2024 talk to learn about common concepts in Ratatui and what's possible to build with it.
Templates
If you're looking to get started quickly, you can use one of the available templates from the
templates repository using cargo-generate
:
cargo generate ratatui/templates
Built with Ratatui
Check out the showcase section of the website, or the awesome-ratatui repository for a curated list of awesome apps and libraries built with Ratatui!
Alternatives
Contributing
Feel free to join our Discord server for discussions and questions! There is also a Matrix bridge available at #ratatui:matrix.org. We have also recently launched the Ratatui Forum.
We rely on GitHub for bugs and feature requests.
Please make sure you read the contributing guidelines before creating a pull request.
If you'd like to show your support, you can add the Ratatui badge to your project's README:
[](https://ratatui.rs/)
Acknowledgements
Ratatui was forked from the tui-rs crate in 2023 in order to continue its development. None of this could be possible without Florian Dehau who originally created tui-rs which inspired many Rust TUIs.
Special thanks to Pavel Fomchenkov for his work in designing an awesome logo for the Ratatui project and organization.
License
This project is licensed under the MIT License.
Top Related Projects
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot