GithubHelp home page GithubHelp logo

binrick / c_tuibox Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cubified/tuibox

0.0 0.0 0.0 3.99 MB

A single-header terminal UI (TUI) library, capable of creating mouse-driven, interactive applications on the command line.

C 97.99% Makefile 2.01%

c_tuibox's Introduction

tuibox

tuibox ("toybox") is a single-header terminal UI library, capable of creating mouse-driven, interactive applications on the command line.

It is completely dependency-free, using nothing other than ANSI escape sequences to handle parsing input and rendering the screen. Additionally, its individual parts work largely independently, meaning as much (or as little) of the library as is necessary can be used.

Demos

demo_basic.c: Basic button demo w/ truecolor background

  • (Note: Ugly color banding is from GIF compression)

demo_basic.gif

demo_bounce.c: Bouncing box using custom render loop

demo_bounce.gif

demo_drag.c: Click and drag

demo_drag.gif

bdfedit, a bitmap font editor built using tuibox:

demo_bdfedit.gif

colorslide, an RGBA/HSL/CMYK color picker built using tuibox:

demo_colorslide.gif

vt100utils, an ANSI graphics escape sequence encoder/decoder that can couple with tuibox for content-aware text processing:

demo_vt100utils.gif

Features

tuibox currently contains the following:

  • Event-driven terminal render loop
  • Mouse click and hover events on individual UI elements
  • Keyboard events (including escape sequence events, such as arrow keys)
  • Render caching (controlled by a user-defined dirty bit)
  • Completely dependency-free, using pure ANSI escape sequences (no ncurses)
  • Incrementally-adoptable -- rendering, events, and loops can all be used independently

Design Overview

The basic hierarchy of a UI looks like this:

  • UI: A collection of screens
    • Screen: A collection of boxes
      • Box: Any UI element
        • X/Y, width/height
        • Render function
        • Click event function (optional)
        • Hover event function (optional)
        • Arbitrary user data (see below example)
  • Main loop: Infinite read() loop gathering user input
    • Update: Parses user input and fires events (if applicable)

Example Code

A complete example is as follows:

/* Global UI struct */
ui_t u;

/* Function that runs on box click */
void click(ui_box_t *b, int x, int y){
  b->data1 = "you clicked me!";
  ui_draw(&u);
}

/* Function that runs on box hover */
void hover(ui_box_t *b, int x, int y, int down){
  b->data1 = "you hovered me!";
  ui_draw(&u);
}

void stop(){
  ui_free(&u);
  exit(0);
}

int main(){
  /* Initialize UI data structures */
  ui_new(0, &u);

  /* Add a new text box to screen 0 */
  ui_text(
    UI_CENTER_X, UI_CENTER_Y,
    "hello world!",
    0,
    click, hover,
    &u
  );

  /* Register an event on the q key */
  ui_key("q", stop, &u);

  /* Render the screen */
  ui_draw(&u);

  ui_loop(&u){
    /* Check for mouse/keyboard events */
    ui_update(&u);
  }

  return 0;
}

Compiling and Running Demos

To compile all demos in one go:

 $ make

 $ ./demo_basic
 $ ./demo_bounce
 $ ./demo_drag

Libraries/See Also

  • vec: Dynamic arrays used to store events and boxes
  • bdfedit: Bitmap font editor written alongside tuibox

c_tuibox's People

Contributors

cubified avatar

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.