Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

9 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tiny-tui

A lightweight, zero-dependency, component-based Terminal User Interface (TUI) framework for Node.js. Engineered to work seamlessly in both modern terminal emulators and raw Linux virtual consoles (TERM=linux / TTY).

Features

  • Zero Dependencies: Built purely on standard Node.js modules (node:readline).
  • Flicker-Free Rendering: Full-frame buffering, line-end clearing, and batched re-renders via setImmediate.
  • Flexbox Layout Engine: Responsive Flex, Row, and Column containers supporting flex ratios, fixed sizes, auto-sizing, and gap.
  • TTY-Safe ANSI Styling: Chainable style builder (ANSI.bold.bgBlue.brightWhite(...)) compatible with the 16-color VGA TTY palette, including nested background preservation.
  • Page Routing & Input Isolation: Page and PageManager components that isolate keyboard events and local hotkeys to the active view.

Installation

Clone the repository or install it locally in your project:

npm i tiny-tui

Quick Start

import { App, Component, ANSI, Flex, Box, SelectList } from 'tiny-tui';

class MainScreen extends Component {
    constructor(onExit) {
        super();
        this.state = { status: 'Ready' };

        // Mount child components to automatically propagate state updates and key events
        this.menu = this.mount(new SelectList({
            items: ['Run Task', 'Check Status', 'Exit'],
            onSelect: (item) => {
                if (item === 'Exit') return onExit();
                this.setState({ status: `Executed: ${ANSI.green(item)}` });
            },
        }));
    }

    render(width, height) {
        return new Flex({
            direction: 'column',
            width,
            height,
            children: [
                new Box({
                    title: ANSI.bold.inverse.brightYellow(' SYSTEM MONITOR '),
                    children: [`Status: ${this.state.status}`],
                }),
                new Box({
                    title: 'ACTIONS',
                    flex: 1,
                    children: [this.menu],
                }),
            ],
        }).render(width, height);
    }
}

const app = new App(new MainScreen(() => app.exit()));
app.start();

Showcase / Built with tiny-tui

  • Dual-Pane Terminal File Manager — A Midnight Commander–style dual-pane file manager written using tiny-tui, featuring directory browsing, sorting, and file operations (copy, move, delete).

Core API & Components

App & Component

  • new App(rootComponent): Initializes alternate screen buffer, hides cursor, enables raw TTY input, handles terminal resize events, and restores terminal state cleanly on exit or crash.
  • this.mount(child): Registers a child component so its setState() triggers a screen re-render and keyboard events bubble down automatically.
  • this.setState(partialState): Merges new state and schedules a batched UI update.

Layout (Flex, Row, Column)

Distributes available terminal space across child elements:

  • direction: 'row' | 'column' (shorthands Row and Column are also exported).
  • flex: Proportional weight for remaining space (e.g., flex: 1, flex: 2).
  • width / height: Fixed dimensions in character cells (auto-calculated from content if omitted).
  • gap: Spacing in cells/lines between child items.

Box

Draws a bordered container around content:

  • title: Header text displayed on the top border (preserves custom ANSI styles).
  • borderColor: Style function applied to border characters (e.g., ANSI.cyan).
  • scrollBottom: When true, automatically keeps the latest lines visible if content overflows the box height.

SelectList

Interactive vertical menu with Up/Down arrow navigation and Enter selection:

  • items: Array of strings to display.
  • onSelect(item): Callback triggered when Enter is pressed.

Page & PageManager

Manages multi-screen applications with isolated hotkeys:

  • Page: Extend this class to build custom views. Use this.setHotkeys([{ key, label, action }]) to bind page-specific shortcuts and this.renderHotkeys() to render a formatted hotkey bar anywhere in your layout.
  • PageManager: Accepts { pages, initialPage } and routes render() and handleKey() calls exclusively to the active page via manager.goTo(pageName).

ANSI Utilities

  • ANSI: Chainable color and modifier builder (bold, dim, inverse, red, green, yellow, cyan, gray, brightWhite, bgBlue, bgYellow, etc.) plus ANSI.style(stylesArray, text).
  • String Helpers: stripAnsi(str), visibleLength(str), truncateAnsi(str, maxLen), padRight(str, len), and padLeft(str, len).

Running Tests

The test suite uses the built-in Node.js test runner (node:test) with zero external dependencies:

npm test

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages