A terminal utility module for node.js serving a cross-platform console api. Works with Node.js v0.8 and above.
This project is still under development and thus the API may change, please report any bugs here, and feel free to make feature requests.
Since this is a native c++ node.js addon, you will need these tools to build the packages (see node-gyp):
- python (v2.7+, but not 3.x)
- on unix:
- make
- c++ compiler (e.g. gcc)
- on mac:
- xcode command line tools
- on windows:
- Visual Studio 2010 or 2013
Simply add ttyutil to the dependencies of your project:
npm install --save ttyutilAnd require it anywhere in your codebase:
var TTYUtil = require("ttyutil");The TTYUtil exposes the TTYUtil constructor (var ttyu = new TTYUtil()).
This creates the ttyu object which is used to interact with the terminal.
Adds an event listener callback for the event(see
TTYUtil.EVENT).
For the event callback parameter see here.
Remove the event listener callback from the event (see
TTYUtil.EVENT).
Shortcut for ttyu.removeListener(event, listener).
Emit an event programmatically, see /tests/key.js. more documentation will be added soon.
Start emitting input events (mouse, key and resize events) to the listeners
attached by tty.on(event, listener).
Pause emitting input events.
Destroy the object and release all resources. You wont be able to use any more functions or restart.
Boolean flag indicating if input events are emitted (true when ttyu.start()
was executed). read-only
Integer amount of horizontal character space in the terminal. read-only
Integer amount of vertical character space in the terminal. read-only
Integer identifier for current terminal mode (can be one of
TTYUtil.MODE).
read-only
Integer amount of colors available in the terminal (16-256). read-only
Current x-position of the terminal cursor.
Current y-position of the terminal cursor.
Function to set the current cursor position to (x, y).
Function to write a string chunk at the current cursor position (ttyu.x,
ttyu.y) with the foreground color fg and the background color bg to the
terminal buffer. fg and bg can be terminal color codes, hexadecimal strings
(e.g. "#FF0000") or rgb strings (e.g. "rgb(255,0,0)").
Function to prepare a string chunk with the foreground color fg and
background color bg. Works like ttyu.write(chunk, fg, bg), but returns the
formatted string without printing it to the terminal.
Function to create a warning sound or visual warning effect.
Function to clear a part of the terminal buffer. x and y describe the
top-left corner of the rectangle and width and height define the size of the
rectangle to clear. When called without arguments, this function will clear the
complete (visible) terminal buffer.
Function to get the terminal color code from str, a hexadecimal string
(e.g. "#FF0000") or rgb string (e.g. "rgb(255,0,0)").
This object wraps all input events as constants:
TTYUtil.EVENT.ERROR{String}:ERRORidentifier for error eventsTTYUtil.EVENT.SIGNAL{String}:SIGNALidentifier for process signal eventsTTYUtil.EVENT.KEY{String}:KEYidentifier for key eventsTTYUtil.EVENT.RESIZE{String}:RESIZEidentifier for resize eventsTTYUtil.EVENT.MOUSEDOWN{String}:MOUSEDOWNidentifier for mousedown events (mouse button pressed)TTYUtil.EVENT.MOUSEUP{String}:MOUSEUPidentifier for mouse up events (mouse button released)TTYUtil.EVENT.MOUSEMOVE{String}:MOUSEMOVEidentifier for mouse move eventsTTYUtil.EVENT.MOUSEWHEEL{String}:MOUSEWHEELidentifier for mouse wheel events (vertical scroll events)TTYUtil.EVENT.MOUSEHWHEEL{String}:MOUSEHWHEELidentifier for the mouse hwheel events (horizontal scroll events)
This object wraps all terminal modes as constants:
TTYUtil.MODE.CMD{Integer}:0identifier for the windows cmd.exeTTYUtil.MODE.VT102{Integer}:1identifier for VT102 compilant terminals (e.g. iTerm2 for OSX or xTerm for X11)TTYUtil.MODE.VT100{Integer}:2identifier for VT100 compilant terminals (e.g. Terminal.app for OSX)
This object wraps all mouse buttons identifier as constants:
TTYUtil.MOUSE.LEFT{Integer}:1left-most mouse buttonTTYUtil.MOUSE.LEFT2{Integer}:4second left-most mouse buttonTTYUtil.MOUSE.LEFT3{Integer}:8thrid left-most mouse buttonTTYUtil.MOUSE.LEFT4{Integer}:16fourth left-most mouse buttonTTYUtil.MOUSE.RIGHT{Integer}:2right-most mouse button
This object wraps all control key identifier as constants:
TTYUtil.CTRL.NULL{Integer}:0no control key is activeTTYUtil.CTRL.ALT{Integer}:1the alt key is pressedTTYUtil.CTRL.CTRL{Integer}:2the ctrl key is pressedTTYUtil.CTRL.SHIFT{Integer}:4the shift key is pressedTTYUtil.CTRL.ENHANCED{Integer}:8the enhanced key is pressedTTYUtil.CTRL.CMD{Integer}:16the cmd key is pressed (OSX)TTYUtil.CTRL.NUMLOCK{Integer}:32numlock is activatedTTYUtil.CTRL.SCROLLLOCK{Integer}:64scrolllock is activatedTTYUtil.CTRL.CAPSLOCK{Integer}:128capslock is activated
This object contains a list of all signals that can be catched by ttyutil:
TTYUtil.SIGNAL.SIGINT{String}:SIGINTinterrupt event (e.g. CTRL+C)TTYUtil.SIGNAL.SIGTERM{String}:SIGTERMtermination eventTTYUtil.SIGNAL.SIGPIPE{String}:SIGPIPEpipe error eventTTYUtil.SIGNAL.SIGHUP{String}:SIGHUPterminal close event
This object wraps all key codes as constants. (see /const.js for the complete list)
Every event emits one object to the callbacks. Every object contains a type
property which is one of TTYUtil.EVENT, to identify the event.
ERROR-Event emits an object containing information about the error in theerrorpropertySIGNAL-Event emits an object containing the raised signal (seeTTYUtil.SIGNAL) in thesignalproperty (e.g.{ signal: "SIGINT" })KEY-Event emits an object containing following properties:ctrl{Integer} the control characters currently pressed (seeTTYUtil.CTRL)char{String} the string representation of the pressed keycode{Integer} the ascii key-code of the pressed keywhich{Integer} unique identifier for the pressed key (seeTTYUtil.WHICH)
RESIZE-Event emits an object with only the event type parameter, usettyu.width/ttyu.heightto get the new window sizeMOUSE...-Events emit an object containing following properties:button{Integer} the button pressed (seeTTYUtil.MOUSE)x{Integer} x-coordinate in the terminal, the event occured iny{Integer} x-coordinate in the terminal, the event occured inctrl{Integer} the control characters currently pressed (seeTTYUtil.CTRL)
run the tests using npm test. check out /tests for test cases.
see the /examples folder
Copyright (c) 2015 Bernhard Bücherl
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

