The module exports a Termios
class, that encapsulates termios struct data:
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.
constructor(from?: number | ITermios | null)
Create newTermios
object.from
can be a valid file descriptor (number), anotherTermios
object (copy constructor) ornull
(all data zeroed out). Omittingfrom
will 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
.action
should 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.speed
should be one of the baudrates innative.BAUD
.setOutputSpeed(speed: number): void
Set output channel baud rate.speed
should be one of the baudrates innative.BAUD
.setSpeed(speed: number): void
Set input and output channel baud rate.speed
should 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 termios
member alignments and sizes.
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 descriptorfd
is a tty. Might throw iffd
is 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 descriptorfd
inbuffer
. The given buffer must have a length ofnative.EXPLAIN.size
.tcsetattr(fd: number, action: number, buffer: Buffer): void
Write termios data held inbuffer
to file descriptorfd
.action
should be one oftermios.ACTION
. The given buffer must have a length ofnative.EXPLAIN.size
.tcsendbreak(fd: number, duration: number): void
tcdrain(fd: number): void
tcflush(fd: number, queue_selector: number): void
tcflow(fd: number, flowaction: number)
cfgetispeed(buffer: Buffer): void
cfgetospeed(buffer: Buffer)
cfsetispeed(buffer: Buffer, speed: number): void
cfsetospeed(buffer: Buffer, speed: number): void
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.