Skip to content

Interactive TUI

The InteractiveTable turns any table into a full-featured terminal UI with pagination, search, sorting, and row selection — all driven by keyboard shortcuts.

Basic Usage

ts
import { Table, InteractiveTable } from 'cmd-table';

const table = new Table();
table.addColumn('ID');
table.addColumn('Name');
table.addColumn('Department');
table.addColumn('Status');

// Add sample data
for (let i = 1; i <= 100; i++) {
    table.addRow({
        ID: i,
        Name: `Employee ${i}`,
        Department: ['Engineering', 'Design', 'Marketing', 'HR'][i % 4],
        Status: i % 3 === 0 ? 'Away' : 'Active'
    });
}

const tui = new InteractiveTable(table, {
    onExit: () => process.exit(0)
});

tui.start();

This displays the first page of data with a status bar showing controls.

Keyboard Controls

KeyAction
or nNext page
or pPrevious page
sCycle sort column (toggles asc/desc)
/Enter search mode — type to filter rows
SpaceSelect / deselect all visible rows
EnterConfirm selection and exit
EscClear search filter or clear selection
cToggle column visibility
q or Ctrl+CQuit

Handling Row Selection

Use the onSelect callback to capture selected rows:

ts
const tui = new InteractiveTable(table, {
    onSelect: (selectedRows) => {
        console.log(`Selected ${selectedRows.length} rows:`);
        selectedRows.forEach(row => {
            console.log(`  - ${row.cells[1].content}`);
        });
        process.exit(0);
    },
    onExit: () => {
        console.log('Exited without selection.');
        process.exit(0);
    }
});

tui.start();

Workflow:

  1. Browse pages with /
  2. Press Space to select/deselect visible rows
  3. Press Enter to confirm — onSelect is called with selected rows
  4. Press q to quit — onExit is called

Search & Filtering

  1. Press / to enter search mode
  2. Type your query — rows are filtered in real-time
  3. Press Esc to clear the filter and return to the full dataset

The search matches against all columns and is case-insensitive.

Try It

Run the included demo scripts:

bash
# Basic interactive table
npx ts-node examples/interactive_table.ts

# Large dataset (1000+ rows)
npx ts-node examples/large_table.ts

Configuration Options

OptionTypeDescription
onSelect(rows: Row[]) => voidCalled when user confirms selection with Enter
onExit() => voidCalled when user quits with q or Ctrl+C
pageSizenumberRows per page (default: fits terminal height)