What is terminal-kit?
The terminal-kit npm package provides a comprehensive set of tools for creating rich terminal applications. It offers functionalities for handling terminal input and output, creating interactive user interfaces, and managing terminal capabilities.
What are terminal-kit's main functionalities?
Terminal Output
This feature allows you to output text to the terminal. The code sample demonstrates how to print 'Hello, world!' to the terminal using terminal-kit.
const term = require('terminal-kit').terminal;
term('Hello, world!\n');
Interactive Menus
This feature allows you to create interactive menus. The code sample shows how to create a single-column menu with three options and handle the user's selection.
const term = require('terminal-kit').terminal;
term.singleColumnMenu(['Option 1', 'Option 2', 'Option 3'], function(error, response) {
term('
').eraseLineAfter.green('You selected: %s
', response.selectedText);
});
Text Input
This feature allows you to capture text input from the user. The code sample demonstrates how to prompt the user to enter their name and then display it.
const term = require('terminal-kit').terminal;
term('Enter your name: ');
term.inputField(function(error, input) {
term.green('
Your name is: %s
', input);
});
Progress Bars
This feature allows you to create and manage progress bars. The code sample shows how to create a progress bar that updates every 100 milliseconds until it reaches 100%.
const term = require('terminal-kit').terminal;
const ProgressBar = require('terminal-kit').ProgressBar;
const progressBar = new ProgressBar({
width: 80,
title: 'Progress',
eta: true,
percent: true
});
let progress = 0;
const interval = setInterval(() => {
progress += 0.01;
progressBar.update(progress);
if (progress >= 1) {
clearInterval(interval);
term.green('
Done!
');
}
}, 100);
Other packages similar to terminal-kit
blessed
Blessed is a high-level terminal interface library for node.js. It provides a wide range of widgets and supports complex layouts, making it suitable for creating advanced terminal applications. Compared to terminal-kit, blessed offers more built-in widgets and a more extensive API for layout management.
ink
Ink is a React-like library for building command-line interfaces using React components. It allows you to use the familiar React paradigm to create terminal applications. Compared to terminal-kit, ink leverages the React ecosystem, making it a good choice for developers who are already comfortable with React.
vorpal
Vorpal is a framework for building interactive CLI applications. It provides a command-line interface with built-in support for commands, arguments, and options. Compared to terminal-kit, vorpal focuses more on creating command-line tools rather than rich terminal UIs.
Terminal Kit
Terminal utilities for node.js, it supports 'xterm' compatible terminal and the Linux Console.
It does not depend on ncurses.
- License: MIT
- Current status: alpha/unstable
- Platform: linux, tested with gnome-terminal, xterm and Linux Console so far
Work in progress, only a rough documentation ATM.
Features
- colors
- styles (bold, underline, italic, and many more)
- string formating
- style mixing
- cursor positionning
- keyboard input
- mouse support
- terminal window title
- event-driven
Quick example
var term = require( 'terminal-kit' ) ;
term( 'Hello world!\n' ) ;
term.red( 'red' ) ;
term.bold( 'bold' ) ;
term.bold.underline.red( 'mixed' ) ;
term.green( "My name is %s, I'm %d.\n" , 'Jack' , 32 ) ;
term( 'The terminal size is %dx%d' , term.width , term.height ) ;
term.moveTo( 1 , 1 ) ;
term.moveTo( 1 , 1 , 'Upper-left corner' ) ;
term.moveTo( 1 , 1 , "My name is %s, I'm %d.\n" , 'Jack' , 32 ) ;
term.moveTo.cyan( 1 , 1 , "My name is %s, I'm %d.\n" , 'Jack' , 32 ) ;
Short function description
All those functions are chainable, and their arguments can be combined.
Common/Misc
- reset(): full reset the terminal.
- error(): it just set error to true so it will write to STDERR instead of STDOUT
- beep(): emit a beep
Foreground colors
- defaultColor(): back to the default foreground color
- black(): ...
- red(): ...
- green(): ...
- yellow(): dark yellow, most of time brown or orange
- blue(): ...
- magenta(): ...
- cyan(): ...
- white(): ...
- brightBlack(): ...
- brightRed(): ...
- brightGreen(): ...
- brightYellow(): true yellow
- brightBlue(): ...
- brightMagenta(): ...
- brightCyan(): ...
- brightWhite(): ...
- color(register): choose between 16 colors using an 0..15 integer
- darkColor(register): choose between 8 regular (dark) colors using an 0..7 integer
- brightColor(register): choose between 8 bright colors using an 0..7 integer
Background colors
- bgDefaultColor(): back to the default background color
- bgBlack(): ...
- bgRed(): ...
- bgGreen(): ...
- bgYellow(): dark yellow, most of time brown or orange
- bgBlue(): ...
- bgMagenta(): ...
- bgCyan(): ...
- bgWhite(): ...
- bgDarkColor(): ...
- bgBrightBlack(): ...
- bgBrightRed(): ...
- bgBrightGreen(): ...
- bgBrightYellow(): true yellow
- bgBrightBlue(): ...
- bgBrightMagenta(): ...
- bgBrightCyan(): ...
- bgColor(register): choose between 16 colors using an 0..15 integer
- bgBrightWhite(): choose between 8 regular (dark) colors using an 0..7 integer
- bgBrightColor(): choose between 8 bright colors using an 0..7 integer
Styles
- styleReset(): reset all styles and go back to default colors without
- bold(): bold text
- dim(): faint color
- italic(): italic
- underline(): underline
- blink(): blink text, not widely supported
- inverse(): foreground and background color
- hidden(): invisible, but can be copy/paste'd
- strike(): strike through
Cursors
- saveCursor(): save cursor position
- restoreCursor(): restore a previously saved cursor position
- up(n): move the cursor 'n' chars up
- down(n): move the cursor 'n' chars down
- right(n): move the cursor 'n' chars right
- left(n): move the cursor 'n' chars left
- moveTo(x,y): move the cursor to the (x,y) coordinate (1,1 is the upper-left corner)
- move(x,y): relative move of the cursor
Editing
- clear(): clear the screen and move the cursor to the upper-left corner
- eraseDisplayBelow(): erase everything below the cursor
- eraseDisplayAbove(): erase everything above the cursor
- eraseDisplay(): erase everything
- eraseLineAfter(): erase current line after the cursor
- eraseLineBefore(): erase current line before the cursor
- eraseLine(): erase current line
- alternateScreenBuffer(boolean): this set/unset the alternate screen buffer, many terminal do not support it or inhibit it
Input/Output
- requestCursorLocation(): request the cursor location, a 'terminal' event will be fired when available
- requestScreenSize(): request for screen size, a 'terminal' event will be fired when available (rarely useful, most of time this event is fired on resize)
- applicationKeypad(): should allow keypad to send different code than 0..9 keys, not widely supported
Internal input/output (do not use directly, use grabInput() instead)
- mouseButton(): ask the terminal to send event when a mouse button is pressed, with the mouse cursor position
- mouseDrag(): ask the terminal to send event when a mouse button is pressed and when draging, with the mouse cursor position
- mouseMotion(): ask the terminal to send all mouse event, even mouse motion that occurs without buttons
- mouseSGR(): another mouse protocol that extend coordinate mapping (without it, it supports only 223 rows and columns)
- focusEvent(): ask the terminal to send event when it gains and loses focus, not widely supported
OS functions (not widely supported)
- windowTitle(): set the title of an xterm-compatible window
Input management with grabInput()
- grabInput(options): turn input grabbing on, keyboard entry will not be echoed, every input will generate an event
Quick example:
var term = require( '../lib/terminal.js' ) ;
function terminate()
{
term.grabInput( false ) ;
setTimeout( function() { process.exit() } , 100 ) ;
}
term.bold.cyan( 'Type anything on the keyboard...\n' ) ;
term.green( 'Hit CTRL-C to quit.\n\n' ) ;
term.grabInput( { mouse: 'button' } ) ;
term.on( 'key' , function( name , matches , data ) {
console.log( "'key' event:" , name ) ;
if ( matches.indexOf( 'CTRL_C' ) >= 0 ) { terminate() ; }
} ) ;
term.on( 'terminal' , function( name , data ) {
console.log( "'terminal' event:" , name , data ) ;
} ) ;
term.on( 'mouse' , function( name , data ) {
console.log( "'mouse' event:" , name , data ) ;
} ) ;