burgee
Switching a terminal UI from…

Coming from terminal-kit

Move a terminal-kit full-screen program to controlroom, the burgee family's terminal-kit alternative: fullscreen, grabInput, on('key'), menus and screen buffers mapped onto open(), keymaps and flagstaff panes, what does not map, and what burgee migrate reports.

controlroom is the burgee family's terminal-kit alternative for keyboard-driven terminal programs, inline or full-screen. open(), layout, tabs and keymaps, the input line and plugins all ship, since 0.2.0.

terminal-kit is several libraries in one. Its styled output, spinners, progress bars and yes/no questions map to other packages: roundel for colour, flagstaff for spinners and progress, caique for questions. This page is about the part that takes over the screen and reads keys, which is controlroom's. If you only draw inline output, start at Which one do I need?

Migrate from terminal-kit: not one import

There is no controlroom/terminal-kit, and none is planned: its surface is too large to reproduce honestly, so moving is a rewrite of each screen, not of the import line. burgee migrate finds the terminal-kit sites in your project and names the section of this page that covers each one:

npx burgee migrate --dry-run --json

It reports and never rewrites: a terminal-kit screen and a controlroom screen are different shapes, so each one is a decision, not a substitution. For this program:

const term = require('terminal-kit').terminal;

term.fullscreen(true);
term.grabInput(true);
term.on('key', (name) => {
  if (name === 'CTRL_C') term.processExit(0);
});
term.singleColumnMenu(['build', 'test'], (error, response) => {});

the report's guided list reads:

[
  { "file": "src/ui.js", "line": 1, "from": "terminal-kit", "pattern": "import", "guide": "https://burgee.interlace.tools/docs/coming-from/terminal-kit#the-mapping" },
  { "file": "src/ui.js", "line": 3, "from": "terminal-kit", "pattern": "alternate-screen", "guide": "https://burgee.interlace.tools/docs/coming-from/terminal-kit#alternate-screen" },
  { "file": "src/ui.js", "line": 4, "from": "terminal-kit", "pattern": "key", "guide": "https://burgee.interlace.tools/docs/coming-from/terminal-kit#key-handling" },
  { "file": "src/ui.js", "line": 5, "from": "terminal-kit", "pattern": "key", "guide": "https://burgee.interlace.tools/docs/coming-from/terminal-kit#key-handling" },
  { "file": "src/ui.js", "line": 8, "from": "terminal-kit", "pattern": "list", "guide": "https://burgee.interlace.tools/docs/coming-from/terminal-kit#list" }
]

A site is reported only when its receiver came from terminal-kit in the same file: the terminal or realTerminal the module hands out, one made by createTerminal(), or a ScreenBuffer. A term passed in from another file is not followed. The exit code stays 0, because nothing was refused. The Migrate page has the whole report.

The mapping

terminal-kitcontrolroom
require('terminal-kit').terminalopen(runtime, { screen })
term.fullscreen(true)open(runtime, { screen: 'alternate' })
term.fullscreen(false)close() on what open() returned, or any exit
term.grabInput(true)nothing: raw mode is taken once by the screen
term.on('key', fn)an entry in a keymap, which is data, and onAction
new termkit.ScreenBuffer() and draw()nothing: an update() repaints
term.moveTo(x, y) with texta pane in a layout tree, holding a flagstaff component
term.singleColumnMenu()flagstaff's tasks for a list you show; caique's select for a pick
term.on('resize', fn)nothing: the screen lays out again
term.inputField()the input line: caique's line editor in a pane
term.grabInput({ mouse }), on('mouse')nothing

Screen

terminal-kit hands you a terminal and leaves the rest to you. controlroom's open(runtime, { screen: 'alternate' }) is one call for the alternate screen, raw mode, the layout and the restore. It takes the terminal only when roundel's output policy says tty and stdin can go raw. In every other mode it opens a static screen instead, so the same program runs in a pipe, in CI and under --json. runtime is the structural runtime the family's packages take instead of reading process; processRuntime() is the real one.

status.mjs
import { open, processRuntime } from 'controlroom';

/** The smallest flagstaff component: its state is the text it shows. */
const text = { name: 'text', static: (s) => s };

const screen = open(processRuntime(), {
  screen: 'alternate',
  json: process.argv.includes('--json'),
  layout: 'status',
  keymap: { r: 'refresh', q: 'quit', 'ctrl+c': 'quit' },
  panes: { status: { component: text, state: 'cache: stale', label: 'Status' } },
  onAction(action, s) {
    if (action === 'refresh') s.update('status', 'cache: fresh');
  },
});

// Off a terminal nobody can press `r`, so the program does what the key would have done.
if (!screen.interactive) {
  screen.dispatch('refresh');
  screen.close();
}

On a terminal it waits for r, and q or Ctrl+C closes it. Piped:

node status.mjs | cat
Status
cache: stale
cache: fresh

screen: 'inline', the default, draws a live region under the program's own output instead, which stays in the scrollback.

burgee migrate reports createTerminal(), new termkit.ScreenBuffer() and new termkit.ScreenBufferHD() here, and follows terminal and realTerminal to their uses.

Box

terminal-kit has no box: a program moves the cursor and writes, or fills a ScreenBuffer. In controlroom a region of the screen is a pane, a name in one layout tree of rows and columns with fixed, fractional ({ fr, min }) and fit-to-content sizes. What a pane draws is any flagstaff component; a border, padding and a title come from flagstaff's boxComponent.

import { layout } from 'controlroom';

const tree = {
  direction: 'column',
  parts: [
    { content: { direction: 'row', parts: [{ size: { fr: 1, min: 20 }, content: 'learn' }, { size: { fr: 2 }, content: 'tasks' }] } },
    { size: 8, content: 'logs' },
  ],
};
layout(tree, { x: 0, y: 0, width: 100, height: 30 }).get('tasks');
// { x: 34, y: 0, width: 66, height: 22 }

No absolute moveTo() survives the move: positions are arithmetic over the terminal's size, done again on every resize. layout() is the function open() calls on every paint.

List

singleColumnMenu(), singleLineMenu(), singleRowMenu() and gridMenu() each ask the user to pick. A pick is a prompt, and prompts are caique's: select, asked before or after the screen, not inside it. No pane component picks. A list you only show, such as steps with a state each, is flagstaff's tasks in a pane. burgee migrate reports the four menus here.

Key handling

terminal-kit has you call grabInput(true) and then subscribe to on('key') with a function that switches on the key name. controlroom takes raw mode once for the screen's whole life, hands it back on every exit, and binds each key to an action name in a keymap that is plain data. The screen's tabs, focus and collapsed panes move through a reducer, and your onAction(action, screen) sees every action after it. The hint line is generated from the same keymap.

import { hints, initial, reduce } from 'controlroom';

const keymap = { left: 'tab.prev', right: 'tab.next', s: 'toggle:status', q: 'quit', 'ctrl+c': 'quit' };
const labels = { 'tab.prev': 'switch tab', 'tab.next': 'switch tab', 'toggle:status': 'toggle status', quit: 'quit' };

let state = initial(['Status', 'Tail logs'], ['learn', 'tasks']);
state = reduce(state, keymap.right); // state.active is 1: the second tab
hints(keymap, labels); // '←→ switch tab  s toggle status  q/^C quit'

An action the reducer does not know, such as quit here, leaves the state alone and is the program's own to handle; a live screen closes on quit. Key names are spelled as caique's key decoder spells them:

terminal-kitcontrolroom
CTRL_Cctrl+c
SHIFT_TABshift+tab
LEFT, RIGHT, ESCAPEleft, right, escape

Keys are read only when stdout is a terminal and stdin can go raw, and there is no grabInput() to forget to release. Everywhere else screen.dispatch(action) runs an action as if its key had been pressed.

burgee migrate reports grabInput() and every on() or once() whose event name starts with key.

Render loop

A terminal-kit program writes as it goes, or fills a ScreenBuffer and calls draw({ delta: true }) when it decides to. controlroom has no draw call to make. update(pane, state) repaints: the layout is composed from every pane's frame and handed to flagstaff's frame writer, which writes only the rows that changed, inside one synchronized-output block, and the screen is laid out again on resize. controlroom never writes to the terminal itself. burgee migrate reports each draw() on a screen buffer.

Alternate screen

fullscreen(true) enters the alternate screen and fullscreen(false) leaves it. controlroom's is the screen: 'alternate' option. The alternate screen, the hidden cursor, raw mode and bracketed paste are each entered through closeout with their undo registered in the same call, so close(), Ctrl+C, a signal and a throw all hand the terminal back. The alternate screen has no scrollback, so text given to commit() there is kept and handed to the main screen when the screen closes. burgee migrate reports each fullscreen() here.

What does not map

These are out of scope, so a program that depends on them should stay on terminal-kit.

Mouse

grabInput({ mouse: 'button' }) and on('mouse') have no controlroom equivalent, and none is planned. burgee migrate reports the mouse option and each mouse subscription it can see.

Text editing in a pane

inputField() maps to the input line: caique's line editor hosted in one pane, with history, completion and pasted text arriving whole. Keys go to it while it has focus and fall through to the keymap when it does not use them, and in a pipe each line of stdin is one entry. It is the input: { editor, pane, onSubmit, onEnd } option of open(). burgee migrate reports each inputField() here. A multi-line editor or a form inside the layout does not map: a question asked once is caique's prompt.

Legacy Windows consoles

controlroom handles no Windows legacy console quirks beyond what Node's own terminal handling does. If your program relies on terminal-kit's handling of a particular terminal, check that before you move it.

terminal-kit's document model, images and terminal capability database have no controlroom counterpart either.

Is controlroom compatible with terminal-kit?

No, by design. There is no drop-in and nothing is graded: no terminal-kit suite runs against controlroom, and none will, because the API is not being reproduced. The Compatibility page grades controlroom's drop-in for ink, controlroom/ink, by ink 8.0.0's own suite. It has no terminal-kit row.

What you gain over terminal-kit

  • Every caller gets an answer, not just a terminal. Off a terminal a screen prints each pane's static projection under its label, with no escape byte, no carriage return and no cursor movement, and only the lines that changed on each update. Tabs hide nothing, and a liveOnly pane such as the hint line is not printed.
  • --json is an event stream. One NDJSON event per change on stderr, named after the pane, with stdout left for the program's result.
  • It never waits for a key nobody can press. Behind a pipe, in CI or with < file, a screen reads no keys, and dispatch() is what the program has instead.
  • The terminal comes back. Leaving the alternate screen and raw mode is registered with closeout when they are entered, not left to an exit handler the program has to write.
  • One render engine. The spinner in a pane is the same flagstaff component, drawn by the same frame loop, as the spinner in an ordinary CLI.

This page does not say what terminal-kit writes to a pipe or under CI, and makes no size or speed comparison against terminal-kit: none has been measured here.

When to switch from terminal-kit

Switch a screen if its output has to work for a pipe, CI, a screen reader or an agent as well as for a person at a terminal. The inline parts of terminal-kit — colour, spinners, progress bars, questions — move to roundel, flagstaff and caique, a file at a time. Stay on terminal-kit if you depend on the mouse, on multi-line editing in place, or on its document model.

On this page