Giter Club home page Giter Club logo

widgetui's Introduction

Widgetui

Turn

Ratatui Minimal
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;
                }
            }
        }
    })
}

Into

Much Better
use ratatui::widgets::Paragraph;

use widgetui::*;

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(crossterm::event::KeyCode::Char('q')) {
        events.register_exit();
    }

    Ok(())
}

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

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 ratatui::widgets::Paragraph;

use widgetui::*;

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(crossterm::event::KeyCode::Char('q')) {
        events.register_exit();
    }

    Ok(())
}

fn main() -> Result<(), Box<dyn 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

  • I chose WidgetFrame because if I just used Widget, then you couldn't do the awesome
use widgetui::*;
use widgetui::ratatui::prelude::*;
  • It took about 10 hours to get this project initially set up!

    • Much longer after I decided to add bevy system like widget methods.
  • You used to have to take in a States struct, but in order to fix it, there is a lot of behind the scenes things going on!

  • You can only use 11 states in the same widget!

    • If you need more, please add an issue with details on why, and we may consider adding access to more at once!

widgetui's People

Contributors

albinekb avatar luqmanishere avatar theemeraldbee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

widgetui's Issues

Async support

As a bevy enjoyer myself, I've been interested in its ergonomics for a TUI library. Widgetui seems really nice and simple, but I wanted to use async code. As far as I understand, ratatui can work in async environment, but I'm not sure if this library can ? Would it be possible ?

Issue with starter code

Starter code includes a snippet that has an incorrectly closed set of parentheses:
frame.render_widget(Paragraph::new("Hello, world!", frame.size()));
Should be replaced with
frame.render_widget(Paragraph::new("Hello, world!"), frame.size());

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.