Home

Awesome

Widgetui

Turn

<details> <summary>Ratatui Minimal</summary>
fn main() -> Result<(), Box<dyn Error>> {
    let mut terminal = setup_terminal()?;
    run(&mut terminal)?;
    restore_terminal(&mut terminal)?;
    Ok(())
}

fn setup_terminal() -> Result<Terminal<CrosstermBackend<Stdout>>, Box<dyn Error>> {
    let mut stdout = io::stdout();
    enable_raw_mode()?;
    execute!(stdout, EnterAlternateScreen)?;
    Ok(Terminal::new(CrosstermBackend::new(stdout))?)
}

fn restore_terminal(
    terminal: &mut Terminal<CrosstermBackend<Stdout>>,
) -> Result<(), Box<dyn Error>> {
    disable_raw_mode()?;
    execute!(terminal.backend_mut(), LeaveAlternateScreen,)?;
    Ok(terminal.show_cursor()?)
}

fn run(terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<(), Box<dyn Error>> {
    Ok(loop {
        terminal.draw(|frame| {
            let greeting = Paragraph::new("Hello World!");
            frame.render_widget(greeting, frame.size());
        })?;
        if event::poll(Duration::from_millis(250))? {
            if let Event::Key(key) = event::read()? {
                if KeyCode::Char('q') == key.code {
                    break;
                }
            }
        }
    })
}
</details>

Into

<details> <summary>Much Better</summary>
use crossterm::event::KeyCode;
use ratatui::widgets::Paragraph;
use widgetui::*;

use std::error::Error;

fn widget(mut frame: ResMut<WidgetFrame>, mut events: ResMut<Events>) -> WidgetResult {
    let size = frame.size();
    frame.render_widget(Paragraph::new("Hello, world!"), size);

    if events.key(KeyCode::Char('q')) {
        events.register_exit();
    }

    Ok(())
}

fn main() -> Result<(), Box<dyn Error>> {
    Ok(App::new(100)?.widgets(widget).run()?)
}
</details>

The goal of this project is to simplify the requirements to make a good project using tui. It removes boilerplate, and improves the developer experience by using the power of typemaps, and dependency injection

Installation

Run the following within your project directory

cargo add widgetui

Introduction

Widgetui is a wrapper over Ratatui's Crossterm backend which allows for powerful abstraction, and simplifies creating a good app within Ratatui.

Why pick this over Ratatui?

Widgetui isn't meant to replace or undermine Ratatui. It is simply a wrapper. Without Ratatui, this crate would not exist, as well, you will still require Ratatui and Crossterm crates just to work with the apps.

TLDR; Don't, use both together to improve developer experience, and build your apps faster!

Quickstart

use crossterm::event::KeyCode;
use ratatui::widgets::Paragraph;
use widgetui::*;

use std::error::Error;

fn widget(mut frame: ResMut<WidgetFrame>, mut events: ResMut<Events>) -> WidgetResult {
    let size = frame.size();
    frame.render_widget(Paragraph::new("Hello, world!"), size);

    if events.key(KeyCode::Char('q')) {
        events.register_exit();
    }

    Ok(())
}

fn main() -> Result<(), impl Error> {
    App::new(100)?.widgets(widget).run()
}

The above will create an application that will display an empty terminal window, then close once you press q.

This application, with many less lines, will render the same thing that Ratatui's Quickstart renders.

Documentation

Documentation can be found on docs.rs. Need help? Check the wiki!

Fun Facts

use widgetui::*;
use widgetui::ratatui::prelude::*;