2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Blessed high-level interface
|
2013-06-11 16:48:37 +00:00
|
|
|
* Copyright (c) 2013, Christopher Jeffrey (MIT License)
|
|
|
|
* Still under heavy development.
|
2013-06-01 02:09:07 +00:00
|
|
|
*/
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
/**
|
|
|
|
* Modules
|
|
|
|
*/
|
|
|
|
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Node
|
|
|
|
*/
|
|
|
|
|
2013-06-01 02:39:11 +00:00
|
|
|
function Node(options) {
|
2013-06-06 09:03:25 +00:00
|
|
|
EventEmitter.call(this);
|
2013-06-09 13:33:23 +00:00
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
this.options = options || {};
|
2013-06-09 13:33:23 +00:00
|
|
|
this.screen = this.screen
|
|
|
|
|| Screen._default
|
|
|
|
|| (function(){throw new Error('No active screen.')})();
|
|
|
|
this.parent = options.parent || null; // this.screen;
|
|
|
|
this.children = [];
|
|
|
|
|
|
|
|
(options.children || []).forEach(this.append.bind(this));
|
|
|
|
|
|
|
|
if (this._isScreen && !this.focused) {
|
|
|
|
this.focused = this.children[0];
|
|
|
|
}
|
2013-06-01 02:39:11 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
Node.prototype.__proto__ = EventEmitter.prototype;
|
|
|
|
|
2013-06-01 02:39:11 +00:00
|
|
|
Node.prototype.prepend = function(element) {
|
2013-06-09 13:33:23 +00:00
|
|
|
element.parent = this;
|
|
|
|
|
|
|
|
if (this._isScreen && !this.focused) {
|
|
|
|
this.focused = element;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!~this.children.indexOf(element)) {
|
|
|
|
this.children.unshift(element);
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:13:49 +00:00
|
|
|
element.emit('reparent', this);
|
|
|
|
this.emit('adopt', element);
|
2013-06-01 02:39:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Node.prototype.append = function(element) {
|
2013-06-09 13:33:23 +00:00
|
|
|
element.parent = this;
|
|
|
|
|
|
|
|
if (this._isScreen && !this.focused) {
|
|
|
|
this.focused = element;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!~this.children.indexOf(element)) {
|
|
|
|
this.children.push(element);
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:13:49 +00:00
|
|
|
element.emit('reparent', this);
|
|
|
|
this.emit('adopt', element);
|
2013-06-01 02:39:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Node.prototype.remove = function(element) {
|
2013-06-09 13:33:23 +00:00
|
|
|
element.parent = null; // this.screen;
|
|
|
|
|
2013-06-01 02:39:11 +00:00
|
|
|
var i = this.children.indexOf(element);
|
2013-06-09 13:33:23 +00:00
|
|
|
if (~i) {
|
|
|
|
this.children.splice(i, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._isScreen && this.focused === element) {
|
|
|
|
this.focused = this.children[0];
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:13:49 +00:00
|
|
|
element.emit('reparent', null);
|
|
|
|
this.emit('remove', element);
|
2013-06-01 02:39:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Node.prototype.detach = function(element) {
|
|
|
|
this.parent.remove(element);
|
|
|
|
};
|
2013-06-01 02:09:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Screen
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Screen(options) {
|
2013-06-01 05:29:31 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-06-09 13:33:23 +00:00
|
|
|
if (!Screen._default) {
|
|
|
|
Screen._default = this;
|
|
|
|
}
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
Node.call(this, options);
|
2013-06-01 02:39:11 +00:00
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
this._isScreen = true;
|
2013-06-01 02:09:07 +00:00
|
|
|
this.program = options.program;
|
|
|
|
this.tput = this.program.tput;
|
|
|
|
this.dattr = ((0 << 18) | (0x1ff << 9)) | 0x1ff;
|
2013-06-01 05:29:31 +00:00
|
|
|
this.position = {
|
2013-06-11 16:48:37 +00:00
|
|
|
left: this.left = this.rleft = 0,
|
|
|
|
right: this.right = this.rright = 0,
|
|
|
|
top: this.top = this.rtop = 0,
|
|
|
|
bottom: this.bottom = this.rbottom = 0
|
2013-06-01 05:29:31 +00:00
|
|
|
};
|
|
|
|
|
2013-06-09 20:52:22 +00:00
|
|
|
//this.focused = null;
|
|
|
|
this.history = [];
|
2013-06-09 16:47:02 +00:00
|
|
|
this.clickable = [];
|
|
|
|
this.input = [];
|
2013-06-09 20:02:12 +00:00
|
|
|
this.grabKeys = false;
|
2013-06-09 16:47:02 +00:00
|
|
|
|
2013-06-01 05:29:31 +00:00
|
|
|
this.alloc();
|
|
|
|
|
|
|
|
this.program.on('resize', function() {
|
|
|
|
self.alloc();
|
|
|
|
self.render();
|
2013-06-06 09:03:25 +00:00
|
|
|
self.emit('resize');
|
2013-06-01 05:29:31 +00:00
|
|
|
});
|
2013-06-06 09:03:25 +00:00
|
|
|
|
2013-06-09 16:47:02 +00:00
|
|
|
this.program.alternateBuffer();
|
|
|
|
this.program.hideCursor();
|
|
|
|
|
|
|
|
process.on('exit', function() {
|
|
|
|
self.program.clear();
|
|
|
|
self.program.showCursor();
|
|
|
|
self.program.normalBuffer();
|
|
|
|
});
|
2013-06-11 14:27:50 +00:00
|
|
|
|
|
|
|
this.on('newListener', function fn(type) {
|
|
|
|
if (type === 'keypress' || type === 'mouse') {
|
|
|
|
self.removeListener('newListener', fn);
|
|
|
|
if (type === 'keypress') self._listenKeys();
|
|
|
|
if (type === 'mouse') self._listenMouse();
|
|
|
|
}
|
|
|
|
});
|
2013-06-01 05:29:31 +00:00
|
|
|
}
|
2013-06-01 02:39:11 +00:00
|
|
|
|
2013-06-09 13:33:23 +00:00
|
|
|
Screen._default = null;
|
|
|
|
|
2013-06-01 05:29:31 +00:00
|
|
|
Screen.prototype.__proto__ = Node.prototype;
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
// TODO: Bubble events.
|
|
|
|
Screen.prototype._listenMouse = function(el, hover) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (el) {
|
2013-06-09 20:02:12 +00:00
|
|
|
if (!hover) {
|
|
|
|
if (!~this.clickable.indexOf(el)) this.clickable.push(el);
|
|
|
|
} else {
|
|
|
|
if (!~this.hover.indexOf(el)) this.hover.push(el);
|
|
|
|
}
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this._listenedMouse) return;
|
|
|
|
this._listenedMouse = true;
|
|
|
|
|
|
|
|
this.program.enableMouse();
|
|
|
|
|
|
|
|
process.on('exit', function() {
|
|
|
|
self.program.disableMouse();
|
|
|
|
});
|
|
|
|
|
2013-06-09 20:21:58 +00:00
|
|
|
//this.on('element click', function(el) {
|
|
|
|
// el.focus();
|
|
|
|
//});
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
this.program.on('mouse', function(data) {
|
|
|
|
var i = 0, left, top, el;
|
|
|
|
for (; i < self.clickable.length; i++) {
|
|
|
|
el = self.clickable[i];
|
|
|
|
left = el.left + (el.border ? 1 : 0);
|
|
|
|
top = el.top + (el.border ? 1 : 0);
|
|
|
|
if (el.parent.childBase != null) top -= el.parent.childBase;
|
2013-06-06 15:27:19 +00:00
|
|
|
if (data.x > left && data.x <= left + el.width
|
|
|
|
&& data.y > top && data.y <= top + el.height) {
|
2013-06-06 09:03:25 +00:00
|
|
|
el.emit('mouse', data);
|
2013-06-09 20:21:58 +00:00
|
|
|
self.emit('element mouse', el, data);
|
2013-06-06 09:03:25 +00:00
|
|
|
if (data.action === 'mouseup') {
|
|
|
|
el.emit('click', data);
|
2013-06-09 20:21:58 +00:00
|
|
|
self.emit('element click', el, data);
|
2013-06-06 09:03:25 +00:00
|
|
|
} else if (data.action === 'movement') {
|
|
|
|
el.emit('hover', data);
|
2013-06-09 20:21:58 +00:00
|
|
|
self.emit('element hover', el, data);
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
el.emit(data.action, data);
|
2013-06-09 20:21:58 +00:00
|
|
|
self.emit('element ' + data.action, data);
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self.emit('mouse', data);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Bubble events.
|
|
|
|
Screen.prototype._listenKeys = function(el) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (el) {
|
2013-06-09 20:02:12 +00:00
|
|
|
if (!~this.input.indexOf(el)) {
|
2013-06-09 20:21:58 +00:00
|
|
|
if (this._listenedMouse) {
|
|
|
|
//this._listenMouse(el);
|
|
|
|
el.on('click', el.focus.bind(el));
|
|
|
|
}
|
2013-06-09 20:02:12 +00:00
|
|
|
this.input.push(el);
|
|
|
|
}
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this._listenedKeys) return;
|
|
|
|
this._listenedKeys = true;
|
|
|
|
|
|
|
|
this.program.on('keypress', function(ch, key) {
|
|
|
|
if (~self.input.indexOf(self.focused)) {
|
|
|
|
self.focused.emit('keypress', ch, key);
|
|
|
|
}
|
2013-06-09 20:02:12 +00:00
|
|
|
if (!self.grabKeys) {
|
2013-06-09 18:14:42 +00:00
|
|
|
self.emit('keypress', ch, key);
|
|
|
|
}
|
2013-06-06 09:03:25 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-06-01 05:29:31 +00:00
|
|
|
Screen.prototype.__defineGetter__('cols', function() {
|
|
|
|
return this.program.cols;
|
|
|
|
});
|
|
|
|
|
|
|
|
Screen.prototype.__defineGetter__('rows', function() {
|
|
|
|
return this.program.rows;
|
|
|
|
});
|
|
|
|
|
|
|
|
Screen.prototype.__defineGetter__('width', function() {
|
|
|
|
return this.program.cols;
|
|
|
|
});
|
|
|
|
|
|
|
|
Screen.prototype.__defineGetter__('height', function() {
|
|
|
|
return this.program.rows;
|
|
|
|
});
|
|
|
|
|
|
|
|
Screen.prototype.alloc = function() {
|
2013-06-01 02:09:07 +00:00
|
|
|
var x, y;
|
2013-06-01 05:29:31 +00:00
|
|
|
this.lines = [];
|
2013-06-01 02:09:07 +00:00
|
|
|
for (y = 0; y < this.rows; y++) {
|
|
|
|
this.lines[y] = [];
|
|
|
|
for (x = 0; x < this.cols; x++) {
|
|
|
|
this.lines[y][x] = [this.dattr, ' '];
|
|
|
|
}
|
|
|
|
this.lines[y].dirty = true;
|
|
|
|
}
|
2013-06-06 10:19:52 +00:00
|
|
|
this.olines = [];
|
|
|
|
for (y = 0; y < this.rows; y++) {
|
|
|
|
this.olines[y] = [];
|
|
|
|
for (x = 0; x < this.cols; x++) {
|
|
|
|
this.olines[y][x] = [];
|
|
|
|
}
|
|
|
|
}
|
2013-06-01 05:29:31 +00:00
|
|
|
};
|
2013-06-01 02:09:07 +00:00
|
|
|
|
|
|
|
Screen.prototype.render = function() {
|
2013-06-11 19:16:19 +00:00
|
|
|
// TODO: Could possibly drop .dirty and just clear the `lines` buffer every
|
|
|
|
// time before a screen.render. This way clearRegion doesn't have to be
|
|
|
|
// called in arbitrary places for the sake of clearing a spot where an
|
|
|
|
// element used to be (e.g. when an element moves or is hidden). There could
|
|
|
|
// be some overhead though.
|
|
|
|
// this.screen.clearRegion(0, this.cols, 0, this.rows);
|
2013-06-01 02:09:07 +00:00
|
|
|
this.children.forEach(function(el) {
|
|
|
|
el.render();
|
|
|
|
});
|
|
|
|
this.draw(0, this.rows - 1);
|
2013-06-11 18:13:49 +00:00
|
|
|
this.emit('draw');
|
2013-06-01 02:09:07 +00:00
|
|
|
};
|
|
|
|
|
2013-06-13 06:33:10 +00:00
|
|
|
Screen.prototype.blankLine = function(ch, dirty) {
|
|
|
|
var out = [];
|
|
|
|
for (var y = 0; y < this.rows; y++) {
|
|
|
|
out[y] = [];
|
|
|
|
for (var x = 0; x < this.cols; x++) {
|
|
|
|
out[y][x] = [this.dattr, ch || ' '];
|
|
|
|
}
|
|
|
|
out[y].dirty = dirty;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.insertLine = function(n, y, top, bottom) {
|
|
|
|
this.program.csr(top + 1, bottom + 1);
|
|
|
|
this.program.cup(y + 1, 1);
|
|
|
|
this.program.il(1);
|
|
|
|
this.program.csr(1, this.height - 1 + 1);
|
|
|
|
this.program.cup(y + 1, 1);
|
|
|
|
|
|
|
|
if (n < 1) n = 1;
|
|
|
|
|
|
|
|
var j = this.rows - 1 - bottom;
|
|
|
|
j = this.rows - 1 - j + 1;
|
|
|
|
|
|
|
|
while (n--) {
|
|
|
|
this.lines.splice(y, 0, this.blankLine());
|
|
|
|
this.lines.splice(j, 1);
|
|
|
|
this.olines.splice(y, 0, this.blankLine());
|
|
|
|
this.olines.splice(j, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.deleteLine = function(n, y, top, bottom) {
|
|
|
|
this.program.csr(top + 1, bottom + 1);
|
|
|
|
this.program.cup(y + 1, 1);
|
|
|
|
this.program.dl(1);
|
|
|
|
this.program.csr(1, this.height - 1 + 1);
|
|
|
|
this.program.cup(y + 1, 1);
|
|
|
|
|
|
|
|
if (n < 1) n = 1;
|
|
|
|
|
|
|
|
var j = this.rows - 1 - bottom;
|
|
|
|
j = this.rows - 1 - j + 1;
|
|
|
|
|
|
|
|
while (n--) {
|
|
|
|
this.lines.splice(j, 0, this.blankLine());
|
|
|
|
this.lines.splice(y, 1);
|
|
|
|
this.olines.splice(j, 0, this.blankLine());
|
|
|
|
this.olines.splice(y, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.insertBottom = function(top, bottom) {
|
|
|
|
return this.deleteLine(1, top, top, bottom);
|
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.insertTop = function(top, bottom) {
|
|
|
|
return this.insertLine(1, top, top, bottom);
|
|
|
|
};
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
Screen.prototype.draw = function(start, end) {
|
|
|
|
var x
|
|
|
|
, y
|
|
|
|
, line
|
|
|
|
, out
|
|
|
|
, ch
|
|
|
|
, data
|
|
|
|
, attr
|
|
|
|
, fgColor
|
|
|
|
, bgColor
|
|
|
|
, flags;
|
|
|
|
|
2013-06-06 10:19:52 +00:00
|
|
|
var lx = -1
|
|
|
|
, ly = -1
|
|
|
|
, o;
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
this.program.saveCursor();
|
|
|
|
|
|
|
|
for (y = start; y <= end; y++) {
|
|
|
|
line = this.lines[y];
|
2013-06-06 10:19:52 +00:00
|
|
|
o = this.olines[y];
|
2013-06-01 02:09:07 +00:00
|
|
|
|
2013-06-06 10:19:52 +00:00
|
|
|
// TODO: Possibly get rid of .dirty altogether.
|
2013-06-01 02:09:07 +00:00
|
|
|
if (!line.dirty) continue;
|
|
|
|
line.dirty = false;
|
|
|
|
|
|
|
|
out = '';
|
|
|
|
attr = this.dattr;
|
|
|
|
|
2013-06-01 05:29:31 +00:00
|
|
|
for (x = 0; x < this.cols; x++) {
|
|
|
|
data = line[x][0];
|
|
|
|
ch = line[x][1];
|
2013-06-01 02:09:07 +00:00
|
|
|
|
2013-06-06 10:19:52 +00:00
|
|
|
if (data === o[x][0] && ch === o[x][1]) {
|
|
|
|
if (lx === -1) {
|
|
|
|
lx = x;
|
|
|
|
ly = y;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else if (lx !== -1) {
|
2013-06-11 18:13:49 +00:00
|
|
|
if (this.tput) {
|
|
|
|
out += y === ly
|
|
|
|
? this.tput.cuf(x - lx)
|
|
|
|
: this.tput.cup(y, x);
|
|
|
|
} else {
|
|
|
|
out += y === ly
|
|
|
|
? '\x1b[' + (x - lx) + 'C'
|
|
|
|
: '\x1b[' + (y + 1) + ';' + (x + 1) + 'H';
|
|
|
|
}
|
2013-06-06 10:19:52 +00:00
|
|
|
lx = -1, ly = -1;
|
|
|
|
}
|
|
|
|
o[x][0] = data;
|
|
|
|
o[x][1] = ch;
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
if (data !== attr) {
|
2013-06-01 02:09:07 +00:00
|
|
|
if (attr !== this.dattr) {
|
|
|
|
out += '\x1b[m';
|
|
|
|
}
|
|
|
|
if (data !== this.dattr) {
|
|
|
|
out += '\x1b[';
|
|
|
|
|
|
|
|
bgColor = data & 0x1ff;
|
|
|
|
fgColor = (data >> 9) & 0x1ff;
|
|
|
|
flags = data >> 18;
|
|
|
|
|
|
|
|
// bold
|
|
|
|
if (flags & 1) {
|
|
|
|
out += '1;';
|
|
|
|
}
|
|
|
|
|
|
|
|
// underline
|
|
|
|
if (flags & 2) {
|
|
|
|
out += '4;';
|
|
|
|
}
|
|
|
|
|
|
|
|
// blink
|
|
|
|
if (flags & 4) {
|
|
|
|
out += '5;';
|
|
|
|
}
|
|
|
|
|
|
|
|
// inverse
|
|
|
|
if (flags & 8) {
|
|
|
|
out += '7;';
|
|
|
|
}
|
|
|
|
|
|
|
|
// invisible
|
|
|
|
if (flags & 16) {
|
|
|
|
out += '8;';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bgColor !== 0x1ff) {
|
2013-06-11 18:13:49 +00:00
|
|
|
if (bgColor < 16 || (this.tput && this.tput.colors <= 16)) {
|
2013-06-01 02:09:07 +00:00
|
|
|
if (bgColor < 8) {
|
|
|
|
bgColor += 40;
|
|
|
|
} else if (bgColor < 16) {
|
2013-06-06 14:24:03 +00:00
|
|
|
bgColor -= 8;
|
2013-06-01 02:09:07 +00:00
|
|
|
bgColor += 100;
|
|
|
|
}
|
|
|
|
out += bgColor + ';';
|
|
|
|
} else {
|
|
|
|
out += '48;5;' + bgColor + ';';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fgColor !== 0x1ff) {
|
2013-06-11 18:13:49 +00:00
|
|
|
if (fgColor < 16 || (this.tput && this.tput.colors <= 16)) {
|
2013-06-01 02:09:07 +00:00
|
|
|
if (fgColor < 8) {
|
|
|
|
fgColor += 30;
|
|
|
|
} else if (fgColor < 16) {
|
2013-06-06 14:24:03 +00:00
|
|
|
fgColor -= 8;
|
2013-06-01 02:09:07 +00:00
|
|
|
fgColor += 90;
|
|
|
|
}
|
|
|
|
out += fgColor + ';';
|
|
|
|
} else {
|
|
|
|
out += '38;5;' + fgColor + ';';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out[out.length-1] === ';') out = out.slice(0, -1);
|
|
|
|
|
|
|
|
out += 'm';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out += ch;
|
|
|
|
attr = data;
|
|
|
|
}
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
if (attr !== this.dattr) {
|
2013-06-01 02:09:07 +00:00
|
|
|
out += '\x1b[m';
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:13:49 +00:00
|
|
|
if (this.tput) {
|
|
|
|
if (out) this.program.write(this.tput.cup(y, 0) + out);
|
|
|
|
} else {
|
|
|
|
if (out) this.program.write('\x1b[' + (y + 1) + ';1H' + out);
|
|
|
|
}
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.program.restoreCursor();
|
|
|
|
};
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
Screen.prototype.focus = function(offset) {
|
|
|
|
if (!this.input.length || !offset) return;
|
|
|
|
var i = this.input.indexOf(this.focused);
|
|
|
|
if (!~i) return;
|
|
|
|
if (!this.input[i + offset]) {
|
|
|
|
if (offset > 0) {
|
|
|
|
while (offset--) if (++i > this.input.length - 1) i = 0;
|
|
|
|
} else {
|
|
|
|
offset = -offset;
|
|
|
|
while (offset--) if (--i < 0) i = this.input.length - 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i += offset;
|
|
|
|
}
|
|
|
|
return this.input[i].focus();
|
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.focusPrev = function() {
|
|
|
|
return this.focus(-1);
|
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.focusNext = function() {
|
|
|
|
return this.focus(1);
|
|
|
|
};
|
|
|
|
|
2013-06-09 20:52:22 +00:00
|
|
|
Screen.prototype.focusPush = function(el) {
|
|
|
|
if (this.history.length === 10) {
|
|
|
|
this.history.shift();
|
|
|
|
}
|
|
|
|
this.history.push(el);
|
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.focusLast =
|
|
|
|
Screen.prototype.focusPop = function() {
|
|
|
|
return this.history.pop();
|
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.__defineGetter__('focused', function() {
|
|
|
|
return this.history[this.history.length-1];
|
|
|
|
});
|
|
|
|
|
|
|
|
Screen.prototype.__defineSetter__('focused', function(el) {
|
|
|
|
return this.focusPush(el);
|
|
|
|
});
|
|
|
|
|
2013-06-06 10:38:30 +00:00
|
|
|
Screen.prototype.clearRegion = function(xi, xl, yi, yl) {
|
2013-06-09 19:32:29 +00:00
|
|
|
return this.fillRegion(this.dattr, ' ', xi, xl, yi, yl);
|
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.fillRegion = function(attr, ch, xi, xl, yi, yl) {
|
2013-06-06 10:38:30 +00:00
|
|
|
var lines = this.lines
|
2013-06-10 01:25:13 +00:00
|
|
|
, cell
|
2013-06-06 10:38:30 +00:00
|
|
|
, xx;
|
|
|
|
|
|
|
|
for (; yi < yl; yi++) {
|
|
|
|
if (!lines[yi]) break;
|
|
|
|
for (xx = xi; xx < xl; xx++) {
|
|
|
|
cell = lines[yi][xx];
|
|
|
|
if (!cell) break;
|
|
|
|
if (attr !== cell[0] || ch !== cell[1]) {
|
|
|
|
lines[yi][xx][0] = attr;
|
|
|
|
lines[yi][xx][1] = ch;
|
|
|
|
lines[yi].dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Element
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Element(options) {
|
2013-06-11 14:27:50 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
Node.call(this, options);
|
2013-06-09 13:33:23 +00:00
|
|
|
|
2013-06-01 02:39:11 +00:00
|
|
|
this.position = {
|
2013-06-01 02:09:07 +00:00
|
|
|
left: options.left || 0,
|
|
|
|
right: options.right || 0,
|
|
|
|
top: options.top || 0,
|
2013-06-01 02:39:11 +00:00
|
|
|
bottom: options.bottom || 0,
|
|
|
|
width: options.width || null,
|
|
|
|
height: options.height || null
|
2013-06-01 02:09:07 +00:00
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:19 +00:00
|
|
|
// TODO: Possibly add padding/margins?
|
|
|
|
// this.position.padding = options.padding || 0;
|
|
|
|
// this.position.margin = options.margin || 0;
|
|
|
|
|
2013-06-06 10:38:30 +00:00
|
|
|
this.hidden = options.hidden || false;
|
2013-06-13 07:16:32 +00:00
|
|
|
this.fg = convert(options.fg);
|
|
|
|
this.bg = convert(options.bg);
|
2013-06-01 02:09:07 +00:00
|
|
|
this.bold = options.bold ? 1 : 0;
|
|
|
|
this.underline = options.underline ? 2 : 0;
|
2013-06-13 07:22:01 +00:00
|
|
|
this.blink = options.blink ? 4 : 0;
|
|
|
|
this.inverse = options.inverse ? 8 : 0;
|
|
|
|
this.invisible = options.invisible ? 16 : 0;
|
2013-06-01 02:09:07 +00:00
|
|
|
|
|
|
|
this.fixed = options.fixed || false;
|
2013-06-13 09:14:14 +00:00
|
|
|
this.align = options.align || 'left';
|
2013-06-01 02:09:07 +00:00
|
|
|
this.border = options.border;
|
2013-06-01 07:06:04 +00:00
|
|
|
if (this.border) {
|
|
|
|
this.border.type = this.border.type || 'bg';
|
2013-06-13 07:16:32 +00:00
|
|
|
this.border.fg = convert(this.border.fg);
|
|
|
|
this.border.bg = convert(this.border.bg);
|
2013-06-01 07:06:04 +00:00
|
|
|
this.border.ch = this.border.ch || ' ';
|
|
|
|
this.border.bold = this.border.bold ? 1 : 0;
|
|
|
|
this.border.underline = this.border.underline ? 2 : 0;
|
2013-06-13 07:22:01 +00:00
|
|
|
this.border.blink = this.border.blink ? 4 : 0;
|
|
|
|
this.border.inverse = this.border.inverse ? 8 : 0;
|
|
|
|
this.border.invisible = this.border.invisible ? 16 : 0;
|
2013-06-01 07:06:04 +00:00
|
|
|
}
|
2013-06-01 02:09:07 +00:00
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
if (options.clickable) {
|
|
|
|
this.screen._listenMouse(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.input) {
|
|
|
|
this.screen._listenKeys(this);
|
|
|
|
}
|
2013-06-01 02:09:07 +00:00
|
|
|
|
|
|
|
this.content = options.content || '';
|
2013-06-09 20:02:12 +00:00
|
|
|
|
|
|
|
if (options.label) {
|
|
|
|
this.append(new Text({
|
|
|
|
screen: this.screen,
|
|
|
|
content: options.label,
|
|
|
|
left: 2,
|
|
|
|
top: this.border ? 0 : -1
|
|
|
|
}));
|
|
|
|
}
|
2013-06-11 14:27:50 +00:00
|
|
|
|
|
|
|
// TODO: Possibly move this to Node for screen.on('mouse', ...).
|
|
|
|
this.on('newListener', function fn(type) {
|
|
|
|
if (type === 'mouse'
|
|
|
|
|| type === 'click'
|
|
|
|
|| type === 'hover'
|
|
|
|
|| type === 'mousedown'
|
|
|
|
|| type === 'mouseup'
|
|
|
|
|| type === 'mousewheel'
|
|
|
|
|| type === 'wheeldown'
|
|
|
|
|| type === 'wheelup'
|
|
|
|
|| type === 'mousemove'
|
|
|
|
|| type === 'movement') {
|
|
|
|
self.screen._listenMouse(self);
|
|
|
|
} else if (type === 'keypress') {
|
|
|
|
self.screen._listenKeys(self);
|
|
|
|
}
|
|
|
|
});
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Element.prototype.__proto__ = Node.prototype;
|
|
|
|
|
2013-06-09 20:21:58 +00:00
|
|
|
/*
|
|
|
|
Element._emit = Element.prototype.emit;
|
|
|
|
Element.prototype.emit = function(type) {
|
|
|
|
var args = Array.prototype.slice.call(arguments)
|
|
|
|
, ret = Element._emit.apply(this, args);
|
|
|
|
|
|
|
|
if (this.screen) {
|
|
|
|
args.shift();
|
|
|
|
args.unshift(this);
|
|
|
|
args.unshift('element ' + type);
|
|
|
|
this.screen.emit.apply(this.screen, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
2013-06-06 10:38:30 +00:00
|
|
|
Element.prototype.hide = function() {
|
2013-06-12 08:47:35 +00:00
|
|
|
if (this.hidden) return;
|
2013-06-06 10:38:30 +00:00
|
|
|
var ret = this.render(true);
|
|
|
|
this.hidden = true;
|
|
|
|
this.screen.clearRegion(ret.xi, ret.xl, ret.yi, ret.yl);
|
2013-06-11 18:13:49 +00:00
|
|
|
this.emit('hide');
|
2013-06-06 10:38:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.show = function() {
|
2013-06-12 08:47:35 +00:00
|
|
|
if (!this.hidden) return;
|
2013-06-06 10:38:30 +00:00
|
|
|
this.hidden = false;
|
|
|
|
//this.render();
|
2013-06-11 18:13:49 +00:00
|
|
|
this.emit('show');
|
2013-06-06 10:38:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.toggle = function() {
|
|
|
|
return this.hidden ? this.show() : this.hide();
|
|
|
|
};
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
Element.prototype.focus = function() {
|
|
|
|
var old = this.screen.focused;
|
|
|
|
this.screen.focused = this;
|
|
|
|
old.emit('blur', this);
|
|
|
|
this.emit('focus', old);
|
|
|
|
this.screen.emit('element blur', old, this);
|
|
|
|
this.screen.emit('element focus', old, this);
|
|
|
|
};
|
|
|
|
|
2013-06-09 18:14:42 +00:00
|
|
|
Element.prototype.setContent = function(content) {
|
|
|
|
var ret = this.render(true);
|
|
|
|
this.content = content || '';
|
|
|
|
this.screen.clearRegion(
|
|
|
|
ret.xi + (this.border ? 1 : 0),
|
|
|
|
ret.xl - (this.border ? 1 : 0),
|
|
|
|
ret.yi + (this.border ? 1 : 0),
|
|
|
|
ret.yl - (this.border ? 1 : 0));
|
|
|
|
};
|
|
|
|
|
2013-06-11 15:07:04 +00:00
|
|
|
/**
|
|
|
|
* Positioning
|
|
|
|
*/
|
|
|
|
|
|
|
|
// NOTE: When coords are entered in the Element constructor, all of the coords
|
|
|
|
// are *relative* to their parent, when retrieving them from `.left`, `.right`,
|
|
|
|
// etc members, the coords are absolute. To see the *relative* coords again,
|
|
|
|
// use `.rleft`, `.rright`, etc.
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
Element.prototype.__defineGetter__('left', function() {
|
2013-06-01 02:39:11 +00:00
|
|
|
var left = this.position.left;
|
2013-06-01 05:29:31 +00:00
|
|
|
|
|
|
|
if (typeof left === 'string') {
|
|
|
|
if (left === 'center') left = '50%';
|
|
|
|
left = +left.slice(0, -1) / 100;
|
2013-06-11 19:00:18 +00:00
|
|
|
left = this.parent.width * left | 0;
|
2013-06-11 15:07:04 +00:00
|
|
|
if (this.position.left === 'center') {
|
|
|
|
left -= this.width / 2 | 0;
|
|
|
|
}
|
2013-06-01 05:29:31 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
if (this.options.left == null && this.options.right != null) {
|
|
|
|
return this.screen.cols - this.width - this.right;
|
|
|
|
}
|
|
|
|
|
2013-06-01 02:39:11 +00:00
|
|
|
return (this.parent.left || 0) + left;
|
2013-06-01 02:09:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('right', function() {
|
2013-06-11 15:07:04 +00:00
|
|
|
if (this.options.right == null && this.options.left != null) {
|
|
|
|
return this.screen.cols - (this.left + this.width);
|
|
|
|
}
|
2013-06-01 02:39:11 +00:00
|
|
|
return (this.parent.right || 0) + this.position.right;
|
2013-06-01 02:09:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('top', function() {
|
2013-06-01 02:39:11 +00:00
|
|
|
var top = this.position.top;
|
2013-06-01 05:29:31 +00:00
|
|
|
|
|
|
|
if (typeof top === 'string') {
|
|
|
|
if (top === 'center') top = '50%';
|
|
|
|
top = +top.slice(0, -1) / 100;
|
2013-06-11 19:00:18 +00:00
|
|
|
top = this.parent.height * top | 0;
|
2013-06-11 15:07:04 +00:00
|
|
|
if (this.position.top === 'center') {
|
|
|
|
top -= this.height / 2 | 0;
|
|
|
|
}
|
2013-06-01 05:29:31 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
if (this.options.top == null && this.options.bottom != null) {
|
|
|
|
return this.screen.rows - this.height - this.bottom;
|
|
|
|
}
|
|
|
|
|
2013-06-01 02:39:11 +00:00
|
|
|
return (this.parent.top || 0) + top;
|
2013-06-01 02:09:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('bottom', function() {
|
2013-06-11 15:07:04 +00:00
|
|
|
if (this.options.bottom == null && this.options.top != null) {
|
|
|
|
return this.screen.rows - (this.top + this.height);
|
|
|
|
}
|
2013-06-01 02:39:11 +00:00
|
|
|
return (this.parent.bottom || 0) + this.position.bottom;
|
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('width', function() {
|
|
|
|
var width = this.position.width;
|
2013-06-01 05:29:31 +00:00
|
|
|
if (typeof width === 'string') {
|
|
|
|
if (width === 'half') width = '50%';
|
|
|
|
width = +width.slice(0, -1) / 100;
|
|
|
|
return this.parent.width * width | 0;
|
|
|
|
}
|
|
|
|
if (!width) {
|
2013-06-01 10:47:18 +00:00
|
|
|
// Problem if .left is 'center', we can't calculate the width
|
2013-06-11 18:57:06 +00:00
|
|
|
// NOTE: This assume `right` cannot be a string.
|
2013-06-01 10:47:18 +00:00
|
|
|
var left = this.position.left;
|
|
|
|
if (typeof left === 'string') {
|
|
|
|
if (left === 'center') left = '50%';
|
|
|
|
left = +left.slice(0, -1) / 100;
|
|
|
|
left = this.parent.width * left | 0;
|
|
|
|
}
|
|
|
|
width = this.parent.width - this.position.right - left;
|
2013-06-01 05:29:31 +00:00
|
|
|
}
|
2013-06-01 02:39:11 +00:00
|
|
|
return width;
|
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('height', function() {
|
|
|
|
var height = this.position.height;
|
2013-06-01 05:29:31 +00:00
|
|
|
if (typeof height === 'string') {
|
|
|
|
if (height === 'half') height = '50%';
|
|
|
|
height = +height.slice(0, -1) / 100;
|
|
|
|
return this.parent.height * height | 0;
|
|
|
|
}
|
|
|
|
if (!height) {
|
2013-06-01 10:47:18 +00:00
|
|
|
// Problem if .top is 'center', we can't calculate the height
|
2013-06-11 18:57:06 +00:00
|
|
|
// NOTE: This assume `bottom` cannot be a string.
|
2013-06-01 10:47:18 +00:00
|
|
|
var top = this.position.top;
|
|
|
|
if (typeof top === 'string') {
|
|
|
|
if (top === 'center') top = '50%';
|
|
|
|
top = +top.slice(0, -1) / 100;
|
|
|
|
top = this.parent.height * top | 0;
|
|
|
|
}
|
|
|
|
height = this.parent.height - this.position.bottom - top;
|
2013-06-01 05:29:31 +00:00
|
|
|
}
|
2013-06-01 02:39:11 +00:00
|
|
|
return height;
|
2013-06-01 02:09:07 +00:00
|
|
|
});
|
|
|
|
|
2013-06-01 10:47:18 +00:00
|
|
|
Element.prototype.__defineGetter__('rleft', function() {
|
|
|
|
var left = this.position.left;
|
|
|
|
|
|
|
|
if (typeof left === 'string') {
|
|
|
|
if (left === 'center') left = '50%';
|
|
|
|
left = +left.slice(0, -1) / 100;
|
2013-06-11 19:00:18 +00:00
|
|
|
left = this.parent.width * left | 0;
|
2013-06-11 15:07:04 +00:00
|
|
|
if (this.position.left === 'center') {
|
|
|
|
left -= this.width / 2 | 0;
|
|
|
|
}
|
2013-06-01 10:47:18 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
if (this.options.left == null && this.options.right != null) {
|
|
|
|
return this.parent.width - this.width - this.right;
|
|
|
|
}
|
|
|
|
|
2013-06-01 10:47:18 +00:00
|
|
|
return left;
|
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('rright', function() {
|
2013-06-11 15:07:04 +00:00
|
|
|
if (this.options.right == null && this.options.left != null) {
|
|
|
|
return this.parent.width - (this.rleft + this.width);
|
|
|
|
}
|
2013-06-01 10:47:18 +00:00
|
|
|
return this.position.right;
|
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('rtop', function() {
|
|
|
|
var top = this.position.top;
|
|
|
|
|
|
|
|
if (typeof top === 'string') {
|
|
|
|
if (top === 'center') top = '50%';
|
|
|
|
top = +top.slice(0, -1) / 100;
|
2013-06-11 19:00:18 +00:00
|
|
|
top = this.parent.height * top | 0;
|
2013-06-11 15:07:04 +00:00
|
|
|
if (this.position.top === 'center') {
|
|
|
|
top -= this.height / 2 | 0;
|
|
|
|
}
|
2013-06-01 10:47:18 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
if (this.options.top == null && this.options.bottom != null) {
|
|
|
|
return this.parent.height - this.height - this.bottom;
|
|
|
|
}
|
|
|
|
|
2013-06-01 10:47:18 +00:00
|
|
|
return top;
|
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('rbottom', function() {
|
2013-06-11 15:07:04 +00:00
|
|
|
if (this.options.bottom == null && this.options.top != null) {
|
|
|
|
return this.parent.height - (this.rtop + this.height);
|
|
|
|
}
|
2013-06-01 10:47:18 +00:00
|
|
|
return this.position.bottom;
|
|
|
|
});
|
|
|
|
|
2013-06-11 14:09:57 +00:00
|
|
|
// TODO: Reconcile the fact the `position.left` is actually `.rleft`. etc.
|
|
|
|
// TODO: Allow string values for absolute coords below.
|
|
|
|
// TODO: Optimize clearing to only clear what is necessary.
|
|
|
|
|
|
|
|
Element.prototype.__defineSetter__('left', function(val) {
|
2013-06-11 15:07:04 +00:00
|
|
|
if (typeof val === 'string') {
|
|
|
|
if (val === 'center') val = '50%';
|
|
|
|
val = +val.slice(0, -1) / 100;
|
|
|
|
val = this.screen.width * val | 0;
|
|
|
|
}
|
2013-06-12 19:31:48 +00:00
|
|
|
val -= this.parent.left;
|
|
|
|
if (this.position.left === val) return;
|
2013-06-11 14:09:57 +00:00
|
|
|
this.emit('move');
|
2013-06-11 18:13:49 +00:00
|
|
|
this.screen.clearRegion(
|
|
|
|
this.left, this.left + this.width,
|
|
|
|
this.top, this.top + this.height);
|
2013-06-12 19:31:48 +00:00
|
|
|
return this.options.left = this.position.left = val;
|
2013-06-11 14:09:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineSetter__('right', function(val) {
|
2013-06-11 15:07:04 +00:00
|
|
|
if (typeof val === 'string') {
|
|
|
|
if (val === 'center') val = '50%';
|
|
|
|
val = +val.slice(0, -1) / 100;
|
|
|
|
val = this.screen.width * val | 0;
|
|
|
|
}
|
2013-06-12 19:31:48 +00:00
|
|
|
val -= this.parent.right;
|
|
|
|
if (this.position.right === val) return;
|
2013-06-11 14:09:57 +00:00
|
|
|
this.emit('move');
|
|
|
|
this.screen.clearRegion(
|
|
|
|
this.left, this.left + this.width,
|
|
|
|
this.top, this.top + this.height);
|
2013-06-12 19:31:48 +00:00
|
|
|
//if (this.options.right == null) {
|
|
|
|
// return this.options.left = this.position.left = this.screen.width - 1 - val;
|
|
|
|
//}
|
|
|
|
return this.options.right = this.position.right = val;
|
2013-06-11 14:09:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineSetter__('top', function(val) {
|
2013-06-11 15:07:04 +00:00
|
|
|
if (typeof val === 'string') {
|
|
|
|
if (val === 'center') val = '50%';
|
|
|
|
val = +val.slice(0, -1) / 100;
|
|
|
|
val = this.screen.height * val | 0;
|
|
|
|
}
|
2013-06-12 19:31:48 +00:00
|
|
|
val -= this.parent.top;
|
|
|
|
if (this.position.top === val) return;
|
2013-06-11 14:09:57 +00:00
|
|
|
this.emit('move');
|
|
|
|
this.screen.clearRegion(
|
|
|
|
this.left, this.left + this.width,
|
|
|
|
this.top, this.top + this.height);
|
2013-06-12 19:31:48 +00:00
|
|
|
return this.options.top = this.position.top = val;
|
2013-06-11 14:09:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineSetter__('bottom', function(val) {
|
2013-06-11 15:07:04 +00:00
|
|
|
if (typeof val === 'string') {
|
|
|
|
if (val === 'center') val = '50%';
|
|
|
|
val = +val.slice(0, -1) / 100;
|
|
|
|
val = this.screen.height * val | 0;
|
|
|
|
}
|
2013-06-12 19:31:48 +00:00
|
|
|
val -= this.parent.bottom;
|
|
|
|
if (this.position.bottom === val) return;
|
2013-06-11 14:09:57 +00:00
|
|
|
this.emit('move');
|
|
|
|
this.screen.clearRegion(
|
|
|
|
this.left, this.left + this.width,
|
|
|
|
this.top, this.top + this.height);
|
2013-06-12 19:31:48 +00:00
|
|
|
//if (this.options.bottom == null) {
|
|
|
|
// return this.options.top = this.position.top = this.screen.height - 1 - val;
|
|
|
|
//}
|
|
|
|
return this.options.bottom = this.position.bottom = val;
|
2013-06-11 14:09:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineSetter__('width', function(val) {
|
2013-06-12 19:31:48 +00:00
|
|
|
if (this.position.width === val) return;
|
2013-06-11 14:09:57 +00:00
|
|
|
this.emit('resize');
|
|
|
|
this.screen.clearRegion(
|
|
|
|
this.left, this.left + this.width,
|
|
|
|
this.top, this.top + this.height);
|
|
|
|
return this.options.width = this.position.width = val;
|
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineSetter__('height', function(val) {
|
2013-06-12 19:31:48 +00:00
|
|
|
if (this.position.height === val) return;
|
2013-06-11 14:09:57 +00:00
|
|
|
this.emit('resize');
|
|
|
|
this.screen.clearRegion(
|
|
|
|
this.left, this.left + this.width,
|
|
|
|
this.top, this.top + this.height);
|
|
|
|
return this.options.height = this.position.height = val;
|
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineSetter__('rleft', function(val) {
|
2013-06-12 19:31:48 +00:00
|
|
|
if (this.position.left === val) return;
|
2013-06-11 14:09:57 +00:00
|
|
|
this.emit('move');
|
|
|
|
this.screen.clearRegion(
|
|
|
|
this.left, this.left + this.width,
|
|
|
|
this.top, this.top + this.height);
|
|
|
|
return this.options.left = this.position.left = val;
|
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineSetter__('rright', function(val) {
|
2013-06-12 19:31:48 +00:00
|
|
|
if (this.position.right === val) return;
|
2013-06-11 14:09:57 +00:00
|
|
|
this.emit('move');
|
|
|
|
this.screen.clearRegion(
|
|
|
|
this.left, this.left + this.width,
|
|
|
|
this.top, this.top + this.height);
|
2013-06-12 19:31:48 +00:00
|
|
|
//if (this.options.right == null) {
|
|
|
|
// return this.options.left = this.position.left = this.parent.width - 1 - val;
|
|
|
|
//}
|
2013-06-11 14:09:57 +00:00
|
|
|
return this.options.right = this.position.right = val;
|
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineSetter__('rtop', function(val) {
|
2013-06-12 19:31:48 +00:00
|
|
|
if (this.position.top === val) return;
|
2013-06-11 14:09:57 +00:00
|
|
|
this.emit('move');
|
|
|
|
this.screen.clearRegion(
|
|
|
|
this.left, this.left + this.width,
|
|
|
|
this.top, this.top + this.height);
|
|
|
|
return this.options.top = this.position.top = val;
|
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineSetter__('rbottom', function(val) {
|
2013-06-12 19:31:48 +00:00
|
|
|
if (this.position.bottom === val) return;
|
2013-06-11 14:09:57 +00:00
|
|
|
this.emit('move');
|
|
|
|
this.screen.clearRegion(
|
|
|
|
this.left, this.left + this.width,
|
|
|
|
this.top, this.top + this.height);
|
2013-06-12 19:31:48 +00:00
|
|
|
//if (this.options.bottom == null) {
|
|
|
|
// return this.options.top = this.position.top = this.parent.height - 1 - val;
|
|
|
|
//}
|
2013-06-11 14:09:57 +00:00
|
|
|
return this.options.bottom = this.position.bottom = val;
|
|
|
|
});
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Box
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Box(options) {
|
|
|
|
Element.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Box.prototype.__proto__ = Element.prototype;
|
|
|
|
|
2013-06-06 10:38:30 +00:00
|
|
|
Box.prototype.render = function(stop) {
|
|
|
|
// NOTE: Maybe move this `hidden` check down below `stop` check and return `ret`.
|
|
|
|
if (this.hidden) return;
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
var lines = this.screen.lines
|
2013-06-13 09:14:14 +00:00
|
|
|
, xi_ = this.left
|
|
|
|
, xi
|
2013-06-01 02:09:07 +00:00
|
|
|
, xl = this.screen.cols - this.right
|
|
|
|
, yi = this.top
|
|
|
|
, yl = this.screen.rows - this.bottom
|
|
|
|
, cell
|
|
|
|
, attr
|
|
|
|
, ch
|
2013-06-11 20:01:52 +00:00
|
|
|
, content = this._pcontent || this.content
|
2013-06-07 10:58:00 +00:00
|
|
|
, ci = this.contentIndex || 0
|
2013-06-11 20:01:52 +00:00
|
|
|
, cl = content.length
|
2013-06-06 15:10:55 +00:00
|
|
|
, battr
|
|
|
|
, dattr
|
|
|
|
, c;
|
2013-06-01 02:09:07 +00:00
|
|
|
|
2013-06-01 02:39:11 +00:00
|
|
|
if (this.position.width) {
|
2013-06-13 09:14:14 +00:00
|
|
|
xl = xi_ + this.width;
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
2013-06-01 02:39:11 +00:00
|
|
|
if (this.position.height) {
|
2013-06-01 05:29:31 +00:00
|
|
|
yl = yi + this.height;
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
if (this.parent.childBase != null && ~this.parent.items.indexOf(this)) {
|
|
|
|
var rtop = this.rtop - (this.parent.border ? 1 : 0)
|
|
|
|
, visible = this.parent.height - (this.parent.border ? 2 : 0);
|
|
|
|
|
|
|
|
yi -= this.parent.childBase;
|
2013-06-13 09:14:14 +00:00
|
|
|
yl = Math.min(yl, this.screen.rows - this.parent.bottom - (this.parent.border ? 1 : 0));
|
2013-06-06 09:03:25 +00:00
|
|
|
|
|
|
|
if (rtop - this.parent.childBase < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (rtop - this.parent.childBase >= visible) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 09:14:14 +00:00
|
|
|
if (this.align === 'center' || this.align === 'right') {
|
|
|
|
var width = this.width - (this.border ? 2 : 0);
|
|
|
|
var ncontent = content.split('\n')[0].replace(/\x1b\[[^m]*m/g, '');
|
|
|
|
if (ncontent.length < width) {
|
|
|
|
var s = width - 1 - ncontent.length - 1;
|
|
|
|
if (this.align === 'center') {
|
|
|
|
//xi_ += s / 2 | 0;
|
|
|
|
content = Array(s / 2 | 0).join(' ') + content;
|
|
|
|
} else {
|
|
|
|
//xi_ += s;
|
|
|
|
content = Array(s).join(' ') + content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
var ret = {
|
2013-06-13 09:14:14 +00:00
|
|
|
xi: xi_,
|
2013-06-06 09:03:25 +00:00
|
|
|
xl: xl,
|
|
|
|
yi: yi,
|
|
|
|
yl: yl
|
|
|
|
};
|
|
|
|
|
2013-06-06 10:38:30 +00:00
|
|
|
if (stop) return ret;
|
|
|
|
|
2013-06-06 15:10:55 +00:00
|
|
|
battr = this.border
|
2013-06-13 07:16:32 +00:00
|
|
|
? sattr(this.border, this.border.fg, this.border.bg)
|
2013-06-06 14:24:03 +00:00
|
|
|
: 0;
|
|
|
|
|
2013-06-06 15:10:55 +00:00
|
|
|
//if (this.escapes) dattr = this.screen.dattr;
|
2013-06-13 07:16:32 +00:00
|
|
|
dattr = sattr(this, this.fg, this.bg);
|
2013-06-06 14:24:03 +00:00
|
|
|
attr = dattr;
|
2013-06-06 12:57:50 +00:00
|
|
|
|
2013-06-10 01:19:32 +00:00
|
|
|
// Check previous line for escape codes.
|
2013-06-11 20:01:52 +00:00
|
|
|
if (this.childBase > 0 && this._clines) {
|
|
|
|
var cci = ci - (this._clines[this.childBase - 1].length + 1);
|
2013-06-10 01:19:32 +00:00
|
|
|
for (; cci < ci; cci++) {
|
2013-06-11 20:01:52 +00:00
|
|
|
if (content[cci] === '\x1b') {
|
|
|
|
if (c = /^\x1b\[(?:\d+(?:;\d+)*)?m/.exec(content.substring(cci))) {
|
2013-06-10 01:19:32 +00:00
|
|
|
attr = attrCode(c[0], attr);
|
|
|
|
cci += c[0].length - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
for (; yi < yl; yi++) {
|
2013-06-06 09:33:10 +00:00
|
|
|
if (!lines[yi]) break;
|
2013-06-13 09:14:14 +00:00
|
|
|
for (xi = xi_; xi < xl; xi++) {
|
2013-06-06 09:03:25 +00:00
|
|
|
cell = lines[yi][xi];
|
2013-06-06 09:33:10 +00:00
|
|
|
if (!cell) break;
|
2013-06-06 14:24:03 +00:00
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
if (this.border && (yi === this.top || xi === this.left || yi === yl - 1 || xi === xl - 1)) {
|
|
|
|
if (this.border.type === 'ascii') {
|
|
|
|
if (yi === this.top) {
|
|
|
|
if (xi === this.left) ch = '┌';
|
|
|
|
else if (xi === xl - 1) ch = '┐';
|
|
|
|
else ch = '─';
|
2013-06-01 05:29:31 +00:00
|
|
|
} else if (yi === yl - 1) {
|
2013-06-01 02:09:07 +00:00
|
|
|
if (xi === this.left) ch = '└';
|
|
|
|
else if (xi === xl - 1) ch = '┘';
|
|
|
|
else ch = '─';
|
|
|
|
} else if (xi === this.left || xi === xl - 1) {
|
|
|
|
ch = '│';
|
|
|
|
}
|
|
|
|
} else if (this.border.type === 'bg') {
|
2013-06-01 07:06:04 +00:00
|
|
|
ch = this.border.ch;
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
2013-06-06 14:24:03 +00:00
|
|
|
if (battr !== cell[0] || ch !== cell[1]) {
|
|
|
|
lines[yi][xi][0] = battr;
|
|
|
|
lines[yi][xi][1] = ch;
|
|
|
|
lines[yi].dirty = true;
|
2013-06-06 12:57:50 +00:00
|
|
|
}
|
2013-06-06 14:24:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-06-11 20:01:52 +00:00
|
|
|
ch = content[ci++] || ' ';
|
2013-06-06 14:24:03 +00:00
|
|
|
|
|
|
|
// Handle escape codes.
|
|
|
|
while (ch === '\x1b') {
|
2013-06-11 20:01:52 +00:00
|
|
|
if (c = /^\x1b\[(?:\d+(?:;\d+)*)?m/.exec(content.substring(ci - 1))) {
|
2013-06-06 14:24:03 +00:00
|
|
|
ci += c[0].length - 1;
|
|
|
|
attr = attrCode(c[0], attr);
|
2013-06-11 20:01:52 +00:00
|
|
|
ch = content[ci] || ' ';
|
2013-06-06 14:24:03 +00:00
|
|
|
ci++;
|
2013-06-06 12:57:50 +00:00
|
|
|
}
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
2013-06-06 09:33:10 +00:00
|
|
|
|
2013-06-06 12:57:50 +00:00
|
|
|
// Handle newlines.
|
2013-06-06 14:24:03 +00:00
|
|
|
if (ch === '\t') ch = ' ';
|
2013-06-06 12:57:50 +00:00
|
|
|
if (ch === '\n' || ch === '\r') {
|
|
|
|
ch = ' ';
|
|
|
|
var xxl = xl - (this.border ? 1 : 0);
|
|
|
|
for (; xi < xxl; xi++) {
|
|
|
|
cell = lines[yi][xi];
|
|
|
|
if (!cell) break;
|
|
|
|
if (attr !== cell[0] || ch !== cell[1]) {
|
|
|
|
lines[yi][xi][0] = attr;
|
|
|
|
lines[yi][xi][1] = ch;
|
|
|
|
lines[yi].dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.border) xi--;
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-06 09:33:10 +00:00
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
if (attr !== cell[0] || ch !== cell[1]) {
|
|
|
|
lines[yi][xi][0] = attr;
|
|
|
|
lines[yi][xi][1] = ch;
|
|
|
|
lines[yi].dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.children.forEach(function(el) {
|
|
|
|
el.render();
|
|
|
|
});
|
2013-06-01 11:08:18 +00:00
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
return ret;
|
2013-06-01 02:09:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Text
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Text(options) {
|
|
|
|
Element.call(this, options);
|
2013-06-01 07:06:04 +00:00
|
|
|
this.full = options.full;
|
2013-06-13 09:14:14 +00:00
|
|
|
this.align = options.align || 'left';
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Text.prototype.__proto__ = Element.prototype;
|
|
|
|
|
2013-06-06 15:10:55 +00:00
|
|
|
// TODO: Remove duplication of code by reusing box.render
|
|
|
|
// better. Possibly remove Text altogether.
|
|
|
|
// TODO: Maybe just remove escape code and newline handling from here.
|
2013-06-06 10:38:30 +00:00
|
|
|
Text.prototype.render = function(stop) {
|
|
|
|
// NOTE: Maybe move this `hidden` check down below `stop` check and return `ret`.
|
|
|
|
if (this.hidden) return;
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
var lines = this.screen.lines
|
2013-06-13 09:14:14 +00:00
|
|
|
, xi_ = this.left
|
|
|
|
, xi
|
2013-06-01 02:09:07 +00:00
|
|
|
, xl = this.screen.cols - this.right
|
|
|
|
, yi = this.top
|
|
|
|
, yl = this.screen.rows - this.bottom
|
|
|
|
, cell
|
|
|
|
, attr
|
|
|
|
, ch
|
2013-06-11 20:01:52 +00:00
|
|
|
, content = this._pcontent || this.content
|
2013-06-07 10:58:00 +00:00
|
|
|
, ci = this.contentIndex || 0
|
2013-06-11 20:01:52 +00:00
|
|
|
, cl = content.length
|
2013-06-06 15:10:55 +00:00
|
|
|
, ended = -1
|
|
|
|
, dattr
|
|
|
|
, c;
|
2013-06-01 02:09:07 +00:00
|
|
|
|
2013-06-01 02:39:11 +00:00
|
|
|
if (this.position.width) {
|
2013-06-13 09:14:14 +00:00
|
|
|
xl = xi_ + this.width;
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
2013-06-01 02:39:11 +00:00
|
|
|
if (this.position.height) {
|
2013-06-01 07:06:04 +00:00
|
|
|
yl = yi + this.height;
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
2013-06-01 10:47:18 +00:00
|
|
|
if (this.full) {
|
2013-06-13 09:14:14 +00:00
|
|
|
//xl -= this.parent.border ? 2 : 0;
|
2013-06-01 10:47:18 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 01:06:58 +00:00
|
|
|
if (this.parent.childBase != null && ~this.parent.items.indexOf(this)) {
|
2013-06-03 03:29:26 +00:00
|
|
|
var rtop = this.rtop - (this.parent.border ? 1 : 0)
|
|
|
|
, visible = this.parent.height - (this.parent.border ? 2 : 0);
|
|
|
|
|
2013-06-04 01:06:58 +00:00
|
|
|
yi -= this.parent.childBase;
|
2013-06-13 09:14:14 +00:00
|
|
|
yl = Math.min(yl, this.screen.rows - this.parent.bottom - (this.parent.border ? 1 : 0));
|
2013-06-04 01:06:58 +00:00
|
|
|
|
|
|
|
if (rtop - this.parent.childBase < 0) {
|
2013-06-03 03:29:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-06-04 01:06:58 +00:00
|
|
|
if (rtop - this.parent.childBase >= visible) {
|
2013-06-03 03:29:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-06-01 07:06:04 +00:00
|
|
|
|
2013-06-13 09:14:14 +00:00
|
|
|
if (this.align === 'center' || this.align === 'right') {
|
|
|
|
var width = this.width - (this.border ? 2 : 0);
|
|
|
|
var ncontent = content.split('\n')[0].replace(/\x1b\[[^m]*m/g, '');
|
|
|
|
if (ncontent.length < width) {
|
|
|
|
var s = width - 1 - ncontent.length - 1;
|
|
|
|
if (this.align === 'center') {
|
|
|
|
//xi_ += s / 2 | 0;
|
|
|
|
content = Array(s / 2 | 0).join(' ') + content;
|
|
|
|
} else {
|
|
|
|
//xi_ += s;
|
|
|
|
content = Array(s).join(' ') + content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
var ret = {
|
2013-06-13 09:14:14 +00:00
|
|
|
xi: xi_,
|
2013-06-06 09:03:25 +00:00
|
|
|
xl: xl,
|
|
|
|
yi: yi,
|
|
|
|
yl: yl
|
|
|
|
};
|
|
|
|
|
2013-06-06 10:38:30 +00:00
|
|
|
if (stop) return ret;
|
|
|
|
|
2013-06-13 07:16:32 +00:00
|
|
|
dattr = sattr(this, this.fg, this.bg);
|
2013-06-06 15:10:55 +00:00
|
|
|
attr = dattr;
|
2013-06-06 09:03:25 +00:00
|
|
|
|
2013-06-01 11:08:18 +00:00
|
|
|
for (; yi < yl; yi++) {
|
2013-06-06 09:33:10 +00:00
|
|
|
if (!lines[yi]) break;
|
2013-06-13 09:14:14 +00:00
|
|
|
for (xi = xi_; xi < xl; xi++) {
|
2013-06-01 02:09:07 +00:00
|
|
|
cell = lines[yi][xi];
|
2013-06-06 09:33:10 +00:00
|
|
|
if (!cell) break;
|
2013-06-06 15:10:55 +00:00
|
|
|
|
2013-06-11 20:01:52 +00:00
|
|
|
ch = content[ci++];
|
2013-06-06 15:10:55 +00:00
|
|
|
|
|
|
|
// Handle escape codes.
|
|
|
|
while (ch === '\x1b') {
|
2013-06-11 20:01:52 +00:00
|
|
|
if (c = /^\x1b\[(?:\d+(?:;\d+)*)?m/.exec(content.substring(ci - 1))) {
|
2013-06-06 15:10:55 +00:00
|
|
|
ci += c[0].length - 1;
|
|
|
|
attr = attrCode(c[0], attr);
|
2013-06-11 20:01:52 +00:00
|
|
|
ch = content[ci];
|
2013-06-06 15:10:55 +00:00
|
|
|
ci++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle newlines.
|
|
|
|
if (ch === '\t') ch = ' ';
|
|
|
|
if (ch === '\n' || ch === '\r') {
|
|
|
|
ch = ' ';
|
|
|
|
var xxl = xl;
|
|
|
|
for (; xi < xxl; xi++) {
|
|
|
|
cell = lines[yi][xi];
|
|
|
|
if (!cell) break;
|
|
|
|
if (attr !== cell[0] || ch !== cell[1]) {
|
|
|
|
lines[yi][xi][0] = attr;
|
|
|
|
lines[yi][xi][1] = ch;
|
|
|
|
lines[yi].dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-06-01 07:06:04 +00:00
|
|
|
if (!ch) {
|
|
|
|
if (this.full) {
|
|
|
|
if (ended === -1) ended = yi;
|
|
|
|
if (yi === ended) {
|
|
|
|
ch = ' ';
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-06-06 09:33:10 +00:00
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
if (attr !== cell[0] || ch !== cell[1]) {
|
|
|
|
lines[yi][xi][0] = attr;
|
|
|
|
lines[yi][xi][1] = ch;
|
|
|
|
lines[yi].dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-06 09:03:25 +00:00
|
|
|
|
|
|
|
return ret;
|
2013-06-01 02:09:07 +00:00
|
|
|
};
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
/**
|
|
|
|
* Line
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Line(options) {
|
|
|
|
var orientation = options.orientation || 'vertical';
|
|
|
|
delete options.orientation;
|
|
|
|
|
|
|
|
if (orientation === 'vertical') {
|
|
|
|
options.width = 1;
|
|
|
|
} else {
|
|
|
|
options.height = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.border = {
|
|
|
|
type: 'bg',
|
2013-06-13 07:16:32 +00:00
|
|
|
bg: convert(options.bg),
|
|
|
|
fg: convert(options.fg),
|
2013-06-06 09:03:25 +00:00
|
|
|
ch: !options.type || options.type === 'ascii'
|
|
|
|
? orientation === 'horizontal' ? '─' : '│'
|
|
|
|
: options.ch || ' '
|
|
|
|
};
|
|
|
|
|
|
|
|
delete options.bg;
|
|
|
|
delete options.fg;
|
|
|
|
delete options.ch;
|
|
|
|
|
|
|
|
Box.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Line.prototype.__proto__ = Box.prototype;
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* ScrollableBox
|
|
|
|
*/
|
|
|
|
|
|
|
|
function ScrollableBox(options) {
|
|
|
|
Box.call(this, options);
|
2013-06-06 09:03:25 +00:00
|
|
|
this.scrollable = true;
|
2013-06-01 02:09:07 +00:00
|
|
|
this.childOffset = 0;
|
2013-06-03 03:29:26 +00:00
|
|
|
this.childBase = 0;
|
2013-06-06 09:03:25 +00:00
|
|
|
this.baseLimit = options.baseLimit || Infinity;
|
|
|
|
this.alwaysScroll = options.alwaysScroll;
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ScrollableBox.prototype.__proto__ = Box.prototype;
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
ScrollableBox.prototype.scroll = function(offset) {
|
|
|
|
var visible = this.height - (this.border ? 2 : 0);
|
|
|
|
if (this.alwaysScroll) {
|
|
|
|
// Semi-workaround
|
|
|
|
this.childOffset = offset > 0
|
|
|
|
? visible - 1 + offset
|
|
|
|
: offset;
|
|
|
|
} else {
|
|
|
|
this.childOffset += offset;
|
|
|
|
}
|
|
|
|
if (this.childOffset > visible - 1) {
|
|
|
|
var d = this.childOffset - (visible - 1);
|
|
|
|
this.childOffset -= d;
|
|
|
|
this.childBase += d;
|
|
|
|
} else if (this.childOffset < 0) {
|
|
|
|
var d = this.childOffset;
|
|
|
|
this.childOffset += -d;
|
|
|
|
this.childBase += d;
|
|
|
|
}
|
|
|
|
if (this.childBase < 0) this.childBase = 0;
|
|
|
|
else if (this.childBase > this.baseLimit) this.childBase = this.baseLimit;
|
2013-06-11 16:48:37 +00:00
|
|
|
this.emit('scroll');
|
2013-06-06 09:03:25 +00:00
|
|
|
};
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* List
|
|
|
|
*/
|
|
|
|
|
|
|
|
function List(options) {
|
2013-06-06 09:03:25 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
ScrollableBox.call(this, options);
|
2013-06-01 10:47:18 +00:00
|
|
|
|
|
|
|
this.items = [];
|
2013-06-01 02:09:07 +00:00
|
|
|
this.selected = 0;
|
|
|
|
|
2013-06-13 07:16:32 +00:00
|
|
|
this.selectedBg = convert(options.selectedBg);
|
|
|
|
this.selectedFg = convert(options.selectedFg);
|
2013-06-01 07:06:04 +00:00
|
|
|
this.selectedBold = options.selectedBold ? 1 : 0;
|
|
|
|
this.selectedUnderline = options.selectedUnderline ? 2 : 0;
|
2013-06-13 07:22:01 +00:00
|
|
|
this.selectedBlink = options.selectedBlink ? 4 : 0;
|
|
|
|
this.selectedInverse = options.selectedInverse ? 8 : 0;
|
|
|
|
this.selectedInvisible = options.selectedInvisible ? 16 : 0;
|
2013-06-01 02:09:07 +00:00
|
|
|
|
2013-06-06 10:39:44 +00:00
|
|
|
this.mouse = options.mouse || false;
|
2013-06-06 09:03:25 +00:00
|
|
|
|
2013-06-01 07:06:04 +00:00
|
|
|
if (options.items) {
|
|
|
|
options.items.forEach(this.add.bind(this));
|
2013-06-01 10:47:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.children.length) {
|
2013-06-09 13:33:23 +00:00
|
|
|
// Will throw if this.parent is not set!
|
|
|
|
// Probably not good to have in a constructor.
|
|
|
|
// this.select(0);
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
2013-06-06 15:27:19 +00:00
|
|
|
|
|
|
|
if (this.mouse) {
|
|
|
|
self.on('wheeldown', function(data) {
|
|
|
|
self.select(self.selected + 2);
|
|
|
|
self.screen.render();
|
|
|
|
});
|
|
|
|
|
|
|
|
self.on('wheelup', function(data) {
|
|
|
|
self.select(self.selected - 2);
|
|
|
|
self.screen.render();
|
|
|
|
});
|
|
|
|
}
|
2013-06-11 16:48:37 +00:00
|
|
|
|
|
|
|
// self.on('keypress', function(ch, key) {
|
2013-06-11 18:13:49 +00:00
|
|
|
// if (key.name === 'enter') {
|
|
|
|
// self.emit('select', self.items[self.selected], self.selected);
|
|
|
|
// }
|
2013-06-11 16:48:37 +00:00
|
|
|
// });
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
List.prototype.__proto__ = ScrollableBox.prototype;
|
|
|
|
|
|
|
|
List.prototype.add = function(item) {
|
2013-06-06 09:03:25 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-06-06 13:14:25 +00:00
|
|
|
// TODO: Use box here and get rid of text.
|
2013-06-13 09:14:14 +00:00
|
|
|
var item = new Box({
|
2013-06-01 02:09:07 +00:00
|
|
|
screen: this.screen,
|
|
|
|
parent: this,
|
2013-06-01 10:47:18 +00:00
|
|
|
fg: this.fg,
|
|
|
|
bg: this.bg,
|
2013-06-03 03:29:26 +00:00
|
|
|
content: item.content || item,
|
2013-06-13 09:14:14 +00:00
|
|
|
align: this.align || 'left',
|
2013-06-04 01:06:58 +00:00
|
|
|
top: this.items.length + (this.border ? 1 : 0),
|
2013-06-01 10:47:18 +00:00
|
|
|
left: (this.border ? 1 : 0) + 1,
|
2013-06-13 09:14:14 +00:00
|
|
|
right: (this.border ? 1 : 0) + 1,
|
2013-06-01 10:47:18 +00:00
|
|
|
full: true,
|
|
|
|
height: 1
|
|
|
|
});
|
2013-06-06 09:03:25 +00:00
|
|
|
|
2013-06-01 10:47:18 +00:00
|
|
|
this.append(item);
|
|
|
|
this.items.push(item);
|
2013-06-06 09:03:25 +00:00
|
|
|
|
2013-06-06 15:27:19 +00:00
|
|
|
if (this.mouse) {
|
|
|
|
item.on('click', function(data) {
|
|
|
|
self.select(item);
|
|
|
|
self.screen.render();
|
|
|
|
});
|
|
|
|
}
|
2013-06-01 10:47:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
List.prototype._remove = List.prototype.remove;
|
|
|
|
List.prototype.remove = function(child) {
|
|
|
|
if (typeof child === 'number') {
|
|
|
|
child = this.children[child];
|
|
|
|
}
|
|
|
|
var i = this.items.indexOf(child);
|
|
|
|
if (~i) this.items.splice(i, 1);
|
|
|
|
this._remove(child);
|
2013-06-01 02:09:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
List.prototype.select = function(index) {
|
2013-06-01 10:47:18 +00:00
|
|
|
if (!this.items.length) return;
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
if (typeof index === 'object') {
|
2013-06-01 10:47:18 +00:00
|
|
|
index = this.items.indexOf(index);
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
2013-06-01 10:47:18 +00:00
|
|
|
|
|
|
|
if (index < 0) index = 0;
|
|
|
|
else if (index >= this.items.length) index = this.items.length - 1;
|
|
|
|
|
|
|
|
if (this.selected === index && this._listInitialized) return;
|
|
|
|
this._listInitialized = true;
|
|
|
|
|
2013-06-13 07:26:09 +00:00
|
|
|
['bg', 'fg', 'bold', 'underline',
|
|
|
|
'blink', 'inverse', 'invisible'].forEach(function(name) {
|
|
|
|
this.items[this.selected][name] = this[name];
|
|
|
|
this.items[index][name] = this['selected'
|
|
|
|
+ name.substring(0, 1).toUpperCase()
|
|
|
|
+ name.substring(1)];
|
|
|
|
}, this);
|
2013-06-13 07:22:01 +00:00
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
var diff = index - this.selected;
|
|
|
|
this.selected = index;
|
|
|
|
this.scroll(diff);
|
2013-06-01 02:09:07 +00:00
|
|
|
};
|
|
|
|
|
2013-06-01 10:47:18 +00:00
|
|
|
List.prototype.move = function(offset) {
|
|
|
|
this.select(this.selected + offset);
|
2013-06-01 02:09:07 +00:00
|
|
|
};
|
|
|
|
|
2013-06-01 10:47:18 +00:00
|
|
|
List.prototype.up = function(offset) {
|
|
|
|
this.move(-(offset || 1));
|
|
|
|
};
|
|
|
|
|
|
|
|
List.prototype.down = function(offset) {
|
|
|
|
this.move(offset || 1);
|
|
|
|
};
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
/**
|
|
|
|
* ScrollableText
|
|
|
|
*/
|
|
|
|
|
|
|
|
function ScrollableText(options) {
|
2013-06-11 14:09:57 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
options.alwaysScroll = true;
|
2013-06-11 14:09:57 +00:00
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
ScrollableBox.call(this, options);
|
2013-06-06 10:39:44 +00:00
|
|
|
|
|
|
|
if (options.mouse) {
|
|
|
|
var self = this;
|
|
|
|
this.on('wheeldown', function(data) {
|
|
|
|
self.scroll(self.height / 2 | 0 || 1);
|
|
|
|
self.screen.render();
|
|
|
|
});
|
|
|
|
this.on('wheelup', function(data) {
|
|
|
|
self.scroll(-(self.height / 2 | 0) || -1);
|
|
|
|
self.screen.render();
|
|
|
|
});
|
|
|
|
}
|
2013-06-07 10:58:00 +00:00
|
|
|
|
2013-06-11 14:09:57 +00:00
|
|
|
this.screen.on('resize', function() {
|
2013-06-11 19:30:36 +00:00
|
|
|
self._recalculateIndex();
|
2013-06-11 14:09:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.on('resize', function() {
|
2013-06-11 19:30:36 +00:00
|
|
|
self._recalculateIndex();
|
2013-06-11 14:09:57 +00:00
|
|
|
});
|
|
|
|
|
2013-06-11 20:01:52 +00:00
|
|
|
//this._clines = [];
|
|
|
|
//this._pcontent = '';
|
2013-06-07 10:58:00 +00:00
|
|
|
this.contentIndex = 0;
|
2013-06-11 20:01:52 +00:00
|
|
|
|
|
|
|
// Not technically necessary, but can't hurt.
|
|
|
|
this._recalculateIndex();
|
|
|
|
|
|
|
|
if (!this.parent) {
|
|
|
|
// Not technically necessary, but can't hurt.
|
|
|
|
this.once('reparent', function() {
|
|
|
|
self._recalculateIndex();
|
|
|
|
});
|
|
|
|
}
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ScrollableText.prototype.__proto__ = ScrollableBox.prototype;
|
|
|
|
|
2013-06-07 10:58:00 +00:00
|
|
|
ScrollableText.prototype._scroll = ScrollableText.prototype.scroll;
|
|
|
|
ScrollableText.prototype.scroll = function(offset) {
|
2013-06-09 13:33:23 +00:00
|
|
|
var base = this.childBase
|
|
|
|
, ret = this._scroll(offset)
|
2013-06-10 01:19:32 +00:00
|
|
|
, cb = this.childBase
|
|
|
|
, diff = cb - base
|
|
|
|
, w
|
|
|
|
, i
|
|
|
|
, max
|
|
|
|
, t;
|
2013-06-07 10:58:00 +00:00
|
|
|
|
2013-06-10 01:25:13 +00:00
|
|
|
if (diff === 0) return ret;
|
2013-06-09 13:33:23 +00:00
|
|
|
|
|
|
|
// When scrolling text, we want to be able to handle SGR codes as well as line
|
|
|
|
// feeds. This allows us to take preformatted text output from other programs
|
|
|
|
// and put it in a scrollable text box.
|
2013-06-10 01:25:13 +00:00
|
|
|
// TODO: Move this into a separate function and call it from the constructor
|
|
|
|
// and setContent.
|
2013-06-11 20:01:52 +00:00
|
|
|
// TODO: Possibly map _clines into line lengths only to save memory.
|
2013-06-07 10:58:00 +00:00
|
|
|
if (this.content != null) {
|
2013-06-10 01:19:32 +00:00
|
|
|
w = this.width - (this.border ? 2 : 0);
|
2013-06-11 20:01:52 +00:00
|
|
|
if (this._clines == null || this._clines.width !== w) {
|
|
|
|
this._clines = wrapContent(this.content, w);
|
|
|
|
this._pcontent = this._clines.join('\n');
|
2013-06-09 13:33:23 +00:00
|
|
|
}
|
2013-06-07 10:58:00 +00:00
|
|
|
|
2013-06-11 20:01:52 +00:00
|
|
|
max = this._clines.length - 1 - (this.height - (this.border ? 2 : 0));
|
2013-06-10 01:25:13 +00:00
|
|
|
if (cb > max) this.childBase = cb = max;
|
2013-06-10 01:19:32 +00:00
|
|
|
|
|
|
|
if (diff > 0) {
|
2013-06-11 20:01:52 +00:00
|
|
|
for (i = base; i < cb; i++) this.contentIndex += this._clines[i].length + 1;
|
2013-06-10 01:19:32 +00:00
|
|
|
} else {
|
2013-06-11 20:01:52 +00:00
|
|
|
for (i = base - 1; i >= cb; i--) this.contentIndex -= this._clines[i].length + 1;
|
2013-06-09 13:33:23 +00:00
|
|
|
}
|
2013-06-07 10:58:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2013-06-10 02:32:24 +00:00
|
|
|
ScrollableText.prototype._setContent = ScrollableText.prototype.setContent;
|
2013-06-11 14:09:57 +00:00
|
|
|
ScrollableText.prototype.setContent = function(content) {
|
2013-06-10 02:32:24 +00:00
|
|
|
var ret = this._setContent(content);
|
2013-06-11 20:01:52 +00:00
|
|
|
this._recalculateIndex();
|
2013-06-11 19:30:36 +00:00
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2013-06-11 20:01:52 +00:00
|
|
|
ScrollableText.prototype._recalculateIndex = function() {
|
|
|
|
if (!this.parent) return;
|
|
|
|
this._clines = wrapContent(this.content, this.width - (this.border ? 2 : 0));
|
|
|
|
this._pcontent = this._clines.join('\n');
|
2013-06-10 02:32:24 +00:00
|
|
|
|
2013-06-11 14:09:57 +00:00
|
|
|
for (var i = 0, t = 0; i < this.childBase; i++) {
|
2013-06-11 20:01:52 +00:00
|
|
|
t += this._clines[i].length + 1;
|
2013-06-11 14:09:57 +00:00
|
|
|
}
|
|
|
|
this.contentIndex = t;
|
2013-06-10 02:32:24 +00:00
|
|
|
};
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Input
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Input(options) {
|
|
|
|
Box.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Input.prototype.__proto__ = Box.prototype;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Textbox
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Textbox(options) {
|
|
|
|
Input.call(this, options);
|
2013-06-09 20:02:12 +00:00
|
|
|
this.screen._listenKeys(this);
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Textbox.prototype.__proto__ = Input.prototype;
|
|
|
|
|
2013-06-09 18:14:42 +00:00
|
|
|
Textbox.prototype.setInput = function(callback) {
|
|
|
|
var self = this;
|
|
|
|
|
2013-06-09 20:02:12 +00:00
|
|
|
this.screen.grabKeys = true;
|
2013-06-09 18:14:42 +00:00
|
|
|
|
|
|
|
this.focus();
|
2013-06-09 19:34:13 +00:00
|
|
|
// this.screen.program.saveCursor();
|
|
|
|
this.screen.program.cup(
|
|
|
|
this.top + 1 + (this.border ? 1 : 0),
|
|
|
|
this.left + 1 + (this.border ? 1 : 0));
|
2013-06-09 18:14:42 +00:00
|
|
|
this.screen.program.showCursor();
|
|
|
|
this.screen.program.sgr('normal');
|
|
|
|
|
|
|
|
this._callback = function(err, value) {
|
2013-06-09 19:34:13 +00:00
|
|
|
// self.screen.program.restoreCursor();
|
2013-06-09 18:14:42 +00:00
|
|
|
self.screen.program.hideCursor();
|
|
|
|
// Wait for global keypress event to fire.
|
|
|
|
process.nextTick(function() {
|
2013-06-09 20:02:12 +00:00
|
|
|
self.screen.grabKeys = false;
|
2013-06-09 18:14:42 +00:00
|
|
|
});
|
|
|
|
return err
|
|
|
|
? callback(err)
|
|
|
|
: callback(null, value);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.__listener = this._listener.bind(this);
|
|
|
|
this.on('keypress', this.__listener);
|
|
|
|
};
|
|
|
|
|
|
|
|
Textbox.prototype._listener = function(ch, key) {
|
|
|
|
var callback = this._callback
|
|
|
|
, value = this.content;
|
|
|
|
|
|
|
|
if (key.name === 'escape' || key.name === 'enter') {
|
|
|
|
delete this._callback;
|
|
|
|
this.setContent('');
|
|
|
|
this.removeListener('keypress', this.__listener);
|
|
|
|
delete this.__listener;
|
|
|
|
callback(null, key.name === 'enter' ? value : null);
|
|
|
|
} else if (key.name === 'backspace') {
|
|
|
|
if (this.content.length) {
|
|
|
|
this.setContent(this.content.slice(0, -1));
|
2013-06-09 19:32:29 +00:00
|
|
|
if (this.content.length < this.width - (this.border ? 2 : 0) - 1) {
|
|
|
|
this.screen.program.cub();
|
|
|
|
}
|
2013-06-09 18:14:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ch) {
|
|
|
|
this.setContent(this.content + ch);
|
2013-06-09 19:32:29 +00:00
|
|
|
if (this.content.length < this.width - (this.border ? 2 : 0)) {
|
|
|
|
this.screen.program.cuf();
|
|
|
|
}
|
2013-06-09 18:14:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.screen.render();
|
|
|
|
};
|
|
|
|
|
2013-06-09 19:32:29 +00:00
|
|
|
Textbox.prototype._render = Input.prototype.render;
|
|
|
|
Textbox.prototype.render = function(stop) {
|
|
|
|
var content = this.content;
|
2013-06-11 20:01:52 +00:00
|
|
|
// NOTE: This workaround will never work with _pcontent.
|
2013-06-09 19:32:29 +00:00
|
|
|
this.content = this.content.slice(-(this.width - (this.border ? 2 : 0) - 1));
|
|
|
|
var ret = this._render(stop);
|
|
|
|
this.content = content;
|
|
|
|
if (stop) return stop;
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2013-06-09 18:14:42 +00:00
|
|
|
Textbox.prototype.setEditor = function(callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.focus();
|
|
|
|
|
|
|
|
self.screen.program.normalBuffer();
|
|
|
|
self.screen.program.showCursor();
|
|
|
|
|
|
|
|
return readEditor(function(err, value) {
|
|
|
|
self.screen.program.alternateBuffer();
|
|
|
|
self.screen.program.hideCursor();
|
|
|
|
self.screen.alloc();
|
|
|
|
self.screen.render();
|
|
|
|
if (err) return callback(err);
|
|
|
|
self.setContent(value);
|
|
|
|
return callback(null, value);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Textarea
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Textarea(options) {
|
|
|
|
Input.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Textarea.prototype.__proto__ = Input.prototype;
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Button
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Button(options) {
|
|
|
|
Input.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Button.prototype.__proto__ = Input.prototype;
|
|
|
|
|
2013-06-01 07:06:04 +00:00
|
|
|
/**
|
|
|
|
* ProgressBar
|
|
|
|
*/
|
|
|
|
|
|
|
|
function ProgressBar(options) {
|
|
|
|
Input.call(this, options);
|
|
|
|
this.filled = options.filled || 0;
|
|
|
|
if (typeof this.filled === 'string') {
|
|
|
|
this.filled = +this.filled.slice(0, -1);
|
|
|
|
}
|
|
|
|
this.ch = options.ch || ' ';
|
2013-06-13 07:16:32 +00:00
|
|
|
this.barFg = convert(options.barFg);
|
|
|
|
this.barBg = convert(options.barBg);
|
2013-06-12 09:18:47 +00:00
|
|
|
this.orientation = options.orientation || 'horizontal';
|
2013-06-01 07:06:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ProgressBar.prototype.__proto__ = Input.prototype;
|
|
|
|
|
|
|
|
ProgressBar.prototype._render = ProgressBar.prototype.render;
|
2013-06-06 10:38:30 +00:00
|
|
|
ProgressBar.prototype.render = function(stop) {
|
|
|
|
// NOTE: Maybe move this `hidden` check down below `stop` check and return `ret`.
|
|
|
|
if (this.hidden) return;
|
|
|
|
|
|
|
|
var ret = this._render(stop);
|
2013-06-09 19:32:29 +00:00
|
|
|
|
2013-06-06 10:38:30 +00:00
|
|
|
if (stop) return ret;
|
2013-06-06 09:03:25 +00:00
|
|
|
|
|
|
|
var xi = ret.xi
|
|
|
|
, xl = ret.xl
|
|
|
|
, yi = ret.yi
|
2013-06-07 12:12:53 +00:00
|
|
|
, yl = ret.yl
|
2013-06-09 19:32:29 +00:00
|
|
|
, dattr;
|
2013-06-06 09:03:25 +00:00
|
|
|
|
|
|
|
if (this.border) xi++, yi++, xl--, yl--;
|
|
|
|
|
2013-06-12 09:18:47 +00:00
|
|
|
if (this.orientation === 'horizontal') {
|
|
|
|
xl = xi + ((xl - xi) * (this.filled / 100)) | 0;
|
|
|
|
} else if (this.orientation === 'vertical') {
|
|
|
|
yi = yi + ((yl - yi) - (((yl - yi) * (this.filled / 100)) | 0));
|
|
|
|
}
|
2013-06-01 07:06:04 +00:00
|
|
|
|
2013-06-13 07:16:32 +00:00
|
|
|
dattr = sattr(this, this.barFg, this.barBg);
|
2013-06-01 07:06:04 +00:00
|
|
|
|
2013-06-09 19:32:29 +00:00
|
|
|
this.screen.fillRegion(dattr, this.ch, xi, xl, yi, yl);
|
2013-06-06 09:03:25 +00:00
|
|
|
|
|
|
|
return ret;
|
2013-06-01 07:06:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ProgressBar.prototype.progress = function(filled) {
|
|
|
|
this.filled += filled;
|
|
|
|
if (this.filled < 0) this.filled = 0;
|
|
|
|
else if (this.filled > 100) this.filled = 100;
|
2013-06-11 16:48:37 +00:00
|
|
|
if (this.filled === 100) {
|
|
|
|
this.emit('complete');
|
|
|
|
}
|
2013-06-01 07:06:04 +00:00
|
|
|
};
|
|
|
|
|
2013-06-09 19:32:29 +00:00
|
|
|
ProgressBar.prototype.reset = function() {
|
2013-06-11 16:48:37 +00:00
|
|
|
this.emit('reset');
|
2013-06-09 19:32:29 +00:00
|
|
|
this.filled = 0;
|
|
|
|
};
|
|
|
|
|
2013-06-06 14:24:03 +00:00
|
|
|
/**
|
|
|
|
* Helpers
|
|
|
|
*/
|
|
|
|
|
2013-06-09 13:33:23 +00:00
|
|
|
// Convert an SGR string to our own attribute format.
|
2013-06-06 14:24:03 +00:00
|
|
|
function attrCode(code, cur) {
|
|
|
|
var flags = (cur >> 18) & 0x1ff;
|
|
|
|
var fg = (cur >> 9) & 0x1ff;
|
|
|
|
var bg = cur & 0x1ff;
|
|
|
|
var c, i;
|
|
|
|
|
|
|
|
code = /^\x1b\[([^m]*)m$/.exec(code)[1].split(';');
|
|
|
|
if (!code[0]) code[0] = '0';
|
|
|
|
|
|
|
|
for (i = 0; i < code.length; i++) {
|
|
|
|
c = +code[i] || 0;
|
|
|
|
switch (c) {
|
|
|
|
case 0:
|
|
|
|
bg = 0x1ff;
|
|
|
|
fg = 0x1ff;
|
|
|
|
flags = 0;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
flags |= 1;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
flags |= 2;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
flags |= 4;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
flags |= 8;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
flags |= 16;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (c === 48 && code[i+1] === '5') {
|
|
|
|
i += 2;
|
|
|
|
bg = +code[i];
|
|
|
|
break;
|
|
|
|
} else if (c === 38 && code[i+1] === '5') {
|
|
|
|
i += 2;
|
|
|
|
fg = +code[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c >= 40 && c <= 47) {
|
|
|
|
bg = c - 40;
|
|
|
|
} else if (c >= 100 && c <= 107) {
|
|
|
|
bg = c - 100;
|
|
|
|
bg += 8;
|
|
|
|
} else if (c >= 30 && c <= 37) {
|
|
|
|
fg = c - 30;
|
|
|
|
} else if (c >= 90 && c <= 97) {
|
|
|
|
fg = c - 90;
|
|
|
|
fg += 8;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (flags << 18) | (fg << 9) | bg;
|
|
|
|
}
|
|
|
|
|
2013-06-09 18:14:42 +00:00
|
|
|
function readEditor(callback) {
|
|
|
|
var spawn = require('child_process').spawn
|
|
|
|
, fs = require('fs')
|
|
|
|
, editor = process.env.EDITOR || 'vi'
|
|
|
|
, file = '/tmp/blessed.' + Math.random().toString(36);
|
|
|
|
|
|
|
|
var write = process.stdout.write;
|
|
|
|
process.stdout.write = function() {};
|
|
|
|
|
|
|
|
try {
|
|
|
|
process.stdin.pause();
|
|
|
|
} catch (e) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
var resume = function() {
|
|
|
|
try {
|
|
|
|
process.stdin.resume();
|
|
|
|
} catch (e) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
process.stdout.write = write;
|
|
|
|
};
|
|
|
|
|
|
|
|
var ps = spawn(editor, [file], {
|
|
|
|
stdio: 'inherit',
|
|
|
|
env: process.env,
|
|
|
|
cwd: process.env.HOME
|
|
|
|
});
|
|
|
|
|
|
|
|
ps.on('error', function(err) {
|
|
|
|
resume();
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
ps.on('exit', function(code) {
|
|
|
|
resume();
|
|
|
|
return fs.readFile(file, 'utf8', function(err, data) {
|
|
|
|
return fs.unlink(file, function() {
|
|
|
|
if (err) return callback(err);
|
|
|
|
return callback(null, data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-06-10 01:19:32 +00:00
|
|
|
function wrapContent(content, width) {
|
|
|
|
var lines = content.split('\n')
|
|
|
|
, out = [];
|
|
|
|
|
|
|
|
lines.forEach(function(line) {
|
|
|
|
var total
|
|
|
|
, i
|
|
|
|
, part
|
|
|
|
, esc;
|
|
|
|
|
|
|
|
while (line.length > width) {
|
|
|
|
for (i = 0, total = 0; i < line.length; i++) {
|
|
|
|
while (line[i] === '\x1b') {
|
|
|
|
while (line[i] && line[i++] !== 'm');
|
|
|
|
}
|
|
|
|
if (!line[i]) break;
|
|
|
|
if (++total === width) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
part = line.substring(0, i - 1);
|
|
|
|
esc = /\x1b[\[\d;]*$/.exec(part);
|
|
|
|
|
|
|
|
if (esc) {
|
|
|
|
part = part.slice(0, -esc[0].length);
|
|
|
|
line = line.substring(i - 1 - esc[0].length);
|
|
|
|
out.push(part);
|
|
|
|
} else {
|
|
|
|
line = line.substring(i - 1);
|
|
|
|
out.push(part);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If only an escape code got cut off, at it to `part`.
|
|
|
|
if (/^(?:\x1b[\[\d;]*m)+$/.test(line)) {
|
|
|
|
out[out.length-1] += line;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
out.push(line);
|
|
|
|
});
|
|
|
|
|
|
|
|
out.width = width;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2013-06-13 07:16:32 +00:00
|
|
|
var colors = {
|
|
|
|
default: -1,
|
|
|
|
bg: -1,
|
|
|
|
fg: -1,
|
|
|
|
black: 0,
|
|
|
|
red: 1,
|
|
|
|
green: 2,
|
|
|
|
yellow: 3,
|
|
|
|
blue: 4,
|
|
|
|
magenta: 5,
|
|
|
|
cyan: 6,
|
|
|
|
white: 7,
|
|
|
|
lightblack: 8,
|
|
|
|
lightred: 9,
|
|
|
|
lightgreen: 10,
|
|
|
|
lightyellow: 11,
|
|
|
|
lightblue: 12,
|
|
|
|
lightmagenta: 13,
|
|
|
|
lightcyan: 14,
|
|
|
|
lightwhite: 15
|
|
|
|
};
|
|
|
|
|
|
|
|
function convert(color) {
|
|
|
|
var val = colors[color];
|
|
|
|
if (val == null) val = color;
|
|
|
|
if (val == null) val = -1;
|
|
|
|
if (val === -1) return 0x1ff;
|
|
|
|
return val;
|
|
|
|
}
|
2013-06-01 07:06:04 +00:00
|
|
|
|
2013-06-13 07:16:32 +00:00
|
|
|
function sattr(obj, fg, bg) {
|
|
|
|
return (((obj.invisible << 18)
|
|
|
|
+ (obj.inverse << 18)
|
|
|
|
+ (obj.blink << 18)
|
|
|
|
+ (obj.bold << 18)
|
|
|
|
+ (obj.underline << 18))
|
|
|
|
| (fg << 9))
|
|
|
|
| bg;
|
|
|
|
}
|
2013-06-01 07:06:04 +00:00
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Expose
|
|
|
|
*/
|
|
|
|
|
|
|
|
exports.Screen = Screen;
|
|
|
|
exports.Box = Box;
|
|
|
|
exports.Text = Text;
|
2013-06-06 09:03:25 +00:00
|
|
|
exports.Line = Line;
|
2013-06-01 05:29:31 +00:00
|
|
|
exports.ScrollableBox = ScrollableBox;
|
2013-06-01 02:09:07 +00:00
|
|
|
exports.List = List;
|
2013-06-06 09:03:25 +00:00
|
|
|
exports.ScrollableText = ScrollableText;
|
2013-06-01 05:29:31 +00:00
|
|
|
exports.Input = Input;
|
|
|
|
exports.Textbox = Textbox;
|
|
|
|
exports.Button = Button;
|
2013-06-01 07:06:04 +00:00
|
|
|
exports.ProgressBar = ProgressBar;
|