headless-terminal
Advanced tools
Comparing version
// dummy dom api | ||
if (typeof global != 'undefined') { | ||
var vt = require('vt') | ||
, ScreenBuffer = require('screen-buffer') | ||
, EventEmitter = require('events').EventEmitter | ||
, inherits = require('util').inherits | ||
if (!global.navigator) { | ||
global.navigator = { userAgent: '' } | ||
} | ||
// # new HeadlessTerminal(cols, rows) | ||
// | ||
// A headless terminal is a terminal with an internal screen buffer. | ||
// | ||
// When the display is changed, the `change` event is emitted | ||
// with the display buffer as an argument. | ||
// | ||
// __Note:__ Since v0.3 the API has been _completely changed_. | ||
// | ||
// | ||
// ## Usage | ||
// | ||
// ```javascript | ||
// var HeadlessTerminal = require('headless-terminal') | ||
// var terminal = new HeadlessTerminal(80, 25) | ||
// terminal.write('write some data and ansi code') | ||
// console.log(terminal.displayBuffer.toString()) | ||
// ``` | ||
// | ||
function HeadlessTerminal(cols, rows) { | ||
EventEmitter.call(this) | ||
this.termBuffer = new vt.TermBuffer(cols, rows) | ||
this.termBuffer.setMode('crlf', true) | ||
this.termWriter = new vt.TermWriter(this.termBuffer) | ||
if (!global.document) { | ||
global.document = { | ||
addEventListener: function() { } | ||
, createElement: function() { | ||
return { | ||
addEventListener: function() { } | ||
, appendChild: function() { } | ||
, removeChild: function() { } | ||
, style: {} | ||
} | ||
} | ||
} | ||
global.document.body = global.document.createElement('body') | ||
} | ||
// ## Attributes | ||
// | ||
// ### displayBuffer | ||
// | ||
// The underlying [screen-buffer](http://github.com/dtinth/screen-buffer) | ||
// | ||
this.displayBuffer = new ScreenBuffer() | ||
} | ||
// ## API | ||
// | ||
// HeadlessTerminal inherits EventEmitter. | ||
// | ||
inherits(HeadlessTerminal, EventEmitter) | ||
HeadlessTerminal.prototype.open = function() { | ||
/* nuffink, leaving it here for */ | ||
} | ||
var Terminal = require('./vendor/term') | ||
, ScreenBuffer = require('screen-buffer') | ||
// ### write(whatever) | ||
// | ||
// Writes some thing to the terminal. | ||
// After that, a change event will be emitted. | ||
// | ||
HeadlessTerminal.prototype.write = function(whatever) { | ||
this.termWriter.write(whatever) | ||
var screen = this.displayBuffer | ||
, buffer = this.termBuffer | ||
, height = buffer.height | ||
screen.setRows(height) | ||
for (var i = 0; i < height; i ++) { | ||
var line = buffer.getLine(i) | ||
screen.update(i, this._convertLine(line)) | ||
} | ||
screen.cursorX = buffer.cursor.x | ||
screen.cursorY = buffer.cursor.y | ||
Terminal.cursorBlink = false | ||
/** | ||
* A headless terminal is a terminal with an internal screen buffer. | ||
* Don't forget to open() the terminal! | ||
* | ||
* When the display is changed, the `change` event is emitted | ||
* with the display buffer as an argument. | ||
*/ | ||
function HeadlessTerminal(cols, rows, refresher) { | ||
this.displayBuffer = new ScreenBuffer() | ||
Terminal.call(this, cols, rows) | ||
// ## Events | ||
// | ||
// ### 'change' (buffer) | ||
// | ||
// Emitted when something is written to the terminal. | ||
// The first argument will be the underlying screen-buffer. | ||
// | ||
this.emit('change', this.displayBuffer, 0, height - 1) | ||
} | ||
Terminal.inherits(HeadlessTerminal, Terminal) | ||
HeadlessTerminal.prototype._convertLine = function(line) { | ||
// expose | ||
HeadlessTerminal.Terminal = Terminal | ||
HeadlessTerminal.ScreenBuffer = ScreenBuffer | ||
HeadlessTerminal.patcher = require('./buffer-patcher') | ||
var chars = [ ] | ||
, str = pad(line.str, this.termBuffer.width) | ||
, attr = null | ||
HeadlessTerminal.prototype.refresh = function(start, end) { | ||
// update the display buffer | ||
for (var y = start; y <= end; y ++) { | ||
var row = y + this.ydisp | ||
, line = this.lines[row] | ||
this.displayBuffer.update(y, line) | ||
var length = str.length | ||
chars.length = length | ||
for (var i = 0; i < length; i ++) { | ||
if (line.attr[i]) attr = line.attr[i] | ||
chars[i] = [this._convertAttribute(attr), str.charAt(i)] | ||
} | ||
this.displayBuffer.cursorX = this.x | ||
this.displayBuffer.cursorY = this.y | ||
// emit the change event | ||
this.emit('change', this.displayBuffer, start, end) | ||
return chars | ||
} | ||
HeadlessTerminal.prototype._convertAttribute = function(attr) { | ||
var fg = attr && attr.fg != null ? attr.fg : 257 | ||
, bg = attr && attr.bg != null ? attr.bg : 256 | ||
, inverse = attr && attr.inverse ? 1 : 0 | ||
, underline = attr && attr.underline ? 1 : 0 | ||
, bold = attr && attr.bold ? 1 : 0 | ||
return (inverse << 20) | (underline << 19) | (bold << 18) | (fg << 9) | bg | ||
} | ||
function pad(str, width) { | ||
if (str.length >= width) return str | ||
var howMany = width - str.length | ||
return str + new Array(howMany + 1).join(' ') | ||
} | ||
// ## Static Members | ||
// | ||
// ### HeadlessTerminal.ScreenBuffer | ||
// | ||
// The ScreenBuffer class. | ||
// | ||
HeadlessTerminal.ScreenBuffer = ScreenBuffer | ||
module.exports = HeadlessTerminal | ||
// ## License | ||
// | ||
// MIT | ||
// | ||
{ | ||
"name": "headless-terminal", | ||
"version": "0.1.1", | ||
"description": "Headless xterm emulator forked from tty.js's term.js", | ||
"version": "0.3.1", | ||
"description": "Headless xterm emulator using vt (terminal.js).", | ||
"main": "headless-terminal.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha" | ||
}, | ||
@@ -17,4 +17,8 @@ "repository": { | ||
"dependencies": { | ||
"screen-buffer": "~0.1.2" | ||
"screen-buffer": "~0.3.3", | ||
"vt": "~0.1.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1.17.1" | ||
} | ||
} |
@@ -0,16 +1,51 @@ | ||
# new HeadlessTerminal(cols, rows) | ||
A headless terminal emulator forked from tty.js. For use in ttycast 0.1.x. | ||
A headless terminal is a terminal with an internal screen buffer. | ||
License is MIT. | ||
When the display is changed, the `change` event is emitted | ||
with the display buffer as an argument. | ||
Usage: | ||
__Note:__ Since v0.3 the API has been _completely changed_. | ||
var HeadlessTerminal = require('headless-terminal') | ||
var terminal = new HeadlessTerminal(80, 25) | ||
terminal.open() | ||
## Usage | ||
terminal.write('write some data and ansi code') | ||
console.log(terminal.displayBuffer.toString()) | ||
```javascript | ||
var HeadlessTerminal = require('headless-terminal') | ||
var terminal = new HeadlessTerminal(80, 25) | ||
terminal.write('write some data and ansi code') | ||
console.log(terminal.displayBuffer.toString()) | ||
``` | ||
## Attributes | ||
### displayBuffer | ||
The underlying [screen-buffer](http://github.com/dtinth/screen-buffer) | ||
## API | ||
HeadlessTerminal inherits EventEmitter. | ||
### write(whatever) | ||
Writes some thing to the terminal. | ||
After that, a change event will be emitted. | ||
## Events | ||
### 'change' (buffer) | ||
Emitted when something is written to the terminal. | ||
The first argument will be the underlying screen-buffer. | ||
## Static Members | ||
### HeadlessTerminal.ScreenBuffer | ||
The ScreenBuffer class. | ||
## License | ||
MIT | ||
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
8
33.33%2
-33.33%52
205.88%6492
-94.53%2
100%1
Infinity%147
-96.2%1
Infinity%+ Added
+ Added
+ Added
- Removed
Updated