Convert Figma logo to code with AI

fdehau logotui-rs

Build terminal user interfaces and dashboards using Rust

10,830
484
10,830
0

Top Related Projects

9,716

Rust library that's all about cooking up terminal user interfaces (TUIs) 👨‍🍳🐀

4,237

A Text User Interface library for the Rust programming language

2,094

Mirror of https://gitlab.redox-os.org/redox-os/termion

Cross platform terminal library rust

Quick Overview

tui-rs is a Rust library for building rich terminal user interfaces and dashboards. It provides a high-level, declarative interface for creating complex layouts, widgets, and interactive elements in the terminal, allowing developers to create visually appealing and functional command-line applications.

Pros

  • Easy-to-use API with a declarative approach to UI design
  • Supports a wide range of widgets and layout options
  • Cross-platform compatibility (Windows, macOS, Linux)
  • Efficient rendering and low resource usage

Cons

  • Limited documentation and examples for advanced use cases
  • Steeper learning curve compared to simpler CLI libraries
  • Lack of built-in support for some advanced UI patterns (e.g., modal dialogs)
  • Occasional breaking changes between major versions

Code Examples

  1. Creating a simple app with a block and text:
use tui::{
    backend::CrosstermBackend,
    widgets::{Block, Borders, Paragraph},
    layout::{Layout, Constraint, Direction},
    Terminal,
};
use std::io;

fn main() -> Result<(), io::Error> {
    let stdout = io::stdout();
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    terminal.draw(|f| {
        let size = f.size();
        let block = Block::default()
            .title("Hello")
            .borders(Borders::ALL);
        f.render_widget(block, size);
        let text = Paragraph::new("Welcome to tui-rs!");
        f.render_widget(text, size);
    })?;

    Ok(())
}
  1. Creating a layout with multiple widgets:
use tui::{
    backend::CrosstermBackend,
    widgets::{Block, Borders, List, ListItem},
    layout::{Layout, Constraint, Direction},
    Terminal,
};
use std::io;

fn main() -> Result<(), io::Error> {
    let stdout = io::stdout();
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    terminal.draw(|f| {
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .margin(1)
            .constraints([
                Constraint::Percentage(10),
                Constraint::Percentage(80),
                Constraint::Percentage(10)
            ].as_ref())
            .split(f.size());

        let block = Block::default()
            .title("Header")
            .borders(Borders::ALL);
        f.render_widget(block, chunks[0]);

        let items = vec![
            ListItem::new("Item 1"),
            ListItem::new("Item 2"),
            ListItem::new("Item 3"),
        ];
        let list = List::new(items)
            .block(Block::default().title("List").borders(Borders::ALL));
        f.render_widget(list, chunks[1]);

        let block = Block::default()
            .title("Footer")
            .borders(Borders::ALL);
        f.render_widget(block, chunks[2]);
    })?;

    Ok(())
}
  1. Adding interactivity with events:
use tui::{
    backend::CrosstermBackend,
    widgets::{Block, Borders, Paragraph},
    layout::{Layout, Constraint, Direction},
    Terminal,
};
use std::io;
use crossterm::event::{self, Event, KeyCode};

fn main() -> Result<(), io::Error> {
    let stdout = io::stdout();
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    loop {
        terminal.draw(|f| {
            let size = f.size();
            let block = Block::default()
                .title("Press 'q' to quit")
                .borders(Borders::ALL);
            f.render_widget(block, size);
        })?;

        if let Event::Key(key)

Competitor Comparisons

9,716

Rust library that's all about cooking up terminal user interfaces (TUIs) 👨‍🍳🐀

Pros of ratatui

  • More active development and maintenance
  • Enhanced cross-platform support, including Windows
  • Improved documentation and examples

Cons of ratatui

  • Potential breaking changes due to ongoing development
  • Less established ecosystem compared to tui-rs

Code Comparison

tui-rs:

use tui::widgets::{Block, Borders};
let block = Block::default()
    .title("Block")
    .borders(Borders::ALL);

ratatui:

use ratatui::widgets::{Block, Borders};
let block = Block::default()
    .title("Block")
    .borders(Borders::ALL);

Key Differences

  • ratatui is a fork of tui-rs, aiming to continue its development
  • ratatui has a more active community and frequent updates
  • Both libraries share similar APIs, making migration relatively straightforward
  • ratatui focuses on improving cross-platform compatibility and user experience

Use Cases

  • Choose ratatui for new projects or when seeking active development and support
  • Stick with tui-rs for existing projects or if stability is a priority

Community and Support

  • ratatui has a growing community and more frequent contributions
  • tui-rs has a larger existing user base but less active development

Performance

  • Both libraries offer similar performance characteristics
  • ratatui may have slight improvements due to ongoing optimizations
4,237

A Text User Interface library for the Rust programming language

Pros of Cursive

  • More user-friendly API, making it easier for beginners to create TUIs
  • Built-in support for common widgets like dialogs, menus, and text inputs
  • Cross-platform compatibility, including Windows support

Cons of Cursive

  • Less flexible for creating custom layouts compared to tui-rs
  • Potentially higher memory usage due to its more abstracted approach
  • Slower rendering performance for complex UIs with many elements

Code Comparison

Cursive example:

siv.add_layer(
    Dialog::around(TextView::new("Hello, world!"))
        .title("My Dialog")
        .button("Quit", |s| s.quit()),
);

tui-rs example:

f.render_widget(
    Paragraph::new("Hello, world!")
        .block(Block::default().title("My Dialog").borders(Borders::ALL)),
    chunks[0],
);

Both libraries offer ways to create text-based user interfaces in Rust, but they differ in their approach. Cursive provides a higher-level abstraction with pre-built widgets, making it easier for beginners to create functional TUIs quickly. On the other hand, tui-rs offers more flexibility and control over the layout and rendering process, which can be beneficial for more complex or performance-critical applications.

2,094

Mirror of https://gitlab.redox-os.org/redox-os/termion

Pros of termion

  • Lower-level library, offering more fine-grained control over terminal operations
  • Lighter weight and potentially faster due to its minimalistic approach
  • Supports both Unix-like systems and Redox OS, providing broader compatibility

Cons of termion

  • Requires more manual implementation for complex UI elements
  • Less abstraction, which can lead to more verbose code for advanced interfaces
  • Steeper learning curve for beginners due to its low-level nature

Code Comparison

termion example:

use termion::{color, style};
println!("{}{}Colored text!{}",
         color::Fg(color::Red),
         style::Bold,
         style::Reset);

tui-rs example:

use tui::widgets::{Block, Borders};
let block = Block::default()
    .title("Block")
    .borders(Borders::ALL);
f.render_widget(block, chunks[0]);

termion focuses on direct terminal manipulation, while tui-rs provides higher-level abstractions for creating user interfaces. termion is better suited for simpler terminal applications or when fine-grained control is needed, whereas tui-rs excels in creating more complex, interactive terminal user interfaces with less code.

Cross platform terminal library rust

Pros of crossterm

  • Platform-independent terminal manipulation library
  • Supports a wide range of terminal operations (cursor movement, colors, input handling)
  • Lightweight and focused on core terminal functionality

Cons of crossterm

  • Lacks high-level UI components and widgets
  • Requires more manual work to create complex user interfaces
  • Less suitable for creating full-fledged TUI applications out-of-the-box

Code comparison

crossterm:

use crossterm::{
    execute,
    style::{Color, Print, SetForegroundColor},
};

fn main() -> crossterm::Result<()> {
    execute!(
        stdout(),
        SetForegroundColor(Color::Blue),
        Print("Hello, World!")
    )
}

tui-rs:

use tui::{
    backend::CrosstermBackend,
    widgets::{Block, Borders},
    Terminal,
};

fn main() -> Result<(), io::Error> {
    let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
    terminal.draw(|f| {
        let block = Block::default().title("Block").borders(Borders::ALL);
        f.render_widget(block, f.size());
    })?;
    Ok(())
}

crossterm focuses on low-level terminal operations, while tui-rs provides higher-level abstractions for building text-based user interfaces. crossterm is more flexible but requires more manual work, whereas tui-rs offers pre-built widgets and layout management for quicker TUI development.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

tui-rs

⚠️ August 2023: This crate is no longer maintained. See https://github.com/ratatui-org/ratatui for an actively maintained fork. ⚠️

Build Status Crate Status Docs Status

Demo cast under Linux Termite with Inconsolata font 12pt

tui-rs is a Rust library to build rich terminal user interfaces and dashboards. It is heavily inspired by the Javascript library blessed-contrib and the Go library termui.

The library supports multiple backends:

The library is based on the principle of immediate rendering with intermediate buffers. This means that at each new frame you should build all widgets that are supposed to be part of the UI. While providing a great flexibility for rich and interactive UI, this may introduce overhead for highly dynamic content. So, the implementation try to minimize the number of ansi escapes sequences generated to draw the updated UI. In practice, given the speed of Rust the overhead rather comes from the terminal emulator than the library itself.

Moreover, the library does not provide any input handling nor any event system and you may rely on the previously cited libraries to achieve such features.

I'm actively looking for help maintaining this crate. See this issue

Rust version requirements

Since version 0.17.0, tui requires rustc version 1.56.1 or greater.

Documentation

Demo

The demo shown in the gif can be run with all available backends.

# crossterm
cargo run --example demo --release -- --tick-rate 200
# termion
cargo run --example demo --no-default-features --features=termion --release -- --tick-rate 200

where tick-rate is the UI refresh rate in ms.

The UI code is in examples/demo/ui.rs while the application state is in examples/demo/app.rs.

If the user interface contains glyphs that are not displayed correctly by your terminal, you may want to run the demo without those symbols:

cargo run --example demo --release -- --tick-rate 200 --enhanced-graphics false

Widgets

The library comes with the following list of widgets:

Click on each item to see the source of the example. Run the examples with with cargo (e.g. to run the gauge example cargo run --example gauge), and quit by pressing q.

You can run all examples by running cargo make run-examples (require cargo-make that can be installed with cargo install cargo-make).

Third-party widgets

Apps using tui

Alternatives

You might want to checkout Cursive for an alternative solution to build text user interfaces in Rust.

License

MIT