node-termios v0.1.3
Access termios settings from nodejs.
The module exports a Termios class, that encapsulates termios struct data:
Properties
c_iflag: Integer representing the input mode flags.c_oflag: Integer representing the output mode flags.c_cflag: Integer representing the control mode flags.c_lflag: Integer representing the local mode flags.c_cc: Buffer representing the control code settings.
Methods
constructor(from?: number | ITermios | null)
Create newTermiosobject.fromcan be a valid file descriptor (number), anotherTermiosobject (copy constructor) ornull(all data zeroed out). Omittingfromwill try to load default values fromttydefaults.h(not supported by all platforms).loadFrom(fd: number): void
Load termios data from file descriptorfd.writeTo(fd: number, action?: number): void
Write termios data to file descriptorfd.actionshould be one ofnative.ACTION(default:native.ACTION.TCSAFLUSH).getInputSpeed(): number
Return input channel baud rate setting as innative.BAUD.getOutputSpeed(): number
Return output channel baud rate setting as innative.BAUD.setInputSpeed(speed: number): void
Set input channel baud rate.speedshould be one of the baudrates innative.BAUD.setOutputSpeed(speed: number): void
Set output channel baud rate.speedshould be one of the baudrates innative.BAUD.setSpeed(speed: number): void
Set input and output channel baud rate.speedshould be one of the baudrates innative.BAUD.setraw(): void
Convenient method to set termios data to raw mode (values taken from Python).setcbreak(): void
Convenient method to set termios data to cbreak mode (values taken from Python).setcooked(): void
Convenient method to set termios data back to cooked mode.
The module further exports known symbols defined by the
underlying termios.h (platform dependent) and low level functions under native:
ALL_SYMBOLS: All known symbols.IFLAGS: Input mode symbols.OFLAGS: Output mode symbols.CFLAGS: Character mode symbols.LFLAGS: Local mode symbols.CC: Valid symbols defined for control character settings.ACTION: Actions defined fortcsetattr.FLUSH: Symbols fortcflush.FLOW: Symbols fortcflow.BAUD: Defined baudrates of the platform.EXPLAIN:struct termiosmember alignments and sizes.
Low level functions
The low level function are direct mappings of the C functions,
where fd should be a valid TTY file descriptor
and buffer is a nodejs buffer of termios struct size (see native.EXPLAIN.size).
Additional enum like arguments are mapped in native (see listing above).
Also see termios manpage for further details.
isatty(fd: number): boolean
Test if file descriptorfdis a tty. Might throw iffdis not valid.ttyname(fd: number): string
Return tty file name forfd, or empty string (invalid file descriptor).ptsname(fd: number): string
Return pts file name for file descriptorfd, or empty string (invalid file descriptor). Note that this only works on a master end of a PTY. For slave end usettyname.tcgetattr(fd: number, buffer: Buffer): void
Load termios data for file descriptorfdinbuffer. The given buffer must have a length ofnative.EXPLAIN.size.tcsetattr(fd: number, action: number, buffer: Buffer): void
Write termios data held inbufferto file descriptorfd.actionshould be one oftermios.ACTION. The given buffer must have a length ofnative.EXPLAIN.size.tcsendbreak(fd: number, duration: number): voidtcdrain(fd: number): voidtcflush(fd: number, queue_selector: number): voidtcflow(fd: number, flowaction: number)cfgetispeed(buffer: Buffer): voidcfgetospeed(buffer: Buffer)cfsetispeed(buffer: Buffer, speed: number): voidcfsetospeed(buffer: Buffer, speed: number): void
Examples
The example demostrates how to switch off/on echoing on STDIN:
const { Termios, native: { LFLAGS } } = require('node-termios');
// load termios data from STDIN
var t = new Termios(0);
// disable ECHO (not yet written)
t.c_lflag &= ~LFLAGS.ECHO;
// write back to STDIN
t.writeTo(0);
// now interactive input does not show up anymore
...
// switch on again when done
t.c_lflag |= LFLAGS.ECHO;
t.writeTo(0);A typical usage pattern seen in C is to store the settings, do some needed customizations like entering raw mode and restoring old settings afterwards:
const { Termios } = require('node-termios');
// save current settings to restore later on
const initial = new Termios(some_tty_fd);
// create copy and enter raw mode
const altered = new Termios(initial);
altered.setraw();
altered.writeTo(some_tty_fd);
// raw mode, thus every single data event shows up
...
// restore old settings
initial.writeTo(some_tty_fd);Note that in the interactive nodejs shell the standard file descriptors are already customized by the nodejs env and should not be handled like in the example above, or the terminal might break with the shell expectations.
For a silly yet slightly more advanced example, see example.js.