Coder Social home page Coder Social logo

tuich's Introduction

TUICH

A simple rust library to simplify TUIs development!

This library was heavily inspired by ratatui.rs!

Warning

This lib is unstable, use it at your own risk!

Installation

For now you can only install TUICH via git:

[dependencies]
tuich = { git = "https://github.com/bbogdan-ov/tuich" }

I think TUICH isn't ready to be uploaded to crates.io yet

Simple example

Here is the simple example app using the crossterm backend

use std::io;
use tuich::{
    backend::{
        crossterm::CrosstermBackend,
        BackendEvent,
        BackendEventReader
    },
    event::{Event, Key, KeyCode, KeyMod},
    style::{Color, Stylized},
    terminal::Terminal,
    widget::{Draw, Borders, Paragraph}
};

type Term = Terminal<CrosstermBackend<io::Stdout>>;

fn main() -> io::Result<()> {
    // Create and run a new terminal in "classic mode" with crossterm backend
    // Classic mode just hides the cursor, enters alternate screen and raw mode
    let mut term: Term = Terminal::classic(CrosstermBackend::default())?;
    // Create an event reader, so we can pass it into another thread in the future
    let mut event_reader = term.event_reader();

    let mut number: isize = 0;

    // Draw the UI for the first time
    draw_ui(&mut term, &number)?;

    loop {
        match event_reader.read_events()? {
            Event::Key(key, _key_code) => match key {
                // Exit after pressing on 'q'
                Key(_, KeyCode::Char('q')) => break,
                // Increase the number when Ctrl + Right was pressed
                Key(KeyMod::Ctrl, KeyCode::Right) =>
                    number += 1,
                // Decrease the number when Ctrl + Left was pressed
                Key(KeyMod::Ctrl, KeyCode::Left) =>
                    number -= 1,
                _ => ()
            },
            _ => ()
        }

        // Draw UI in the loop
        draw_ui(&mut term, &number)?;
    }

    Ok(())
}

fn draw_ui(term: &mut Term, number: &isize) -> io::Result<()> {
    let rect = term.rect();
    let buf = &mut term.buffer;

    // Clear the buffer before every draw
    buf.clear();

    // Draw borders with magenta border, green foreground fill, width of screen width and height of 3
    let borders_rect = Borders::single()
        .style(Color::Magenta)
        .fill((" ", Color::Green))
        .draw(buf, rect.with_height(3));

    // Draw text "inside" the borders
    Paragraph::new([
        // Create a span with a red foreground, gray background and italic modifier
        "Hello!"
            .red()
            .on_gray()
            .italic(),
        // Create a plain span
        // Its color/style will depend on the cell on which it is placed
        // In this situation foreground color will be green because of the borders' fill color
        format!(" The number is > {} <", number).into()
    ])
        .draw(buf, borders_rect.margin(1));

    // Display the buffer on the terminal screen
    term.draw()?;

    Ok(())
}

License

MIT license
Do whatever you want with this project!

@ bogdanov 2024

tuich's People

Contributors

bbogdan-ov avatar libsod avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

libsod

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.