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
|
|
|
|
*/
|
|
|
|
|
2013-07-12 12:48:55 +00:00
|
|
|
var EventEmitter = require('events').EventEmitter
|
|
|
|
, path = require('path')
|
2013-07-14 11:43:17 +00:00
|
|
|
, fs = require('fs')
|
|
|
|
, colors = require('./colors');
|
2013-06-06 09:03:25 +00:00
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Node
|
|
|
|
*/
|
|
|
|
|
2013-06-01 02:39:11 +00:00
|
|
|
function Node(options) {
|
2013-06-14 20:58:54 +00:00
|
|
|
if (!(this instanceof Node)) {
|
|
|
|
return new 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
|
2013-06-14 08:51:12 +00:00
|
|
|
|| Screen.global
|
2013-06-09 13:33:23 +00:00
|
|
|
|| (function(){throw new Error('No active screen.')})();
|
2013-07-12 01:48:30 +00:00
|
|
|
this.parent = options.parent || null;
|
2013-06-09 13:33:23 +00:00
|
|
|
this.children = [];
|
2013-06-18 10:08:31 +00:00
|
|
|
this.$ = this._ = this.data = {};
|
2013-06-29 18:09:22 +00:00
|
|
|
this.uid = Node.uid++;
|
2013-07-12 03:52:46 +00:00
|
|
|
this.index = -1;
|
2013-06-09 13:33:23 +00:00
|
|
|
|
2013-06-18 15:52:48 +00:00
|
|
|
if (this.parent) {
|
|
|
|
this.parent.append(this);
|
|
|
|
}
|
|
|
|
|
2013-06-20 11:43:56 +00:00
|
|
|
if (!this.parent) {
|
|
|
|
this._detached = true;
|
|
|
|
}
|
|
|
|
|
2013-06-09 13:33:23 +00:00
|
|
|
(options.children || []).forEach(this.append.bind(this));
|
|
|
|
|
2013-07-04 04:15:09 +00:00
|
|
|
// if (this.type === 'screen' && !this.focused) {
|
|
|
|
// this.focused = this.children[0];
|
|
|
|
// }
|
2013-06-01 02:39:11 +00:00
|
|
|
}
|
|
|
|
|
2013-06-29 18:09:22 +00:00
|
|
|
Node.uid = 0;
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
Node.prototype.__proto__ = EventEmitter.prototype;
|
|
|
|
|
2013-06-25 11:34:10 +00:00
|
|
|
Node.prototype.type = 'node';
|
|
|
|
|
2013-07-15 03:11:48 +00:00
|
|
|
Node.prototype.insert = function(element, i) {
|
|
|
|
// var old = element.parent;
|
|
|
|
|
|
|
|
element.detach();
|
|
|
|
element.parent = this;
|
|
|
|
|
|
|
|
if (this.type === 'screen' && !this.focused) {
|
|
|
|
this.focused = element;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!~this.children.indexOf(element)) {
|
|
|
|
if (i === 0) {
|
|
|
|
this.children.unshift(element);
|
|
|
|
} else if (i === this.children.length) {
|
|
|
|
this.children.push(element);
|
|
|
|
} else {
|
|
|
|
this.children.splice(i, 0, element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
element.emit('reparent', this);
|
|
|
|
this.emit('adopt', element);
|
|
|
|
|
|
|
|
// if (!old) {
|
|
|
|
(function emit(el) {
|
|
|
|
el._detached = false;
|
|
|
|
el.emit('attach');
|
|
|
|
if (el.children) el.children.forEach(emit);
|
|
|
|
})(element);
|
|
|
|
|
|
|
|
// element.emitDescendants('attach', function(el) {
|
|
|
|
// el._detached = false;
|
|
|
|
// });
|
|
|
|
};
|
|
|
|
|
2013-07-15 03:12:36 +00:00
|
|
|
Node.prototype.prepend = function(element) {
|
|
|
|
this.insert(element, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
Node.prototype.append = function(element) {
|
|
|
|
this.insert(element, this.children.length);
|
|
|
|
};
|
|
|
|
|
2013-07-15 03:11:48 +00:00
|
|
|
Node.prototype.insertBefore = function(element, other) {
|
|
|
|
var i = this.children.indexOf(other);
|
|
|
|
if (~i) this.insert(element, i);
|
|
|
|
};
|
|
|
|
|
|
|
|
Node.prototype.insertAfter = function(element, other) {
|
|
|
|
var i = this.children.indexOf(other);
|
|
|
|
if (~i) this.insert(element, i + 1);
|
|
|
|
};
|
|
|
|
|
2013-06-01 02:39:11 +00:00
|
|
|
Node.prototype.remove = function(element) {
|
2013-07-12 01:48:30 +00:00
|
|
|
element.parent = null;
|
2013-06-09 13:33:23 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-07-03 23:47:55 +00:00
|
|
|
if (this.type !== 'screen') {
|
2013-06-18 15:52:48 +00:00
|
|
|
i = this.screen.clickable.indexOf(element);
|
|
|
|
if (~i) this.screen.clickable.splice(i, 1);
|
|
|
|
i = this.screen.input.indexOf(element);
|
|
|
|
if (~i) this.screen.input.splice(i, 1);
|
|
|
|
}
|
|
|
|
|
2013-07-03 23:47:55 +00:00
|
|
|
if (this.type === 'screen' && this.focused === element) {
|
2013-06-09 13:33:23 +00:00
|
|
|
this.focused = this.children[0];
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:13:49 +00:00
|
|
|
element.emit('reparent', null);
|
|
|
|
this.emit('remove', element);
|
2013-06-20 11:43:56 +00:00
|
|
|
|
|
|
|
(function emit(el) {
|
|
|
|
el._detached = true;
|
|
|
|
el.emit('detach');
|
|
|
|
if (el.children) el.children.forEach(emit);
|
|
|
|
})(element);
|
2013-06-20 12:18:04 +00:00
|
|
|
|
2013-07-12 01:48:30 +00:00
|
|
|
// element.emitDescendants('detach', function(el) {
|
|
|
|
// el._detached = true;
|
|
|
|
// });
|
2013-06-24 11:16:02 +00:00
|
|
|
|
2013-07-04 06:50:50 +00:00
|
|
|
// this.clearPos();
|
2013-06-01 02:39:11 +00:00
|
|
|
};
|
|
|
|
|
2013-06-20 16:48:12 +00:00
|
|
|
Node.prototype.detach = function() {
|
2013-06-24 11:16:02 +00:00
|
|
|
if (this.parent) this.parent.remove(this);
|
2013-07-04 06:50:50 +00:00
|
|
|
// this.clearPos();
|
2013-06-01 02:39:11 +00:00
|
|
|
};
|
2013-06-01 02:09:07 +00:00
|
|
|
|
2013-06-20 12:18:04 +00:00
|
|
|
Node.prototype.emitDescendants = function() {
|
|
|
|
var args = Array.prototype.slice(arguments)
|
|
|
|
, iter;
|
|
|
|
|
|
|
|
if (typeof args[args.length-1] === 'function') {
|
|
|
|
iter = args.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
(function emit(el) {
|
|
|
|
if (iter) iter(el);
|
|
|
|
el.emit.apply(el, args);
|
|
|
|
if (el.children) {
|
|
|
|
el.children.forEach(emit);
|
|
|
|
}
|
|
|
|
})(this);
|
|
|
|
};
|
|
|
|
|
2013-07-12 13:58:25 +00:00
|
|
|
Node.prototype.emitAncestors = function() {
|
|
|
|
var args = Array.prototype.slice(arguments)
|
|
|
|
, el = this
|
|
|
|
, iter;
|
|
|
|
|
|
|
|
if (typeof args[args.length-1] === 'function') {
|
|
|
|
iter = args.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (iter) iter(el);
|
|
|
|
el.emit.apply(el, args);
|
|
|
|
} while (el = el.parent);
|
|
|
|
};
|
|
|
|
|
2013-06-29 18:09:22 +00:00
|
|
|
Node.prototype.hasDescendant = function(target) {
|
|
|
|
return (function find(el) {
|
|
|
|
for (var i = 0; i < el.children.length; i++) {
|
|
|
|
if (el.children[i] === target) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (find(el.children[i]) === true) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
})(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
Node.prototype.hasAncestor = function(target) {
|
|
|
|
var el = this;
|
|
|
|
while (el = el.parent) {
|
|
|
|
if (el === target) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2013-06-25 22:31:13 +00:00
|
|
|
Node.prototype.gon = function(type, callback) {
|
|
|
|
var self = this
|
|
|
|
, events = this._events || {}
|
|
|
|
, listeners = (events[type] || []).slice();
|
|
|
|
|
2013-07-03 23:46:06 +00:00
|
|
|
return this.on(type, function() {
|
|
|
|
if (callback.apply(this, arguments) === true) {
|
2013-06-25 22:31:13 +00:00
|
|
|
self._events[type] = listeners;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-07-12 09:59:58 +00:00
|
|
|
Node.prototype.get = function(name, value) {
|
|
|
|
if (this.data.hasOwnProperty(name)) {
|
|
|
|
return this.data[name];
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
Node.prototype.set = function(name, value) {
|
|
|
|
return this.data[name] = value;
|
|
|
|
};
|
|
|
|
|
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-14 20:58:54 +00:00
|
|
|
if (!(this instanceof Screen)) {
|
|
|
|
return new Screen(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options && options.rsety && options.listen) {
|
|
|
|
options = { program: options };
|
|
|
|
}
|
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
options.program = options.program
|
|
|
|
|| require('./program').global
|
|
|
|
|| new (require('./program'))(options);
|
|
|
|
|
2013-06-14 08:51:12 +00:00
|
|
|
if (!Screen.global) {
|
|
|
|
Screen.global = this;
|
2013-06-09 13:33:23 +00:00
|
|
|
}
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
Node.call(this, options);
|
2013-06-01 02:39:11 +00:00
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
this.program = options.program;
|
2013-07-12 05:36:36 +00:00
|
|
|
this.program.zero = true;
|
2013-06-01 02:09:07 +00:00
|
|
|
this.tput = this.program.tput;
|
2013-07-12 05:36:36 +00:00
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
this.dattr = ((0 << 18) | (0x1ff << 9)) | 0x1ff;
|
2013-07-12 05:36:36 +00:00
|
|
|
|
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-16 14:31:55 +00:00
|
|
|
this.hover = null;
|
2013-06-09 20:52:22 +00:00
|
|
|
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-18 15:52:48 +00:00
|
|
|
this.lockKeys = false;
|
2013-07-12 03:52:46 +00:00
|
|
|
this.focused;
|
2013-06-09 16:47:02 +00:00
|
|
|
|
2013-06-29 18:09:22 +00:00
|
|
|
this._ci = -1;
|
|
|
|
|
2013-07-13 02:51:24 +00:00
|
|
|
function resize() {
|
2013-06-01 05:29:31 +00:00
|
|
|
self.alloc();
|
|
|
|
self.render();
|
2013-06-24 11:16:02 +00:00
|
|
|
(function emit(el) {
|
|
|
|
el.emit('resize');
|
|
|
|
if (el.children) {
|
|
|
|
el.children.forEach(emit);
|
|
|
|
}
|
|
|
|
})(self);
|
2013-07-03 23:46:06 +00:00
|
|
|
// self.emitDescendants('resize');
|
2013-07-13 02:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.program.on('resize', function() {
|
|
|
|
if (!self.options.resizeTimeout) {
|
|
|
|
return resize();
|
|
|
|
}
|
|
|
|
if (self._resizeTimer) {
|
|
|
|
clearTimeout(self._resizeTimer);
|
|
|
|
delete self._resizeTimer;
|
|
|
|
}
|
|
|
|
var time = typeof self.options.resizeTimeout === 'number'
|
|
|
|
? self.options.resizeTimeout
|
|
|
|
: 300;
|
|
|
|
self._resizeTimer = setTimeout(resize, time);
|
2013-06-01 05:29:31 +00:00
|
|
|
});
|
2013-06-06 09:03:25 +00:00
|
|
|
|
2013-07-14 04:38:43 +00:00
|
|
|
this.program.on('focus', function() {
|
|
|
|
self.emit('focus');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.program.on('blur', function() {
|
|
|
|
self.emit('blur');
|
|
|
|
});
|
|
|
|
|
2013-06-09 16:47:02 +00:00
|
|
|
this.program.alternateBuffer();
|
|
|
|
this.program.hideCursor();
|
2013-07-15 06:44:11 +00:00
|
|
|
this.program.cup(0, 0);
|
2013-07-15 23:53:04 +00:00
|
|
|
//this.program.csr(0, this.height - 1);
|
2013-07-14 17:08:03 +00:00
|
|
|
this.alloc();
|
2013-06-09 16:47:02 +00:00
|
|
|
|
2013-06-18 11:30:26 +00:00
|
|
|
function reset() {
|
|
|
|
if (reset.done) return;
|
|
|
|
reset.done = true;
|
2013-07-15 23:53:04 +00:00
|
|
|
if (self.program.scrollTop !== 0
|
|
|
|
|| self.program.scrollBottom !== self.rows - 1) {
|
|
|
|
self.program.csr(0, self.height - 1);
|
|
|
|
}
|
2013-06-09 16:47:02 +00:00
|
|
|
self.program.clear();
|
|
|
|
self.program.showCursor();
|
|
|
|
self.program.normalBuffer();
|
2013-06-18 11:30:26 +00:00
|
|
|
if (self._listenedMouse) {
|
|
|
|
self.program.disableMouse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-24 11:16:02 +00:00
|
|
|
this._maxListeners = Infinity;
|
|
|
|
|
2013-06-18 11:30:26 +00:00
|
|
|
process.on('uncaughtException', function(err) {
|
|
|
|
reset();
|
|
|
|
if (err) console.error(err.stack + '');
|
|
|
|
return process.exit(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('exit', function() {
|
|
|
|
reset();
|
2013-06-09 16:47:02 +00:00
|
|
|
});
|
2013-06-11 14:27:50 +00:00
|
|
|
|
|
|
|
this.on('newListener', function fn(type) {
|
2013-07-04 04:15:09 +00:00
|
|
|
if (type === 'keypress' || type.indexOf('key ') === 0 || type === 'mouse') {
|
|
|
|
if (type === 'keypress' || type.indexOf('key ') === 0) self._listenKeys();
|
2013-06-11 14:27:50 +00:00
|
|
|
if (type === 'mouse') self._listenMouse();
|
|
|
|
}
|
2013-07-12 09:59:58 +00:00
|
|
|
if (type === 'mouse'
|
|
|
|
|| type === 'click'
|
|
|
|
|| type === 'mouseover'
|
|
|
|
|| type === 'mouseout'
|
|
|
|
|| type === 'mousedown'
|
|
|
|
|| type === 'mouseup'
|
|
|
|
|| type === 'mousewheel'
|
|
|
|
|| type === 'wheeldown'
|
|
|
|
|| type === 'wheelup'
|
|
|
|
|| type === 'mousemove') {
|
|
|
|
self._listenMouse();
|
|
|
|
}
|
2013-06-11 14:27:50 +00:00
|
|
|
});
|
2013-06-01 05:29:31 +00:00
|
|
|
}
|
2013-06-01 02:39:11 +00:00
|
|
|
|
2013-06-14 08:51:12 +00:00
|
|
|
Screen.global = null;
|
2013-06-09 13:33:23 +00:00
|
|
|
|
2013-06-01 05:29:31 +00:00
|
|
|
Screen.prototype.__proto__ = Node.prototype;
|
|
|
|
|
2013-06-25 11:34:10 +00:00
|
|
|
Screen.prototype.type = 'screen';
|
|
|
|
|
2013-07-04 04:15:09 +00:00
|
|
|
// TODO: Bubble and capture events throughout the tree.
|
2013-06-16 14:31:55 +00:00
|
|
|
Screen.prototype._listenMouse = function(el) {
|
2013-06-06 09:03:25 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-06-16 14:31:55 +00:00
|
|
|
if (el && !~this.clickable.indexOf(el)) {
|
|
|
|
this.clickable.push(el);
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this._listenedMouse) return;
|
|
|
|
this._listenedMouse = true;
|
|
|
|
|
|
|
|
this.program.enableMouse();
|
|
|
|
|
2013-07-03 23:46:06 +00:00
|
|
|
// this.on('element click', el.focus.bind(el));
|
2013-06-09 20:21:58 +00:00
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
this.program.on('mouse', function(data) {
|
2013-06-18 15:52:48 +00:00
|
|
|
if (self.lockKeys) return;
|
|
|
|
|
2013-07-12 13:58:25 +00:00
|
|
|
var clickable = hsort(self.clickable)
|
|
|
|
, i = 0
|
2013-06-16 14:31:55 +00:00
|
|
|
, left
|
|
|
|
, top
|
|
|
|
, width
|
|
|
|
, height
|
|
|
|
, el
|
|
|
|
, set
|
|
|
|
, ret;
|
|
|
|
|
2013-07-12 13:58:25 +00:00
|
|
|
for (; i < clickable.length; i++) {
|
|
|
|
el = clickable[i];
|
2013-07-12 15:22:06 +00:00
|
|
|
|
2013-06-18 10:08:31 +00:00
|
|
|
if (!el.visible) continue;
|
2013-06-16 14:31:55 +00:00
|
|
|
|
2013-07-12 15:22:06 +00:00
|
|
|
// Something like (doesn't work because textbox is usually focused):
|
|
|
|
// if (self.grabKeys && self.focused !== el
|
|
|
|
// && !el.hasAncestor(self.focused)) continue;
|
|
|
|
|
2013-06-16 14:31:55 +00:00
|
|
|
// Get the true coordinates.
|
|
|
|
ret = el._lastPos;
|
|
|
|
if (!ret) continue;
|
|
|
|
left = ret.xi;
|
|
|
|
top = ret.yi;
|
|
|
|
width = ret.xl - ret.xi;
|
|
|
|
height = ret.yl - ret.yi;
|
|
|
|
|
2013-07-14 13:05:42 +00:00
|
|
|
if (data.x >= left && data.x < left + width
|
|
|
|
&& data.y >= top && data.y < top + 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-16 14:31:55 +00:00
|
|
|
} else if (data.action === 'mousemove') {
|
2013-07-12 03:52:46 +00:00
|
|
|
if (self.hover && el.index > self.hover.index) {
|
2013-06-29 18:09:22 +00:00
|
|
|
set = false;
|
|
|
|
}
|
2013-06-16 14:31:55 +00:00
|
|
|
if (self.hover !== el && !set) {
|
|
|
|
if (self.hover) {
|
|
|
|
self.hover.emit('mouseout', data);
|
|
|
|
self.emit('element mouseout', self.hover, data);
|
|
|
|
}
|
|
|
|
el.emit('mouseover', data);
|
|
|
|
self.emit('element mouseover', el, data);
|
|
|
|
self.hover = el;
|
|
|
|
}
|
|
|
|
set = true;
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
el.emit(data.action, data);
|
2013-07-12 13:58:25 +00:00
|
|
|
self.emit('element ' + data.action, el, data);
|
|
|
|
|
|
|
|
// XXX Temporary workaround for List wheel events.
|
|
|
|
// Make sure they get propogated to the list instead of the list item.
|
|
|
|
if ((data.action === 'wheeldown' || data.action === 'wheelup')
|
|
|
|
&& el.listeners(data.action).length === 0
|
|
|
|
&& el.parent.listeners(data.action).length > 0) {
|
|
|
|
el.parent.emit(data.action, data);
|
|
|
|
self.emit('element ' + data.action, el.parent, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seems to work better without this if statement:
|
|
|
|
// if (data.action !== 'mousemove')
|
|
|
|
break;
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-16 14:31:55 +00:00
|
|
|
|
2013-07-03 23:46:06 +00:00
|
|
|
// Just mouseover?
|
|
|
|
if ((data.action === 'mousemove'
|
|
|
|
|| data.action === 'mousedown'
|
|
|
|
|| data.action === 'mouseup')
|
|
|
|
&& self.hover
|
|
|
|
&& !set) {
|
2013-06-16 14:31:55 +00:00
|
|
|
self.hover.emit('mouseout', data);
|
|
|
|
self.emit('element mouseout', self.hover, data);
|
|
|
|
self.hover = null;
|
|
|
|
}
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
self.emit('mouse', data);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-07-03 23:46:06 +00:00
|
|
|
// TODO: Bubble and capture events throughout the tree.
|
2013-06-06 09:03:25 +00:00
|
|
|
Screen.prototype._listenKeys = function(el) {
|
|
|
|
var self = this;
|
|
|
|
|
2013-07-03 23:46:06 +00:00
|
|
|
if (el && !~this.input.indexOf(el)) {
|
|
|
|
// Listen for click, but do not enable
|
|
|
|
// mouse if it's not enabled yet.
|
|
|
|
if (el.options.autoFocus !== false) {
|
2013-06-16 14:31:55 +00:00
|
|
|
var lm = this._listenedMouse;
|
|
|
|
this._listenedMouse = true;
|
2013-07-03 23:46:06 +00:00
|
|
|
el.on('click', el.focus.bind(el));
|
2013-06-16 14:31:55 +00:00
|
|
|
this._listenedMouse = lm;
|
2013-06-09 20:02:12 +00:00
|
|
|
}
|
2013-07-03 23:46:06 +00:00
|
|
|
this.input.push(el);
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this._listenedKeys) return;
|
|
|
|
this._listenedKeys = true;
|
|
|
|
|
2013-07-03 23:46:06 +00:00
|
|
|
// NOTE: The event emissions used to be reversed:
|
|
|
|
// element + screen
|
|
|
|
// They are now:
|
|
|
|
// screen + element
|
|
|
|
// After the first keypress emitted, the handler
|
|
|
|
// checks to make sure grabKeys, lockKeys, and focused
|
|
|
|
// weren't changed, and handles those situations appropriately.
|
2013-06-06 09:03:25 +00:00
|
|
|
this.program.on('keypress', function(ch, key) {
|
2013-06-18 15:52:48 +00:00
|
|
|
if (self.lockKeys) return;
|
2013-07-03 23:46:06 +00:00
|
|
|
|
|
|
|
var focused = self.focused
|
|
|
|
, grabKeys = self.grabKeys;
|
|
|
|
|
|
|
|
if (!grabKeys) {
|
2013-06-09 18:14:42 +00:00
|
|
|
self.emit('keypress', ch, key);
|
2013-07-12 09:59:58 +00:00
|
|
|
self.emit('key ' + key.full, ch, key);
|
2013-06-09 18:14:42 +00:00
|
|
|
}
|
2013-07-03 23:46:06 +00:00
|
|
|
|
|
|
|
// If something changed from the screen key handler, stop.
|
|
|
|
if (self.grabKeys !== grabKeys || self.lockKeys) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-25 22:31:13 +00:00
|
|
|
if (~self.input.indexOf(focused)) {
|
|
|
|
focused.emit('keypress', ch, key);
|
2013-07-12 09:59:58 +00:00
|
|
|
focused.emit('key ' + key.full, ch, key);
|
2013-07-04 04:15:09 +00:00
|
|
|
// self.emit('element keypress', focused, ch, key);
|
2013-07-12 09:59:58 +00:00
|
|
|
// self.emit('element key ' + key.full, focused, ch, key);
|
2013-06-25 22:31:13 +00:00
|
|
|
}
|
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-07-14 17:21:14 +00:00
|
|
|
|
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, ' '];
|
|
|
|
}
|
2013-07-14 17:21:14 +00:00
|
|
|
this.lines[y].dirty = false;
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
2013-07-14 17:21:14 +00:00
|
|
|
|
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++) {
|
2013-07-14 17:08:03 +00:00
|
|
|
this.olines[y][x] = [this.dattr, ' '];
|
2013-06-06 10:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-14 17:21:14 +00:00
|
|
|
|
|
|
|
this.program.clear();
|
2013-06-01 05:29:31 +00:00
|
|
|
};
|
2013-06-01 02:09:07 +00:00
|
|
|
|
|
|
|
Screen.prototype.render = function() {
|
2013-06-29 18:09:22 +00:00
|
|
|
var self = this;
|
2013-07-15 23:53:04 +00:00
|
|
|
|
|
|
|
this.emit('prerender');
|
|
|
|
|
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-29 18:09:22 +00:00
|
|
|
this._ci = 0;
|
2013-06-01 02:09:07 +00:00
|
|
|
this.children.forEach(function(el) {
|
2013-07-12 03:52:46 +00:00
|
|
|
el.index = self._ci++;
|
2013-06-01 02:09:07 +00:00
|
|
|
el.render();
|
|
|
|
});
|
2013-06-29 18:09:22 +00:00
|
|
|
this._ci = -1;
|
2013-07-15 23:53:04 +00:00
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
this.draw(0, this.rows - 1);
|
2013-07-15 23:53:04 +00:00
|
|
|
|
|
|
|
this.emit('render');
|
2013-06-01 02:09:07 +00:00
|
|
|
};
|
|
|
|
|
2013-06-13 06:33:10 +00:00
|
|
|
Screen.prototype.blankLine = function(ch, dirty) {
|
|
|
|
var out = [];
|
2013-06-29 22:56:09 +00:00
|
|
|
for (var x = 0; x < this.cols; x++) {
|
|
|
|
out[x] = [this.dattr, ch || ' '];
|
2013-06-13 06:33:10 +00:00
|
|
|
}
|
2013-06-29 22:56:09 +00:00
|
|
|
out.dirty = dirty;
|
2013-06-13 06:33:10 +00:00
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.insertLine = function(n, y, top, bottom) {
|
2013-07-14 16:04:48 +00:00
|
|
|
if (!this.tput
|
|
|
|
|| !this.tput.strings.change_scroll_region
|
|
|
|
|| !this.tput.strings.delete_line
|
|
|
|
|| !this.tput.strings.insert_line) return;
|
|
|
|
|
2013-07-15 23:53:04 +00:00
|
|
|
this.program.sc();
|
2013-07-12 05:36:36 +00:00
|
|
|
this.program.csr(top, bottom);
|
|
|
|
this.program.cup(y, 0);
|
2013-07-15 23:53:04 +00:00
|
|
|
//if (y === top && n === 1) {
|
|
|
|
// this.program.ri(); // su
|
|
|
|
//} else {
|
2013-07-14 16:04:48 +00:00
|
|
|
this.program.il(n);
|
2013-07-12 05:36:36 +00:00
|
|
|
this.program.csr(0, this.height - 1);
|
2013-07-15 23:53:04 +00:00
|
|
|
this.program.rc();
|
2013-06-13 06:33:10 +00:00
|
|
|
|
2013-07-15 23:53:04 +00:00
|
|
|
var j = bottom + 1;
|
2013-06-13 06:33:10 +00:00
|
|
|
|
|
|
|
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) {
|
2013-07-14 16:04:48 +00:00
|
|
|
if (!this.tput
|
|
|
|
|| !this.tput.strings.change_scroll_region
|
|
|
|
|| !this.tput.strings.delete_line
|
|
|
|
|| !this.tput.strings.insert_line) return;
|
|
|
|
|
2013-07-15 23:53:04 +00:00
|
|
|
this.program.sc();
|
2013-07-12 05:36:36 +00:00
|
|
|
this.program.csr(top, bottom);
|
2013-07-15 23:53:04 +00:00
|
|
|
//if (y === top && n === 1) {
|
|
|
|
// this.program.cup(bottom, 0);
|
|
|
|
// this.program.ind(); // sd
|
|
|
|
//} else {
|
2013-07-12 05:36:36 +00:00
|
|
|
this.program.cup(y, 0);
|
2013-07-14 16:04:48 +00:00
|
|
|
this.program.dl(n);
|
2013-07-12 05:36:36 +00:00
|
|
|
this.program.csr(0, this.height - 1);
|
2013-07-15 23:53:04 +00:00
|
|
|
this.program.rc();
|
2013-06-13 06:33:10 +00:00
|
|
|
|
2013-07-15 23:53:04 +00:00
|
|
|
var j = bottom + 1;
|
2013-06-13 06:33:10 +00:00
|
|
|
|
|
|
|
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-07-04 04:15:09 +00:00
|
|
|
Screen.prototype.deleteBottom = function(top, bottom) {
|
|
|
|
return this.clearRegion(0, this.width, bottom, bottom);
|
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.deleteTop = function(top, bottom) {
|
2013-07-12 01:48:30 +00:00
|
|
|
// Same as: return this.insertBottom(top, bottom);
|
2013-07-04 04:15:09 +00:00
|
|
|
return this.deleteLine(1, top, top, bottom);
|
|
|
|
};
|
|
|
|
|
2013-07-14 16:58:44 +00:00
|
|
|
// Parse the sides of an element to determine
|
|
|
|
// whether an element has uniform cells on
|
|
|
|
// both sides. If it does, we can use CSR to
|
|
|
|
// optimize scrolling on a scrollable element.
|
2013-07-14 16:23:05 +00:00
|
|
|
// Not exactly sure how worthwile this is.
|
|
|
|
// This will cause a performance/cpu-usage hit,
|
|
|
|
// but will it be less or greater than the
|
|
|
|
// performance hit of slow-rendering scrollable
|
|
|
|
// boxes with clean sides?
|
|
|
|
Screen.prototype.cleanSides = function(el) {
|
2013-07-14 16:58:44 +00:00
|
|
|
var pos = el._lastPos;
|
|
|
|
|
|
|
|
if (!pos) {
|
2013-07-14 16:23:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos._cleanSides != null) {
|
|
|
|
return pos._cleanSides;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos.xi === 0 && pos.xl === this.width) {
|
|
|
|
return pos._cleanSides = true;
|
|
|
|
}
|
|
|
|
|
2013-07-15 06:44:11 +00:00
|
|
|
if (!this.options.smartCSR) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-14 16:58:44 +00:00
|
|
|
var yi = pos.yi + (el.border ? 1 : 0) + el.padding
|
|
|
|
, yl = pos.yl - (el.border ? 1 : 0) - el.padding
|
2013-07-15 03:11:48 +00:00
|
|
|
, first
|
2013-07-14 16:58:44 +00:00
|
|
|
, ch
|
|
|
|
, x
|
|
|
|
, y;
|
|
|
|
|
2013-07-15 03:11:48 +00:00
|
|
|
for (x = pos.xi - 1; x >= 0; x--) {
|
|
|
|
first = this.olines[yi][x];
|
|
|
|
for (y = yi; y < yl; y++) {
|
2013-07-14 16:23:05 +00:00
|
|
|
ch = this.olines[y][x];
|
|
|
|
if (ch[0] !== first[0] || ch[1] !== first[1]) {
|
|
|
|
return pos._cleanSides = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 03:11:48 +00:00
|
|
|
for (x = pos.xl; x < this.width; x++) {
|
|
|
|
first = this.olines[yi][x];
|
|
|
|
for (y = yi; y < yl; y++) {
|
2013-07-14 16:23:05 +00:00
|
|
|
ch = this.olines[y][x];
|
|
|
|
if (ch[0] !== first[0] || ch[1] !== first[1]) {
|
|
|
|
return pos._cleanSides = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos._cleanSides = true;
|
|
|
|
};
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
Screen.prototype.draw = function(start, end) {
|
|
|
|
var x
|
|
|
|
, y
|
|
|
|
, line
|
|
|
|
, out
|
|
|
|
, ch
|
|
|
|
, data
|
|
|
|
, attr
|
2013-07-15 18:15:41 +00:00
|
|
|
, fg
|
|
|
|
, bg
|
2013-06-01 02:09:07 +00:00
|
|
|
, flags;
|
|
|
|
|
2013-06-06 10:19:52 +00:00
|
|
|
var lx = -1
|
|
|
|
, ly = -1
|
|
|
|
, o;
|
|
|
|
|
2013-07-14 12:02:59 +00:00
|
|
|
// var cx = this.program.x
|
|
|
|
// , cy = this.program.y
|
|
|
|
// , ch = this.program.cursorHidden;
|
|
|
|
//
|
|
|
|
// if (!ch) this.program.hideCursor();
|
|
|
|
|
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[';
|
|
|
|
|
2013-07-15 18:15:41 +00:00
|
|
|
bg = data & 0x1ff;
|
|
|
|
fg = (data >> 9) & 0x1ff;
|
2013-06-01 02:09:07 +00:00
|
|
|
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;';
|
|
|
|
}
|
|
|
|
|
2013-07-15 18:15:41 +00:00
|
|
|
if (bg !== 0x1ff) {
|
|
|
|
bg = this._reduceColor(bg);
|
|
|
|
if (bg < 16) {
|
|
|
|
if (bg < 8) {
|
|
|
|
bg += 40;
|
|
|
|
} else if (bg < 16) {
|
|
|
|
bg -= 8;
|
|
|
|
bg += 100;
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
2013-07-15 18:15:41 +00:00
|
|
|
out += bg + ';';
|
2013-06-01 02:09:07 +00:00
|
|
|
} else {
|
2013-07-15 18:15:41 +00:00
|
|
|
out += '48;5;' + bg + ';';
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-15 18:15:41 +00:00
|
|
|
if (fg !== 0x1ff) {
|
|
|
|
fg = this._reduceColor(fg);
|
|
|
|
if (fg < 16) {
|
|
|
|
if (fg < 8) {
|
|
|
|
fg += 30;
|
|
|
|
} else if (fg < 16) {
|
|
|
|
fg -= 8;
|
|
|
|
fg += 90;
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
2013-07-15 18:15:41 +00:00
|
|
|
out += fg + ';';
|
2013-06-01 02:09:07 +00:00
|
|
|
} else {
|
2013-07-15 18:15:41 +00:00
|
|
|
out += '38;5;' + fg + ';';
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out[out.length-1] === ';') out = out.slice(0, -1);
|
|
|
|
|
|
|
|
out += 'm';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-14 08:14:48 +00:00
|
|
|
// Attempt to use ACS for supported characters.
|
2013-07-16 22:50:02 +00:00
|
|
|
// This is not ideal, but it's how ncurses works.
|
|
|
|
// There are a lot of terminals that support ACS
|
|
|
|
// *and UTF8, but do not declare U8. So ACS ends
|
|
|
|
// up being used (slower than utf8). Terminals
|
|
|
|
// that do not support ACS and do not explicitly
|
|
|
|
// support UTF8 get ugly their unicode characters
|
|
|
|
// replaced with really ugly ascii characters.
|
|
|
|
// It is possible there is a terminal out there
|
|
|
|
// somewhere that does not support ACS, but
|
|
|
|
// supports UTF8, but I imagine it's unlikely.
|
|
|
|
// Maybe remove !this.tput.unicode check, however,
|
|
|
|
// this seems to be the way ncurses does it.
|
|
|
|
if (this.tput) {
|
|
|
|
if (this.tput.strings.enter_alt_charset_mode
|
|
|
|
&& this.tput.acscr[ch]
|
|
|
|
&& (!this.tput.brokenACS || !this.tput.unicode)) {
|
|
|
|
ch = this.tput.smacs()
|
|
|
|
+ this.tput.acscr[ch]
|
|
|
|
+ this.tput.rmacs();
|
|
|
|
} else if (!this.tput.strings.enter_alt_charset_mode) {
|
|
|
|
// U8 is not consistently correct. Some terminfo's
|
|
|
|
// terminals that do not declare it may actually
|
|
|
|
// support utf8 (e.g. urxvt), but if the terminal
|
|
|
|
// does not declare support for ACS (and U8), chances
|
|
|
|
// are it does not support UTF8. This is probably
|
|
|
|
// the "safest" way to do this. Should fix things
|
|
|
|
// like sun-color.
|
|
|
|
if (this.tput.numbers.U8 !== 1 && this.utoa[ch]) {
|
|
|
|
ch = this.tput.utoa[ch] || '?';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// } else if (this.program.term('sun') && ch > '~') {
|
|
|
|
// } else if (this.tput.numbers.U8 !== 1 && ch > '~') {
|
|
|
|
// // If this terminal doesn't explicitly support UTF8,
|
|
|
|
// // replace unicode characters with ugly shitty ones.
|
|
|
|
// ch = this.tput.utoa[ch] || '?';
|
|
|
|
// }
|
2013-07-14 01:47:29 +00:00
|
|
|
}
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-07-14 12:02:59 +00:00
|
|
|
// this.program.cup(cy, cx);
|
|
|
|
// if (ch) this.program.hideCursor();
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
this.program.restoreCursor();
|
|
|
|
};
|
|
|
|
|
2013-06-16 09:21:57 +00:00
|
|
|
Screen.prototype._reduceColor = function(col) {
|
|
|
|
if (this.tput) {
|
|
|
|
if (col >= 16 && this.tput.colors <= 16) {
|
2013-07-14 11:43:17 +00:00
|
|
|
col = colors.ccolors[col];
|
2013-06-16 09:21:57 +00:00
|
|
|
} else if (col >= 8 && this.tput.colors <= 8) {
|
|
|
|
col -= 8;
|
|
|
|
} else if (col >= 2 && this.tput.colors <= 2) {
|
|
|
|
col %= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return col;
|
|
|
|
};
|
|
|
|
|
2013-07-15 06:44:11 +00:00
|
|
|
// Convert an SGR string to our own attribute format.
|
|
|
|
Screen.prototype.attrCode = function(code, cur) {
|
|
|
|
var flags = (cur >> 18) & 0x1ff
|
|
|
|
, fg = (cur >> 9) & 0x1ff
|
|
|
|
, bg = cur & 0x1ff
|
|
|
|
, c
|
|
|
|
, i;
|
|
|
|
|
|
|
|
code = /^\x1b\[([\d;]*)m$/.exec(code);
|
|
|
|
if (!code) return cur;
|
|
|
|
|
|
|
|
code = code[1].split(';');
|
|
|
|
if (!code[0]) code[0] = '0';
|
|
|
|
|
|
|
|
for (i = 0; i < code.length; i++) {
|
|
|
|
c = +code[i] || 0;
|
|
|
|
switch (c) {
|
|
|
|
case 0: // normal
|
|
|
|
bg = 0x1ff;
|
|
|
|
fg = 0x1ff;
|
|
|
|
flags = 0;
|
|
|
|
break;
|
|
|
|
case 1: // bold
|
|
|
|
flags |= 1;
|
|
|
|
break;
|
|
|
|
case 22:
|
|
|
|
flags &= ~1;
|
|
|
|
break;
|
|
|
|
case 4: // underline
|
|
|
|
flags |= 2;
|
|
|
|
break;
|
|
|
|
case 24:
|
|
|
|
flags &= ~2;
|
|
|
|
break;
|
|
|
|
case 5: // blink
|
|
|
|
flags |= 4;
|
|
|
|
break;
|
|
|
|
case 25:
|
|
|
|
flags &= ~4;
|
|
|
|
break;
|
|
|
|
case 7: // inverse
|
|
|
|
flags |= 8;
|
|
|
|
break;
|
|
|
|
case 27:
|
|
|
|
flags &= ~8;
|
|
|
|
break;
|
|
|
|
case 8: // invisible
|
|
|
|
flags |= 16;
|
|
|
|
break;
|
|
|
|
case 28:
|
|
|
|
flags &= ~16;
|
|
|
|
break;
|
|
|
|
default: // color
|
|
|
|
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 === 49) {
|
|
|
|
bg = 0x1ff;
|
|
|
|
} else if (c >= 30 && c <= 37) {
|
|
|
|
fg = c - 30;
|
|
|
|
} else if (c >= 90 && c <= 97) {
|
|
|
|
fg = c - 90;
|
|
|
|
fg += 8;
|
|
|
|
} else if (c === 39) {
|
|
|
|
fg = 0x1ff;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (flags << 18) | (fg << 9) | bg;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Convert our own attribute format to an SGR string.
|
|
|
|
Screen.prototype.codeAttr = function(code) {
|
|
|
|
var flags = (code >> 18) & 0x1ff
|
|
|
|
, fg = (code >> 9) & 0x1ff
|
|
|
|
, bg = code & 0x1ff
|
|
|
|
, out = '';
|
|
|
|
|
|
|
|
// 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 (bg !== 0x1ff) {
|
|
|
|
bg = this._reduceColor(bg);
|
|
|
|
if (bg < 16) {
|
|
|
|
if (bg < 8) {
|
|
|
|
bg += 40;
|
|
|
|
} else if (bg < 16) {
|
|
|
|
bg -= 8;
|
|
|
|
bg += 100;
|
|
|
|
}
|
|
|
|
out += bg + ';';
|
|
|
|
} else {
|
|
|
|
out += '48;5;' + bg + ';';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fg !== 0x1ff) {
|
|
|
|
fg = this._reduceColor(fg);
|
|
|
|
if (fg < 16) {
|
|
|
|
if (fg < 8) {
|
|
|
|
fg += 30;
|
|
|
|
} else if (fg < 16) {
|
|
|
|
fg -= 8;
|
|
|
|
fg += 90;
|
|
|
|
}
|
|
|
|
out += fg + ';';
|
|
|
|
} else {
|
|
|
|
out += '38;5;' + fg + ';';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out[out.length-1] === ';') out = out.slice(0, -1);
|
|
|
|
|
|
|
|
return '\x1b[' + out + 'm';
|
|
|
|
};
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
Screen.prototype.focus = function(offset) {
|
2013-06-18 10:08:31 +00:00
|
|
|
var shown = this.input.filter(function(el) {
|
|
|
|
return el.visible;
|
|
|
|
});
|
2013-07-10 17:48:18 +00:00
|
|
|
if (!shown.length || !offset) return;
|
2013-06-06 09:03:25 +00:00
|
|
|
var i = this.input.indexOf(this.focused);
|
|
|
|
if (!~i) return;
|
2013-06-18 10:08:31 +00:00
|
|
|
if (offset > 0) {
|
|
|
|
while (offset--) {
|
|
|
|
if (++i > this.input.length - 1) i = 0;
|
|
|
|
if (!this.input[i].visible) offset++;
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-06-18 10:08:31 +00:00
|
|
|
offset = -offset;
|
|
|
|
while (offset--) {
|
|
|
|
if (--i < 0) i = this.input.length - 1;
|
|
|
|
if (!this.input[i].visible) offset++;
|
|
|
|
}
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
2013-06-20 18:03:31 +00:00
|
|
|
Screen.prototype.saveFocus = function() {
|
|
|
|
return this._savedFocus = this.focused;
|
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.restoreFocus = function() {
|
|
|
|
if (!this._savedFocus) return;
|
|
|
|
this._savedFocus.focus();
|
|
|
|
delete this._savedFocus;
|
|
|
|
return this.focused;
|
|
|
|
};
|
|
|
|
|
2013-06-09 20:52:22 +00:00
|
|
|
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-07-12 10:11:15 +00:00
|
|
|
Screen.prototype.key = function() {
|
|
|
|
return this.program.key.apply(this, arguments);
|
2013-07-04 04:15:09 +00:00
|
|
|
};
|
|
|
|
|
2013-07-12 10:11:15 +00:00
|
|
|
Screen.prototype.onceKey = function() {
|
|
|
|
return this.program.onceKey.apply(this, arguments);
|
2013-07-10 17:48:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.unkey =
|
2013-07-12 10:11:15 +00:00
|
|
|
Screen.prototype.removeKey = function() {
|
|
|
|
return this.program.unkey.apply(this, arguments);
|
2013-07-10 17:48:18 +00:00
|
|
|
};
|
|
|
|
|
2013-07-04 04:15:09 +00:00
|
|
|
Screen.prototype.spawn = function(file, args, options) {
|
|
|
|
if (!Array.isArray(args)) {
|
|
|
|
options = args;
|
|
|
|
args = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var options = options || {}
|
|
|
|
, spawn = require('child_process').spawn
|
|
|
|
, screen = this;
|
|
|
|
|
|
|
|
options.stdio = 'inherit';
|
|
|
|
|
2013-07-12 16:39:08 +00:00
|
|
|
screen.program.saveCursor();
|
2013-07-04 04:15:09 +00:00
|
|
|
screen.program.normalBuffer();
|
|
|
|
screen.program.showCursor();
|
|
|
|
|
2013-07-14 15:06:28 +00:00
|
|
|
var listenedMouse = screen._listenedMouse;
|
|
|
|
if (listenedMouse) {
|
2013-07-04 04:15:09 +00:00
|
|
|
screen.program.disableMouse();
|
|
|
|
}
|
|
|
|
|
2013-07-14 15:06:28 +00:00
|
|
|
var write = screen.program.output.write;
|
|
|
|
screen.program.output.write = function() {};
|
|
|
|
screen.program.input.pause();
|
|
|
|
screen.program.input.setRawMode(false);
|
2013-07-04 04:15:09 +00:00
|
|
|
|
|
|
|
var resume = function() {
|
|
|
|
if (resume.done) return;
|
|
|
|
resume.done = true;
|
|
|
|
|
2013-07-14 15:06:28 +00:00
|
|
|
screen.program.input.setRawMode(true);
|
|
|
|
screen.program.input.resume();
|
|
|
|
screen.program.output.write = write;
|
2013-07-04 04:15:09 +00:00
|
|
|
|
|
|
|
screen.program.alternateBuffer();
|
2013-07-12 16:39:08 +00:00
|
|
|
// Restoring the cursor, or resetting to program.x/y - either works.
|
|
|
|
// Technically, restoring cursor might be more foolproof.
|
|
|
|
// screen.program.cup(screen.program.y, screen.program.x);
|
|
|
|
screen.program.restoreCursor();
|
2013-07-04 04:15:09 +00:00
|
|
|
screen.program.hideCursor();
|
2013-07-14 15:06:28 +00:00
|
|
|
if (listenedMouse) {
|
2013-07-04 04:15:09 +00:00
|
|
|
screen.program.enableMouse();
|
|
|
|
}
|
|
|
|
|
|
|
|
screen.alloc();
|
|
|
|
screen.render();
|
|
|
|
};
|
|
|
|
|
|
|
|
var ps = spawn(file, args, options);
|
|
|
|
|
|
|
|
ps.on('error', resume);
|
|
|
|
|
|
|
|
ps.on('exit', resume);
|
|
|
|
|
|
|
|
return ps;
|
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.exec = function(file, args, options, callback) {
|
|
|
|
var callback = arguments[arguments.length-1]
|
|
|
|
, ps = this.spawn(file, args, options);
|
|
|
|
|
|
|
|
ps.on('error', function(err) {
|
|
|
|
return callback(err, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
ps.on('exit', function(code) {
|
|
|
|
return callback(null, code === 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ps;
|
|
|
|
};
|
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
Screen.prototype.readEditor = function(options, callback) {
|
|
|
|
if (typeof options === 'string') {
|
|
|
|
options = { editor: options };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!callback) {
|
|
|
|
callback = options;
|
|
|
|
options = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
var self = this
|
|
|
|
, fs = require('fs')
|
|
|
|
, editor = options.editor || process.env.EDITOR || 'vi'
|
2013-07-10 17:48:18 +00:00
|
|
|
, name = options.name || process.title || 'blessed'
|
|
|
|
, rnd = Math.random().toString(36).split('.').pop()
|
|
|
|
, file = '/tmp/' + name + '.' + rnd
|
2013-07-04 04:15:09 +00:00
|
|
|
, args = [file]
|
|
|
|
, opt;
|
|
|
|
|
|
|
|
opt = {
|
|
|
|
stdio: 'inherit',
|
|
|
|
env: process.env,
|
|
|
|
cwd: process.env.HOME
|
|
|
|
};
|
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
function writeFile(callback) {
|
|
|
|
if (!options.value) return callback();
|
|
|
|
return fs.writeFile(file, options.value, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeFile(function() {
|
|
|
|
return self.exec(editor, args, opt, function(err, success) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
return fs.readFile(file, 'utf8', function(err, data) {
|
|
|
|
return fs.unlink(file, function() {
|
|
|
|
if (err) return callback(err);
|
|
|
|
return callback(null, data);
|
|
|
|
});
|
2013-07-04 04:15:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-07-12 08:34:32 +00:00
|
|
|
Screen.prototype.setEffects = function(el, fel, over, out, effects, temp) {
|
|
|
|
if (!effects) return;
|
|
|
|
|
|
|
|
var tmp = {};
|
|
|
|
if (temp) el[temp] = tmp;
|
|
|
|
|
|
|
|
if (typeof el !== 'function') {
|
|
|
|
var _el = el;
|
|
|
|
el = function() { return _el; };
|
|
|
|
}
|
|
|
|
|
|
|
|
fel.on(over, function() {
|
|
|
|
Object.keys(effects).forEach(function(key) {
|
|
|
|
var val = effects[key];
|
|
|
|
if (val !== null && typeof val === 'object') {
|
|
|
|
tmp[key] = tmp[key] || {};
|
|
|
|
Object.keys(val).forEach(function(k) {
|
|
|
|
var v = val[k];
|
|
|
|
tmp[key][k] = el()[key][k];
|
2013-07-16 22:50:02 +00:00
|
|
|
el().style[key][k] = v;
|
2013-07-12 08:34:32 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2013-07-16 22:50:02 +00:00
|
|
|
tmp[key] = el().style[key];
|
|
|
|
el().style[key] = val;
|
2013-07-12 08:34:32 +00:00
|
|
|
});
|
|
|
|
el().screen.render();
|
|
|
|
});
|
|
|
|
|
|
|
|
fel.on(out, function() {
|
|
|
|
Object.keys(effects).forEach(function(key) {
|
|
|
|
var val = effects[key];
|
|
|
|
if (val !== null && typeof val === 'object') {
|
|
|
|
tmp[key] = tmp[key] || {};
|
|
|
|
Object.keys(val).forEach(function(k) {
|
2013-07-16 22:50:02 +00:00
|
|
|
if (tmp[key].hasOwnProperty(k)) {
|
|
|
|
el().style[key][k] = tmp[key][k];
|
2013-07-12 08:34:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2013-07-16 22:50:02 +00:00
|
|
|
if (tmp.hasOwnProperty(key)) {
|
|
|
|
el().style[key] = tmp[key];
|
2013-07-12 08:34:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
el().screen.render();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-07-14 15:06:28 +00:00
|
|
|
Screen.prototype.sigtstp = function(callback) {
|
|
|
|
var self = this;
|
|
|
|
this.program.sigtstp(function() {
|
|
|
|
self.alloc();
|
|
|
|
self.render();
|
|
|
|
if (callback) callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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-14 20:58:54 +00:00
|
|
|
if (!(this instanceof Element)) {
|
|
|
|
return new Element(options);
|
|
|
|
}
|
|
|
|
|
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-07-16 22:50:02 +00:00
|
|
|
this.style = options.style;
|
|
|
|
|
|
|
|
if (!this.style) {
|
|
|
|
this.style = {};
|
|
|
|
this.style.fg = options.fg;
|
|
|
|
this.style.bg = options.bg;
|
|
|
|
this.style.bold = options.bold;
|
|
|
|
this.style.underline = options.underline;
|
|
|
|
this.style.blink = options.blink;
|
|
|
|
this.style.inverse = options.inverse;
|
|
|
|
this.style.invisible = options.invisible;
|
|
|
|
}
|
2013-06-01 02:09:07 +00:00
|
|
|
|
2013-06-14 05:38:36 +00:00
|
|
|
this.hidden = options.hidden || false;
|
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-13 09:22:45 +00:00
|
|
|
this.shrink = options.shrink;
|
2013-06-14 00:02:02 +00:00
|
|
|
this.padding = options.padding || 0;
|
2013-06-14 05:38:36 +00:00
|
|
|
|
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-07-17 00:07:46 +00:00
|
|
|
if (this.border.type === 'ascii') this.border.type = 'line';
|
2013-06-01 07:06:04 +00:00
|
|
|
this.border.ch = this.border.ch || ' ';
|
2013-07-16 22:50:02 +00:00
|
|
|
if (!this.border.style) {
|
|
|
|
this.border.style = {};
|
|
|
|
this.border.style.fg = this.border.fg;
|
|
|
|
this.border.style.bg = this.border.bg;
|
|
|
|
}
|
|
|
|
this.style.border = this.border.style;
|
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
|
|
|
|
2013-06-14 01:21:41 +00:00
|
|
|
this.parseTags = options.parseTags || options.tags;
|
2013-06-20 12:18:04 +00:00
|
|
|
|
|
|
|
this.setContent(options.content || '');
|
2013-06-09 20:02:12 +00:00
|
|
|
|
|
|
|
if (options.label) {
|
2013-06-13 09:22:45 +00:00
|
|
|
this.append(new Box({
|
2013-06-09 20:02:12 +00:00
|
|
|
screen: this.screen,
|
|
|
|
content: options.label,
|
|
|
|
left: 2,
|
2013-06-13 09:22:45 +00:00
|
|
|
top: this.border ? 0 : -1,
|
2013-07-12 12:48:55 +00:00
|
|
|
tags: this.parseTags,
|
2013-07-17 00:13:16 +00:00
|
|
|
shrink: true,
|
|
|
|
style: this.style.label
|
2013-06-09 20:02:12 +00:00
|
|
|
}));
|
|
|
|
}
|
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'
|
2013-06-16 09:21:57 +00:00
|
|
|
|| type === 'mouseover'
|
|
|
|
|| type === 'mouseout'
|
2013-06-11 14:27:50 +00:00
|
|
|
|| type === 'mousedown'
|
|
|
|
|| type === 'mouseup'
|
|
|
|
|| type === 'mousewheel'
|
|
|
|
|| type === 'wheeldown'
|
|
|
|
|| type === 'wheelup'
|
2013-06-16 14:31:55 +00:00
|
|
|
|| type === 'mousemove') {
|
2013-06-11 14:27:50 +00:00
|
|
|
self.screen._listenMouse(self);
|
2013-07-04 04:15:09 +00:00
|
|
|
} else if (type === 'keypress' || type.indexOf('key ') === 0) {
|
2013-06-11 14:27:50 +00:00
|
|
|
self.screen._listenKeys(self);
|
|
|
|
}
|
|
|
|
});
|
2013-06-20 11:43:56 +00:00
|
|
|
|
|
|
|
this.on('resize', function() {
|
|
|
|
self.parseContent();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on('attach', function() {
|
|
|
|
self.parseContent();
|
|
|
|
});
|
2013-06-29 17:07:51 +00:00
|
|
|
|
2013-07-12 08:34:32 +00:00
|
|
|
// Legacy
|
2013-07-04 05:10:01 +00:00
|
|
|
if (this.options.hoverBg != null) {
|
|
|
|
this.options.hoverEffects = this.options.hoverEffects || {};
|
|
|
|
this.options.hoverEffects.bg = this.options.hoverBg;
|
2013-06-29 17:07:51 +00:00
|
|
|
}
|
|
|
|
|
2013-07-12 08:34:32 +00:00
|
|
|
[['hoverEffects', 'mouseover', 'mouseout', '_htemp'],
|
|
|
|
['focusEffects', 'focus', 'blur', '_ftemp']].forEach(function(props) {
|
|
|
|
var pname = props[0], over = props[1], out = props[2], temp = props[3];
|
|
|
|
self.screen.setEffects(self, self, over, out, self.options[pname], temp);
|
|
|
|
});
|
2013-07-15 03:11:48 +00:00
|
|
|
|
|
|
|
if (options.focused) {
|
|
|
|
this.focus();
|
|
|
|
}
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Element.prototype.__proto__ = Node.prototype;
|
|
|
|
|
2013-06-25 11:34:10 +00:00
|
|
|
Element.prototype.type = 'element';
|
|
|
|
|
2013-07-16 17:33:55 +00:00
|
|
|
Element.prototype.sattr = function(obj, fg, bg) {
|
|
|
|
var bold = obj.bold
|
|
|
|
, underline = obj.underline
|
|
|
|
, blink = obj.blink
|
|
|
|
, inverse = obj.inverse
|
|
|
|
, invisible = obj.invisible;
|
|
|
|
|
|
|
|
// This used to be a loop, but I decided
|
|
|
|
// to unroll it for performance's sake.
|
|
|
|
if (typeof bold === 'function') bold = bold(this);
|
|
|
|
if (typeof underline === 'function') underline = underline(this);
|
|
|
|
if (typeof blink === 'function') blink = blink(this);
|
|
|
|
if (typeof inverse === 'function') inverse = inverse(this);
|
|
|
|
if (typeof invisible === 'function') invisible = invisible(this);
|
|
|
|
|
|
|
|
if (typeof fg === 'function') fg = fg(this);
|
|
|
|
if (typeof bg === 'function') bg = bg(this);
|
|
|
|
|
|
|
|
return ((((invisible ? 16 : 0) << 18)
|
|
|
|
| ((inverse ? 8 : 0) << 18)
|
|
|
|
| ((blink ? 4 : 0) << 18)
|
|
|
|
| ((underline ? 2 : 0) << 18))
|
|
|
|
| ((bold ? 1 : 0) << 18)
|
|
|
|
| (colors.convert(fg) << 9))
|
|
|
|
| colors.convert(bg);
|
|
|
|
};
|
|
|
|
|
2013-06-24 11:16:02 +00:00
|
|
|
Element.prototype.onScreenEvent = function(type, listener) {
|
|
|
|
var self = this;
|
|
|
|
if (this.parent) {
|
|
|
|
this.screen.on(type, listener);
|
|
|
|
}
|
|
|
|
this.on('attach', function() {
|
|
|
|
self.screen.on(type, listener);
|
|
|
|
});
|
|
|
|
this.on('detach', function() {
|
|
|
|
self.screen.removeListener(type, listener);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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
|
|
|
this.hidden = true;
|
2013-07-04 06:50:50 +00:00
|
|
|
this.clearPos();
|
2013-06-11 18:13:49 +00:00
|
|
|
this.emit('hide');
|
2013-07-12 03:52:46 +00:00
|
|
|
var below = this.screen.history[this.screen.history.length-2];
|
|
|
|
if (below && this.screen.focused === this) below.focus();
|
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;
|
2013-07-12 01:48:30 +00:00
|
|
|
// 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-20 12:18:04 +00:00
|
|
|
Element.prototype.setContent = function(content, noClear) {
|
2013-06-20 17:07:35 +00:00
|
|
|
this.content = content || '';
|
2013-06-20 11:43:56 +00:00
|
|
|
this.parseContent();
|
2013-07-04 06:50:50 +00:00
|
|
|
if (!noClear) this.clearPos();
|
2013-06-09 18:14:42 +00:00
|
|
|
};
|
|
|
|
|
2013-06-20 17:07:35 +00:00
|
|
|
Element.prototype.parseContent = function() {
|
|
|
|
if (this.detached) return false;
|
|
|
|
|
2013-07-14 13:48:17 +00:00
|
|
|
var width = this.width - (this.border ? 2 : 0) - this.padding * 2;
|
2013-06-20 17:07:35 +00:00
|
|
|
if (this._clines == null
|
|
|
|
|| this._clines.width !== width
|
|
|
|
|| this._clines.content !== this.content) {
|
|
|
|
var content = this.content;
|
2013-07-16 08:50:42 +00:00
|
|
|
// Possibly keep this for set/get/insert/etcLine
|
|
|
|
// Although, it may not work since the lines handled
|
|
|
|
// by those methods need to be wrapped. If the line
|
|
|
|
// indexes were only within the context of the box
|
|
|
|
// (e.g. line 0 is always the top of the box) like they
|
|
|
|
// were before, this may be feasible.
|
|
|
|
// this._oclines = content.split('\n');
|
2013-06-20 17:07:35 +00:00
|
|
|
// Could move these 2 lines back to setContent (?)
|
|
|
|
content = content.replace(/\x1b(?!\[[\d;]*m)/g, '');
|
|
|
|
content = this._parseTags(content || '');
|
2013-07-12 01:48:30 +00:00
|
|
|
content = content.replace(/\t/g, ' ');
|
2013-07-16 06:54:29 +00:00
|
|
|
this._clines = wrapContent(content, width,
|
|
|
|
this.parseTags, this.align, this.type === 'textarea');
|
2013-06-20 17:07:35 +00:00
|
|
|
this._clines.width = width;
|
|
|
|
this._clines.content = this.content;
|
|
|
|
this._pcontent = this._clines.join('\n');
|
|
|
|
this.emit('parsed content');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2013-06-14 01:21:41 +00:00
|
|
|
// Convert `{red-fg}foo{/red-fg}` to `\x1b[31mfoo\x1b[39m`.
|
|
|
|
Element.prototype._parseTags = function(text) {
|
|
|
|
if (!this.parseTags) return text;
|
|
|
|
var program = this.screen.program;
|
2013-07-16 05:18:31 +00:00
|
|
|
return text.replace(/{(\/?)([\w\-,;!#]*)}/g, function(tag, slash, color) {
|
2013-06-14 01:21:41 +00:00
|
|
|
if (!color) return slash ? '\x1b[m' : tag;
|
|
|
|
|
|
|
|
color = color.replace(/-/g, ' ');
|
|
|
|
var result = program._attr(color, !slash);
|
|
|
|
|
|
|
|
// Parse error. Just return the original text.
|
|
|
|
if (!/^\x1b\[[\d;]*m$/.test(result)) {
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-06-18 10:08:31 +00:00
|
|
|
Element.prototype.__defineGetter__('visible', function() {
|
|
|
|
var el = this;
|
|
|
|
do {
|
|
|
|
if (el.hidden) return false;
|
|
|
|
} while (el = el.parent);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2013-06-20 11:43:56 +00:00
|
|
|
Element.prototype.__defineGetter__('detached', function() {
|
|
|
|
var el = this;
|
|
|
|
do {
|
2013-07-03 23:47:55 +00:00
|
|
|
if (el.type === 'screen') return false;
|
2013-06-20 11:43:56 +00:00
|
|
|
if (!el.parent) return true;
|
|
|
|
} while (el = el.parent);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2013-07-12 09:59:58 +00:00
|
|
|
Element.prototype.key = function() {
|
2013-07-15 22:45:08 +00:00
|
|
|
return this.screen.program.key.apply(this, arguments);
|
2013-07-12 09:59:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.onceKey = function() {
|
2013-07-15 22:45:08 +00:00
|
|
|
return this.screen.program.onceKey.apply(this, arguments);
|
2013-07-12 09:59:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.unkey =
|
|
|
|
Element.prototype.removeKey = function() {
|
2013-07-15 22:45:08 +00:00
|
|
|
return this.screen.program.unkey.apply(this, arguments);
|
2013-07-04 06:50:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.clearPos = function() {
|
|
|
|
if (this._lastPos && !this._lastPos.cleared) {
|
|
|
|
// optimize by making sure we only clear once.
|
|
|
|
this._lastPos.cleared = true;
|
|
|
|
this.screen.clearRegion(
|
|
|
|
this._lastPos.xi, this._lastPos.xl,
|
|
|
|
this._lastPos.yi, this._lastPos.yl);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
2013-06-16 14:31:55 +00:00
|
|
|
// TODO: Move _getShrinkSize calculation here. This will in turn fix .left.
|
2013-06-01 02:39:11 +00:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
2013-06-16 14:31:55 +00:00
|
|
|
// TODO: Move _getShrinkSize calculation here. This will in turn fix .top.
|
2013-06-01 02:39:11 +00:00
|
|
|
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-07-04 06:50:50 +00:00
|
|
|
this.clearPos();
|
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');
|
2013-07-04 06:50:50 +00:00
|
|
|
this.clearPos();
|
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');
|
2013-07-04 06:50:50 +00:00
|
|
|
this.clearPos();
|
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');
|
2013-07-04 06:50:50 +00:00
|
|
|
this.clearPos();
|
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');
|
2013-07-04 06:50:50 +00:00
|
|
|
this.clearPos();
|
2013-06-11 14:09:57 +00:00
|
|
|
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');
|
2013-07-04 06:50:50 +00:00
|
|
|
this.clearPos();
|
2013-06-11 14:09:57 +00:00
|
|
|
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');
|
2013-07-04 06:50:50 +00:00
|
|
|
this.clearPos();
|
2013-06-11 14:09:57 +00:00
|
|
|
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');
|
2013-07-04 06:50:50 +00:00
|
|
|
this.clearPos();
|
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');
|
2013-07-04 06:50:50 +00:00
|
|
|
this.clearPos();
|
2013-06-11 14:09:57 +00:00
|
|
|
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');
|
2013-07-04 06:50:50 +00:00
|
|
|
this.clearPos();
|
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) {
|
2013-06-14 20:58:54 +00:00
|
|
|
if (!(this instanceof Box)) {
|
|
|
|
return new Box(options);
|
|
|
|
}
|
2013-06-01 02:09:07 +00:00
|
|
|
Element.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Box.prototype.__proto__ = Element.prototype;
|
|
|
|
|
2013-06-25 11:34:10 +00:00
|
|
|
Box.prototype.type = 'box';
|
|
|
|
|
2013-06-16 14:31:55 +00:00
|
|
|
// TODO: Optimize. Move elsewhere.
|
2013-06-16 09:21:57 +00:00
|
|
|
Box.prototype._getShrinkSize = function(content) {
|
|
|
|
return {
|
2013-06-20 12:18:04 +00:00
|
|
|
height: this._clines.length,
|
|
|
|
width: this._clines.reduce(function(current, line) {
|
|
|
|
line = line.replace(/\x1b\[[\d;]*m/g, '');
|
2013-06-16 09:21:57 +00:00
|
|
|
return line.length > current
|
2013-06-20 12:18:04 +00:00
|
|
|
? line.length
|
2013-06-16 09:21:57 +00:00
|
|
|
: current;
|
|
|
|
}, 0)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// Here be dragons.
|
2013-06-16 14:31:55 +00:00
|
|
|
// TODO: Potentially move all calculations performed on
|
|
|
|
// xi/xl/yi/yl here to Element offset and size getters.
|
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-20 12:18:04 +00:00
|
|
|
this.parseContent();
|
|
|
|
|
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
|
2013-06-13 10:30:21 +00:00
|
|
|
, yi_ = this.top
|
|
|
|
, yi
|
2013-06-01 02:09:07 +00:00
|
|
|
, yl = this.screen.rows - this.bottom
|
|
|
|
, cell
|
|
|
|
, attr
|
|
|
|
, ch
|
2013-06-20 12:32:50 +00:00
|
|
|
, content = this._pcontent
|
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
|
2013-06-25 23:23:15 +00:00
|
|
|
, c
|
|
|
|
, rtop
|
2013-06-25 23:17:21 +00:00
|
|
|
, visible
|
|
|
|
, hw
|
|
|
|
, h
|
|
|
|
, w
|
|
|
|
, xll
|
|
|
|
, yll
|
2013-06-25 23:23:15 +00:00
|
|
|
, ret
|
|
|
|
, cci;
|
2013-06-25 23:17:21 +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-13 10:30:21 +00:00
|
|
|
yl = yi_ + this.height;
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 11:34:10 +00:00
|
|
|
// Check to make sure we're visible and inside of the visible scroll area.
|
|
|
|
if (this.parent.childBase != null && (!this.parent.items || ~this.parent.items.indexOf(this))) {
|
2013-06-25 23:23:15 +00:00
|
|
|
rtop = this.rtop - (this.parent.border ? 1 : 0);
|
|
|
|
visible = this.parent.height - (this.parent.border ? 2 : 0);
|
2013-06-06 09:03:25 +00:00
|
|
|
|
2013-06-13 10:30:21 +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-06 09:03:25 +00:00
|
|
|
|
|
|
|
if (rtop - this.parent.childBase < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2013-06-25 23:23:15 +00:00
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
if (rtop - this.parent.childBase >= visible) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 12:55:52 +00:00
|
|
|
// TODO: Check for 'center', recalculate yi, and xi. Better
|
|
|
|
// yet, simply move this check into this.left/width/etc.
|
2013-06-16 09:21:57 +00:00
|
|
|
if (this.shrink) {
|
2013-06-25 23:23:15 +00:00
|
|
|
hw = this._getShrinkSize(content);
|
|
|
|
h = hw.height;
|
|
|
|
w = hw.width;
|
|
|
|
xll = xl;
|
|
|
|
yll = yl;
|
|
|
|
|
2013-06-20 12:55:52 +00:00
|
|
|
if (this.options.width == null
|
|
|
|
&& (this.options.left == null
|
|
|
|
|| this.options.right == null)) {
|
|
|
|
if (this.options.left == null && this.options.right != null) {
|
2013-07-14 13:48:17 +00:00
|
|
|
xi_ = xl - w - (this.border ? 2 : 0) - this.padding * 2;
|
2013-06-20 12:55:52 +00:00
|
|
|
} else {
|
2013-07-14 13:48:17 +00:00
|
|
|
xl = xi_ + w + (this.border ? 2 : 0) + this.padding * 2;
|
2013-06-20 12:55:52 +00:00
|
|
|
}
|
2013-06-16 09:21:57 +00:00
|
|
|
}
|
2013-06-25 23:23:15 +00:00
|
|
|
|
2013-06-20 12:55:52 +00:00
|
|
|
if (this.options.height == null
|
|
|
|
&& (this.options.top == null
|
|
|
|
|| this.options.bottom == null)
|
|
|
|
&& this.childBase == null) {
|
2013-06-20 12:18:04 +00:00
|
|
|
if (this.options.top == null && this.options.bottom != null) {
|
2013-07-14 13:48:17 +00:00
|
|
|
yi_ = yl - h - (this.border ? 2 : 0) - this.padding * 2;
|
2013-06-20 12:18:04 +00:00
|
|
|
} else {
|
2013-07-14 13:48:17 +00:00
|
|
|
yl = yi_ + h + (this.border ? 2 : 0) + this.padding * 2;
|
2013-06-20 12:18:04 +00:00
|
|
|
}
|
2013-06-16 09:21:57 +00:00
|
|
|
}
|
2013-06-25 23:23:15 +00:00
|
|
|
|
2013-06-20 13:12:39 +00:00
|
|
|
// Recenter shrunken elements.
|
|
|
|
if (xl < xll && this.options.left === 'center') {
|
|
|
|
xll = (xll - xl) / 2 | 0;
|
|
|
|
xi_ += xll;
|
|
|
|
xl += xll;
|
|
|
|
}
|
2013-06-25 23:23:15 +00:00
|
|
|
|
2013-06-20 13:12:39 +00:00
|
|
|
if (yl < yll && this.options.top === 'center') {
|
|
|
|
yll = (yll - yl) / 2 | 0;
|
|
|
|
yi_ += yll;
|
|
|
|
yl += yll;
|
|
|
|
}
|
2013-06-16 09:21:57 +00:00
|
|
|
}
|
|
|
|
|
2013-07-10 17:48:18 +00:00
|
|
|
// TODO:
|
|
|
|
// Calculate whether we moved/resized by checking the previous _lastPos.
|
|
|
|
// Maybe clear based on that. Possibly emit events here.
|
2013-06-25 23:23:15 +00:00
|
|
|
ret = this._lastPos = {
|
2013-06-13 09:14:14 +00:00
|
|
|
xi: xi_,
|
2013-06-06 09:03:25 +00:00
|
|
|
xl: xl,
|
2013-06-13 10:30:21 +00:00
|
|
|
yi: yi_,
|
2013-06-06 09:03:25 +00:00
|
|
|
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-07-16 22:50:02 +00:00
|
|
|
? this.sattr(this.style.border, this.style.border.fg, this.style.border.bg)
|
2013-06-06 14:24:03 +00:00
|
|
|
: 0;
|
|
|
|
|
2013-07-16 22:50:02 +00:00
|
|
|
dattr = this.sattr(this.style, this.style.fg, this.style.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-20 16:48:12 +00:00
|
|
|
if (this.contentIndex != null && this.childBase > 0 && this._clines) {
|
2013-06-25 23:23:15 +00:00
|
|
|
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') {
|
2013-06-14 05:38:36 +00:00
|
|
|
if (c = /^\x1b\[[\d;]*m/.exec(content.substring(cci))) {
|
2013-07-15 06:44:11 +00:00
|
|
|
attr = this.screen.attrCode(c[0], attr);
|
2013-06-10 01:19:32 +00:00
|
|
|
cci += c[0].length - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 10:30:21 +00:00
|
|
|
if (this.border) yi_++, yl--, xi_++, xl--;
|
|
|
|
|
2013-07-17 01:19:44 +00:00
|
|
|
if (this.padding || this.options.valign) {
|
|
|
|
this.screen.fillRegion(dattr, ' ', xi_, xl, yi_, yl);
|
|
|
|
}
|
|
|
|
|
2013-06-14 00:02:02 +00:00
|
|
|
if (this.padding) {
|
|
|
|
yi_ += this.padding, yl -= this.padding;
|
|
|
|
xi_ += this.padding, xl -= this.padding;
|
|
|
|
}
|
|
|
|
|
2013-07-17 01:19:44 +00:00
|
|
|
if (this.options.valign === 'middle' || this.options.valign === 'bottom') {
|
|
|
|
//visible = (yl - yi_) - (this.border ? 2 : 0) - this.padding;
|
|
|
|
visible = yl - yi_;
|
|
|
|
if (this._clines.length < visible) {
|
|
|
|
if (this.options.valign === 'middle') {
|
|
|
|
visible = visible / 2 | 0;
|
|
|
|
visible -= this._clines.length / 2 | 0;
|
|
|
|
} else if (this.options.valign === 'bottom') {
|
|
|
|
visible -= this._clines.length;
|
|
|
|
}
|
|
|
|
yi_ += visible;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 10:30:21 +00:00
|
|
|
outer:
|
|
|
|
for (yi = yi_; 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-13 19:58:00 +00:00
|
|
|
cell = lines[yi][xi];
|
|
|
|
if (!cell) break;
|
|
|
|
|
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-14 05:38:36 +00:00
|
|
|
if (c = /^\x1b\[[\d;]*m/.exec(content.substring(ci - 1))) {
|
2013-06-06 14:24:03 +00:00
|
|
|
ci += c[0].length - 1;
|
2013-07-15 06:44:11 +00:00
|
|
|
attr = this.screen.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-14 05:38:36 +00:00
|
|
|
} else {
|
|
|
|
break;
|
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') {
|
2013-06-16 09:21:57 +00:00
|
|
|
// If we're on the first cell and we find a newline and the last cell
|
|
|
|
// of the last line was not a newline, let's just treat this like the
|
|
|
|
// newline was already "counted".
|
|
|
|
if (xi === xi_ && yi !== yi_ && content[ci-2] !== '\n') {
|
|
|
|
xi--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// this.screen.fillRegion(attr, ' ', xi, xl, yi, yi + 1);
|
|
|
|
// continue outer;
|
2013-06-06 12:57:50 +00:00
|
|
|
ch = ' ';
|
2013-06-13 10:30:21 +00:00
|
|
|
for (; xi < xl; xi++) {
|
2013-06-13 19:58:00 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-06-06 12:57:50 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2013-07-04 06:50:50 +00:00
|
|
|
// if (ch < ' ') ch = ' ';
|
2013-06-06 09:33:10 +00:00
|
|
|
|
2013-06-13 19:58:00 +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-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 23:17:21 +00:00
|
|
|
h = this.items ? this.items.length : this._clines.length;
|
|
|
|
if (this.scrollbar && (yl - yi_) < h) {
|
|
|
|
xi = xl - 1;
|
2013-06-25 23:23:15 +00:00
|
|
|
if (this.scrollbar.ignoreBorder && this.border) xi++;
|
2013-07-12 12:48:55 +00:00
|
|
|
if (this.selected == null) {
|
|
|
|
// TODO: Fix this - doesn't work with lists (and possibly scrollabletext).
|
2013-07-14 13:48:17 +00:00
|
|
|
yi = h - (yl - yi_) - (this.border ? 2 : 0) - this.padding * 2;
|
2013-07-12 12:48:55 +00:00
|
|
|
yi = yi_ + (((yl - yi_) * (this.childBase / yi)) | 0);
|
|
|
|
} else {
|
|
|
|
yi = this.selected / h;
|
|
|
|
yi = yi_ + ((yl - yi_) * yi | 0);
|
|
|
|
}
|
2013-06-25 23:17:21 +00:00
|
|
|
cell = lines[yi] && lines[yi][xi];
|
|
|
|
if (cell) {
|
|
|
|
ch = this.scrollbar.ch || ' ';
|
2013-07-16 22:50:02 +00:00
|
|
|
//attr = this.sattr(this.style,
|
|
|
|
// this.scrollbar.style.fg || this.style.fg,
|
|
|
|
// this.scrollbar.style.bg || this.style.bg);
|
|
|
|
attr = this.sattr(this.style,
|
|
|
|
this.style.scrollbar.fg || this.style.fg,
|
|
|
|
this.style.scrollbar.bg || this.style.bg);
|
2013-06-25 23:17:21 +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-14 00:02:02 +00:00
|
|
|
// This seems redundant, but we need to draw the
|
|
|
|
// border second because of the `shrink` option.
|
|
|
|
if (this.border) yi_--, yl++, xi_--, xl++;
|
|
|
|
|
|
|
|
if (this.padding) {
|
|
|
|
yi_ -= this.padding, yl += this.padding;
|
|
|
|
xi_ -= this.padding, xl += this.padding;
|
|
|
|
}
|
|
|
|
|
2013-07-17 01:19:44 +00:00
|
|
|
/*
|
2013-07-17 01:07:00 +00:00
|
|
|
if (this.padding) {
|
|
|
|
// Set padding to green for debugging:
|
|
|
|
// dattr = (dattr & ~0x1ff) | colors.convert('green');
|
|
|
|
// top
|
|
|
|
this.screen.fillRegion(dattr, ' ',
|
|
|
|
xi_ + (this.border ? 1 : 0),
|
|
|
|
xl - (this.border ? 1 : 0),
|
|
|
|
yi_ + (this.border ? 1 : 0),
|
|
|
|
yi_ + 0 + (this.border ? 1 : 0) + this.padding);
|
|
|
|
// bottom
|
|
|
|
this.screen.fillRegion(dattr, ' ',
|
|
|
|
xi_ + (this.border ? 1 : 0),
|
|
|
|
xl - (this.border ? 1 : 0),
|
|
|
|
yl - 0 - (this.border ? 1 : 0) - this.padding,
|
|
|
|
yl - (this.border ? 1 : 0));
|
|
|
|
// left
|
|
|
|
this.screen.fillRegion(dattr, ' ',
|
|
|
|
xi_ + (this.border ? 1 : 0),
|
|
|
|
xi_ + 0 + (this.border ? 1 : 0) + this.padding,
|
|
|
|
yi_ + (this.border ? 1 : 0),
|
|
|
|
yl - (this.border ? 1 : 0));
|
|
|
|
// right
|
|
|
|
this.screen.fillRegion(dattr, ' ',
|
|
|
|
xl - 0 - (this.border ? 1 : 0) - this.padding,
|
|
|
|
xl - (this.border ? 1 : 0),
|
|
|
|
yi_ + (this.border ? 1 : 0),
|
|
|
|
yl - (this.border ? 1 : 0));
|
|
|
|
}
|
2013-07-17 01:19:44 +00:00
|
|
|
*/
|
2013-07-17 01:07:00 +00:00
|
|
|
|
2013-06-13 10:30:21 +00:00
|
|
|
if (this.border) {
|
|
|
|
yi = yi_;
|
|
|
|
for (xi = xi_; xi < xl; xi++) {
|
2013-06-13 19:58:00 +00:00
|
|
|
if (!lines[yi]) break;
|
2013-07-17 00:07:46 +00:00
|
|
|
if (this.border.type === 'line') {
|
2013-06-13 10:30:21 +00:00
|
|
|
if (xi === xi_) ch = '┌';
|
|
|
|
else if (xi === xl - 1) ch = '┐';
|
|
|
|
else ch = '─';
|
2013-06-13 19:58:00 +00:00
|
|
|
} else if (this.border.type === 'bg') {
|
|
|
|
ch = this.border.ch;
|
|
|
|
}
|
|
|
|
cell = lines[yi][xi];
|
|
|
|
if (!cell) break;
|
|
|
|
if (battr !== cell[0] || ch !== cell[1]) {
|
|
|
|
lines[yi][xi][0] = battr;
|
|
|
|
lines[yi][xi][1] = ch;
|
|
|
|
lines[yi].dirty = true;
|
2013-06-13 10:30:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
yi = yi_ + 1;
|
|
|
|
for (; yi < yl; yi++) {
|
2013-06-13 19:58:00 +00:00
|
|
|
if (!lines[yi]) break;
|
2013-07-17 00:07:46 +00:00
|
|
|
if (this.border.type === 'line') {
|
2013-06-13 19:58:00 +00:00
|
|
|
ch = '│';
|
|
|
|
} else if (this.border.type === 'bg') {
|
|
|
|
ch = this.border.ch;
|
|
|
|
}
|
|
|
|
cell = lines[yi][xi_];
|
|
|
|
if (!cell) break;
|
|
|
|
if (battr !== cell[0] || ch !== cell[1]) {
|
|
|
|
lines[yi][xi_][0] = battr;
|
|
|
|
lines[yi][xi_][1] = ch;
|
|
|
|
lines[yi].dirty = true;
|
|
|
|
}
|
|
|
|
cell = lines[yi][xl - 1];
|
|
|
|
if (!cell) break;
|
|
|
|
if (battr !== cell[0] || ch !== cell[1]) {
|
|
|
|
lines[yi][xl - 1][0] = battr;
|
|
|
|
lines[yi][xl - 1][1] = ch;
|
|
|
|
lines[yi].dirty = true;
|
|
|
|
}
|
2013-06-13 10:30:21 +00:00
|
|
|
}
|
|
|
|
yi = yl - 1;
|
|
|
|
for (xi = xi_; xi < xl; xi++) {
|
2013-06-13 19:58:00 +00:00
|
|
|
if (!lines[yi]) break;
|
2013-07-17 00:07:46 +00:00
|
|
|
if (this.border.type === 'line') {
|
2013-06-13 10:30:21 +00:00
|
|
|
if (xi === xi_) ch = '└';
|
|
|
|
else if (xi === xl - 1) ch = '┘';
|
|
|
|
else ch = '─';
|
2013-06-13 19:58:00 +00:00
|
|
|
} else if (this.border.type === 'bg') {
|
|
|
|
ch = this.border.ch;
|
|
|
|
}
|
|
|
|
cell = lines[yi][xi];
|
|
|
|
if (!cell) break;
|
|
|
|
if (battr !== cell[0] || ch !== cell[1]) {
|
|
|
|
lines[yi][xi][0] = battr;
|
|
|
|
lines[yi][xi][1] = ch;
|
|
|
|
lines[yi].dirty = true;
|
2013-06-13 10:30:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
this.children.forEach(function(el) {
|
2013-06-29 18:09:22 +00:00
|
|
|
if (el.screen._ci !== -1) {
|
2013-07-12 03:52:46 +00:00
|
|
|
el.index = el.screen._ci++;
|
2013-06-29 18:09:22 +00:00
|
|
|
}
|
2013-06-01 02:09:07 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2013-06-20 16:48:12 +00:00
|
|
|
// Create a much more efficient rendering by using insert-line,
|
|
|
|
// delete-line, and change screen region codes when possible.
|
2013-06-25 11:34:10 +00:00
|
|
|
// NOTE: If someone does:
|
|
|
|
// box.left = box.right = 0;
|
|
|
|
// screen.render();
|
|
|
|
// box.left++;
|
|
|
|
// box.insertTop('foobar');
|
|
|
|
// Things will break because we're using _lastPos instead of render(true).
|
|
|
|
// Maybe _lastPos could be updated on .left, .right, etc setters?
|
2013-07-12 08:52:14 +00:00
|
|
|
|
|
|
|
Box.prototype.insertLine = function(i, line) {
|
2013-07-15 22:45:08 +00:00
|
|
|
var pos = this._lastPos || 0;
|
|
|
|
|
2013-07-15 09:40:44 +00:00
|
|
|
if (typeof line === 'string') line = [line];
|
|
|
|
|
2013-07-15 22:45:08 +00:00
|
|
|
if (i > this._clines.length) {
|
|
|
|
for (var j = this._clines.length; j < i; j++) this._clines.push('');
|
|
|
|
}
|
|
|
|
|
|
|
|
i = Math.max(i, 0);
|
|
|
|
//i = Math.min(i, this._clines.length);
|
|
|
|
|
|
|
|
var height = pos.yl - pos.yi - (this.border ? 2 : 0) - this.padding * 2
|
|
|
|
, base = this.childBase || 0
|
|
|
|
, visible = i >= base && i - base < height;
|
|
|
|
|
|
|
|
if (pos && visible && this.screen.cleanSides(this)) {
|
2013-07-15 09:40:44 +00:00
|
|
|
this.screen.insertLine(line.length,
|
2013-07-15 22:45:08 +00:00
|
|
|
pos.yi + (this.border ? 1 : 0) + this.padding + i - base,
|
|
|
|
pos.yi,
|
|
|
|
pos.yl - (this.border ? 1 : 0) - this.padding - 1);
|
2013-07-12 08:52:14 +00:00
|
|
|
}
|
2013-07-15 09:40:44 +00:00
|
|
|
|
|
|
|
line.forEach(function(line, j) {
|
2013-07-15 22:45:08 +00:00
|
|
|
this._clines.splice(i + j, 0, line);
|
2013-07-15 09:40:44 +00:00
|
|
|
}, this);
|
|
|
|
|
2013-07-12 08:52:14 +00:00
|
|
|
this.setContent(this._clines.join('\n'), true);
|
|
|
|
};
|
|
|
|
|
2013-07-15 09:40:44 +00:00
|
|
|
Box.prototype.deleteLine = function(i, n) {
|
2013-07-15 22:45:08 +00:00
|
|
|
var pos = this._lastPos || 0;
|
|
|
|
|
2013-07-15 09:40:44 +00:00
|
|
|
var reset = true
|
|
|
|
, n = n || 1;
|
|
|
|
|
2013-07-15 22:45:08 +00:00
|
|
|
i = Math.max(i, 0);
|
|
|
|
i = Math.min(i, this._clines.length - 1);
|
|
|
|
|
|
|
|
var height = pos.yl - pos.yi - (this.border ? 2 : 0) - this.padding * 2
|
|
|
|
, base = this.childBase || 0
|
|
|
|
, visible = i >= base && i - base < height;
|
|
|
|
|
|
|
|
if (pos && visible && this.screen.cleanSides(this)) {
|
2013-07-15 09:40:44 +00:00
|
|
|
this.screen.deleteLine(n,
|
2013-07-15 22:45:08 +00:00
|
|
|
pos.yi + (this.border ? 1 : 0) + this.padding + i - base,
|
|
|
|
pos.yi,
|
|
|
|
pos.yl - (this.border ? 1 : 0) - this.padding - 1);
|
2013-07-12 08:52:14 +00:00
|
|
|
reset = false;
|
|
|
|
}
|
2013-07-15 09:40:44 +00:00
|
|
|
|
|
|
|
while (n--) {
|
2013-07-15 22:45:08 +00:00
|
|
|
this._clines.splice(i, 1);
|
2013-07-15 09:40:44 +00:00
|
|
|
}
|
|
|
|
|
2013-07-12 08:52:14 +00:00
|
|
|
this.setContent(this._clines.join('\n'), reset);
|
|
|
|
};
|
|
|
|
|
|
|
|
Box.prototype.insertTop = function(line) {
|
2013-07-15 22:45:08 +00:00
|
|
|
return this.insertLine((this.childBase || 0) + 0, line);
|
2013-07-12 08:52:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Box.prototype.insertBottom = function(line) {
|
2013-07-15 22:45:08 +00:00
|
|
|
return this.insertLine((this.childBase || 0)
|
|
|
|
+ this.height - (this.border ? 2 : 0) - this.padding * 2, line);
|
2013-07-12 08:52:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Box.prototype.deleteTop = function() {
|
2013-07-15 22:45:08 +00:00
|
|
|
return this.deleteLine((this.childBase || 0) + 0);
|
2013-07-12 08:52:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Box.prototype.deleteBottom = function() {
|
2013-07-15 22:45:08 +00:00
|
|
|
return this.deleteLine((this.childBase || 0)
|
|
|
|
+ this.height - (this.border ? 2 : 0) - this.padding * 2);
|
2013-07-12 08:52:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Box.prototype.setLine = function(i, line) {
|
2013-07-15 22:45:08 +00:00
|
|
|
if (i > this._clines.length) {
|
|
|
|
for (var j = this._clines.length; j < i; j++) this._clines.push('');
|
|
|
|
}
|
2013-07-12 08:52:14 +00:00
|
|
|
i = Math.max(i, 0);
|
2013-07-15 22:45:08 +00:00
|
|
|
//i = Math.min(i, this._clines.length);
|
|
|
|
this._clines[(this.childBase || 0) + i] = line;
|
2013-07-12 08:52:14 +00:00
|
|
|
return this.setContent(this._clines.join('\n'), true);
|
|
|
|
};
|
|
|
|
|
|
|
|
Box.prototype.getLine = function(i) {
|
|
|
|
i = Math.max(i, 0);
|
2013-07-15 22:45:08 +00:00
|
|
|
i = Math.min(i, this._clines.length);
|
|
|
|
return this._clines[(this.childBase || 0) + i];
|
2013-07-12 08:52:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Box.prototype.clearLine = function(i) {
|
2013-07-15 22:45:08 +00:00
|
|
|
i = Math.min(i, this._clines.length - 1);
|
2013-07-12 08:52:14 +00:00
|
|
|
return this.setLine(i, '');
|
|
|
|
};
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Text
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Text(options) {
|
2013-07-12 08:34:32 +00:00
|
|
|
if (!(this instanceof Text)) {
|
|
|
|
return new Text(options);
|
|
|
|
}
|
2013-06-14 08:35:45 +00:00
|
|
|
options.shrink = true;
|
|
|
|
Box.call(this, options);
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
2013-06-14 08:35:45 +00:00
|
|
|
Text.prototype.__proto__ = Box.prototype;
|
2013-06-01 02:09:07 +00:00
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
/**
|
|
|
|
* Line
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Line(options) {
|
2013-07-14 13:05:42 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-06-14 20:58:54 +00:00
|
|
|
if (!(this instanceof Line)) {
|
|
|
|
return new Line(options);
|
|
|
|
}
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
var orientation = options.orientation || 'vertical';
|
|
|
|
delete options.orientation;
|
|
|
|
|
|
|
|
if (orientation === 'vertical') {
|
|
|
|
options.width = 1;
|
|
|
|
} else {
|
|
|
|
options.height = 1;
|
|
|
|
}
|
|
|
|
|
2013-07-17 00:07:46 +00:00
|
|
|
this.ch = !options.type || options.type === 'line'
|
2013-07-14 13:05:42 +00:00
|
|
|
? orientation === 'horizontal' ? '─' : '│'
|
|
|
|
: options.ch || ' ';
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
options.border = {
|
|
|
|
type: 'bg',
|
2013-07-14 13:05:42 +00:00
|
|
|
get fg() { return self.fg; },
|
|
|
|
get bg() { return self.bg; },
|
|
|
|
get ch() { return self.ch; },
|
|
|
|
set fg(c) { self.fg = c; },
|
|
|
|
set bg(c) { self.bg = c; },
|
|
|
|
set ch(c) { self.ch = c; }
|
2013-06-06 09:03:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
delete options.fg;
|
2013-07-14 13:05:42 +00:00
|
|
|
delete options.bg;
|
2013-06-06 09:03:25 +00:00
|
|
|
delete options.ch;
|
|
|
|
|
|
|
|
Box.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Line.prototype.__proto__ = Box.prototype;
|
|
|
|
|
2013-06-25 11:34:10 +00:00
|
|
|
Line.prototype.type = 'line';
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* ScrollableBox
|
|
|
|
*/
|
|
|
|
|
|
|
|
function ScrollableBox(options) {
|
2013-06-14 20:58:54 +00:00
|
|
|
if (!(this instanceof ScrollableBox)) {
|
|
|
|
return new ScrollableBox(options);
|
|
|
|
}
|
2013-06-25 23:17:21 +00:00
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
Box.call(this, options);
|
2013-06-25 23:17:21 +00:00
|
|
|
|
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-25 23:17:21 +00:00
|
|
|
|
|
|
|
this.scrollbar = options.scrollbar;
|
|
|
|
if (this.scrollbar) {
|
|
|
|
this.scrollbar.ch = this.scrollbar.ch || ' ';
|
2013-07-16 22:50:02 +00:00
|
|
|
if (!this.scrollbar.style) {
|
|
|
|
this.scrollbar.style = {};
|
|
|
|
this.scrollbar.style.fg = this.scrollbar.fg;
|
|
|
|
this.scrollbar.style.bg = this.scrollbar.bg;
|
|
|
|
}
|
|
|
|
this.style.scrollbar = this.scrollbar.style;
|
2013-06-25 23:17:21 +00:00
|
|
|
}
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ScrollableBox.prototype.__proto__ = Box.prototype;
|
|
|
|
|
2013-06-25 11:34:10 +00:00
|
|
|
ScrollableBox.prototype.type = 'scrollable-box';
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
ScrollableBox.prototype.scroll = function(offset) {
|
2013-07-14 16:04:48 +00:00
|
|
|
var visible = this.height - (this.border ? 2 : 0) - this.padding * 2
|
|
|
|
, base = this.childBase
|
|
|
|
, d
|
|
|
|
, p
|
|
|
|
, t
|
|
|
|
, b;
|
2013-07-12 01:48:30 +00:00
|
|
|
|
2013-06-18 15:52:48 +00:00
|
|
|
// Maybe do for lists:
|
2013-07-12 01:48:30 +00:00
|
|
|
// if (this.items) visible = Math.min(this.items.length, visible);
|
2013-06-06 09:03:25 +00:00
|
|
|
if (this.alwaysScroll) {
|
|
|
|
// Semi-workaround
|
|
|
|
this.childOffset = offset > 0
|
|
|
|
? visible - 1 + offset
|
|
|
|
: offset;
|
|
|
|
} else {
|
|
|
|
this.childOffset += offset;
|
|
|
|
}
|
2013-07-12 01:48:30 +00:00
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
if (this.childOffset > visible - 1) {
|
2013-07-14 16:04:48 +00:00
|
|
|
d = this.childOffset - (visible - 1);
|
2013-06-06 09:03:25 +00:00
|
|
|
this.childOffset -= d;
|
|
|
|
this.childBase += d;
|
|
|
|
} else if (this.childOffset < 0) {
|
2013-07-14 16:04:48 +00:00
|
|
|
d = this.childOffset;
|
2013-06-06 09:03:25 +00:00
|
|
|
this.childOffset += -d;
|
|
|
|
this.childBase += d;
|
|
|
|
}
|
2013-07-12 01:48:30 +00:00
|
|
|
|
|
|
|
if (this.childBase < 0) {
|
|
|
|
this.childBase = 0;
|
|
|
|
} else if (this.childBase > this.baseLimit) {
|
|
|
|
this.childBase = this.baseLimit;
|
|
|
|
}
|
|
|
|
|
2013-07-14 16:04:48 +00:00
|
|
|
// Optimize scrolling with CSR + IL/DL.
|
|
|
|
p = this._lastPos;
|
2013-07-14 16:39:03 +00:00
|
|
|
if (this.childBase !== base && this.screen.cleanSides(this)) {
|
2013-07-14 16:04:48 +00:00
|
|
|
t = p.yi + (this.border ? 1 : 0) + this.padding;
|
|
|
|
b = p.yl - (this.border ? 1 : 0) - this.padding - 1;
|
|
|
|
d = this.childBase - base;
|
|
|
|
|
2013-07-15 06:44:11 +00:00
|
|
|
// var attr = this.screen.olines[t][p.xi + (this.border ? 1 : 0) + this.padding][0];
|
|
|
|
// this.screen.program.write(this.screen.codeAttr(attr, this.screen));
|
|
|
|
|
2013-07-14 16:04:48 +00:00
|
|
|
if (d > 0 && d < visible) {
|
|
|
|
// scrolled down
|
|
|
|
this.screen.deleteLine(d, t, t, b);
|
|
|
|
} else if (d < 0 && -d < visible) {
|
|
|
|
// scrolled up
|
|
|
|
d = -d;
|
|
|
|
this.screen.insertLine(d, t, t, b);
|
|
|
|
}
|
2013-07-15 06:44:11 +00:00
|
|
|
|
|
|
|
// this.screen.program.sgr0();
|
2013-07-14 16:04:48 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 16:48:37 +00:00
|
|
|
this.emit('scroll');
|
2013-06-06 09:03:25 +00:00
|
|
|
};
|
|
|
|
|
2013-06-25 22:31:13 +00:00
|
|
|
ScrollableBox.prototype.resetScroll = function() {
|
|
|
|
this.childOffset = 0;
|
|
|
|
this.childBase = 0;
|
|
|
|
};
|
|
|
|
|
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-14 20:58:54 +00:00
|
|
|
if (!(this instanceof List)) {
|
|
|
|
return new List(options);
|
|
|
|
}
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
ScrollableBox.call(this, options);
|
2013-06-01 10:47:18 +00:00
|
|
|
|
|
|
|
this.items = [];
|
2013-06-25 11:34:10 +00:00
|
|
|
this.ritems = [];
|
2013-06-01 02:09:07 +00:00
|
|
|
this.selected = 0;
|
|
|
|
|
2013-07-16 17:33:55 +00:00
|
|
|
if (!this.style.selected) {
|
|
|
|
this.style.selected = {};
|
2013-07-16 22:50:02 +00:00
|
|
|
this.style.selected.bg = options.selectedBg;
|
|
|
|
this.style.selected.fg = options.selectedFg;
|
2013-07-16 17:33:55 +00:00
|
|
|
this.style.selected.bold = options.selectedBold;
|
|
|
|
this.style.selected.underline = options.selectedUnderline;
|
|
|
|
this.style.selected.blink = options.selectedBlink;
|
|
|
|
this.style.selected.inverse = options.selectedInverse;
|
|
|
|
this.style.selected.invisible = options.selectedInvisible;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.style.item) {
|
|
|
|
this.style.item = {};
|
2013-07-16 22:50:02 +00:00
|
|
|
this.style.item.bg = options.itemBg;
|
|
|
|
this.style.item.fg = options.itemFg;
|
2013-07-16 17:33:55 +00:00
|
|
|
this.style.item.bold = options.itemBold;
|
|
|
|
this.style.item.underline = options.itemUnderline;
|
|
|
|
this.style.item.blink = options.itemBlink;
|
|
|
|
this.style.item.inverse = options.itemInverse;
|
|
|
|
this.style.item.invisible = options.itemInvisible;
|
|
|
|
}
|
2013-07-16 22:50:02 +00:00
|
|
|
|
|
|
|
// Legacy: for apps written before the addition of item attributes.
|
|
|
|
['bg', 'fg', 'bold', 'underline',
|
|
|
|
'blink', 'inverse', 'invisible'].forEach(function(name) {
|
|
|
|
if (self.style[name] != null && self.style.item[name] == null) {
|
|
|
|
self.style.item[name] = self.style[name];
|
|
|
|
}
|
|
|
|
});
|
2013-07-16 17:33:55 +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) {
|
2013-06-25 11:34:10 +00:00
|
|
|
this.ritems = options.items;
|
2013-06-01 07:06:04 +00:00
|
|
|
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) {
|
2013-06-18 15:52:48 +00:00
|
|
|
this.on('wheeldown', function(data) {
|
2013-06-06 15:27:19 +00:00
|
|
|
self.select(self.selected + 2);
|
|
|
|
self.screen.render();
|
|
|
|
});
|
|
|
|
|
2013-06-18 15:52:48 +00:00
|
|
|
this.on('wheelup', function(data) {
|
2013-06-06 15:27:19 +00:00
|
|
|
self.select(self.selected - 2);
|
|
|
|
self.screen.render();
|
|
|
|
});
|
|
|
|
}
|
2013-06-11 16:48:37 +00:00
|
|
|
|
2013-06-18 15:52:48 +00:00
|
|
|
if (options.keys) {
|
|
|
|
this.on('keypress', function(ch, key) {
|
|
|
|
if (key.name === 'up' || (options.vi && key.name === 'k')) {
|
|
|
|
self.up();
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (key.name === 'down' || (options.vi && key.name === 'j')) {
|
|
|
|
self.down();
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
2013-07-03 23:46:06 +00:00
|
|
|
if (key.name === 'enter' || (options.vi && key.name === 'l' && !key.shift)) {
|
2013-06-18 15:52:48 +00:00
|
|
|
self.emit('action', self.items[self.selected], self.selected);
|
|
|
|
self.emit('select', self.items[self.selected], self.selected);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (key.name === 'escape' || (options.vi && key.name === 'q')) {
|
|
|
|
self.emit('action');
|
|
|
|
self.emit('cancel');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'u' && key.ctrl) {
|
|
|
|
self.move(-((self.height - (self.border ? 2 : 0)) / 2) | 0);
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'd' && key.ctrl) {
|
|
|
|
self.move((self.height - (self.border ? 2 : 0)) / 2 | 0);
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'b' && key.ctrl) {
|
|
|
|
self.move(-(self.height - (self.border ? 2 : 0)));
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'f' && key.ctrl) {
|
|
|
|
self.move(self.height - (self.border ? 2 : 0));
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'h' && key.shift) {
|
|
|
|
self.move(self.childBase - self.selected);
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'm' && key.shift) {
|
|
|
|
// TODO: Maybe use Math.min(this.items.length, ... for calculating visible items elsewhere.
|
|
|
|
self.move(self.childBase
|
|
|
|
+ (Math.min(self.height - (self.border ? 2 : 0), this.items.length) / 2 | 0)
|
|
|
|
- self.selected);
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'l' && key.shift) {
|
2013-07-03 23:46:06 +00:00
|
|
|
// XXX This goes one too far on lists with an odd number of items.
|
2013-06-18 15:52:48 +00:00
|
|
|
self.down(self.childBase
|
|
|
|
+ Math.min(self.height - (self.border ? 2 : 0), this.items.length)
|
|
|
|
- self.selected);
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
2013-07-12 12:48:55 +00:00
|
|
|
if (options.vi && key.name === 'g' && !key.shift) {
|
|
|
|
self.select(0);
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'g' && key.shift) {
|
|
|
|
self.select(self.items.length - 1);
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
2013-06-18 15:52:48 +00:00
|
|
|
});
|
|
|
|
}
|
2013-06-14 08:28:59 +00:00
|
|
|
|
2013-07-12 06:04:57 +00:00
|
|
|
this.on('resize', function() {
|
2013-06-14 08:28:59 +00:00
|
|
|
var visible = self.height - (self.border ? 2 : 0);
|
|
|
|
if (visible >= self.selected + 1) {
|
|
|
|
//if (self.selected < visible - 1) {
|
|
|
|
self.childBase = 0;
|
|
|
|
self.childOffset = self.selected;
|
|
|
|
} else {
|
2013-06-18 10:08:31 +00:00
|
|
|
// Is this supposed to be: self.childBase = visible - self.selected + 1; ?
|
2013-06-14 08:28:59 +00:00
|
|
|
self.childBase = self.selected - visible + 1;
|
|
|
|
self.childOffset = visible - 1;
|
|
|
|
}
|
2013-07-12 06:04:57 +00:00
|
|
|
});
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
List.prototype.__proto__ = ScrollableBox.prototype;
|
|
|
|
|
2013-06-25 11:34:10 +00:00
|
|
|
List.prototype.type = 'list';
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
List.prototype.add = function(item) {
|
2013-06-06 09:03:25 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-07-16 05:52:13 +00:00
|
|
|
var options = {
|
2013-06-01 02:09:07 +00:00
|
|
|
screen: this.screen,
|
2013-06-29 17:07:51 +00:00
|
|
|
content: item,
|
2013-06-13 09:14:14 +00:00
|
|
|
align: this.align || 'left',
|
2013-07-14 13:48:17 +00:00
|
|
|
top: this.items.length + (this.border ? 1 : 0) + this.padding,
|
|
|
|
left: (this.border ? 1 : 0) + this.padding + 1,
|
|
|
|
right: (this.border ? 1 : 0) + this.padding + 1,
|
2013-07-12 08:34:32 +00:00
|
|
|
tags: this.parseTags,
|
2013-06-29 17:07:51 +00:00
|
|
|
height: 1,
|
2013-07-16 05:52:13 +00:00
|
|
|
hoverBg: this.mouse ? this.options.itemHoverBg : null,
|
|
|
|
hoverEffects: this.mouse ? this.options.itemHoverEffects : null
|
|
|
|
};
|
|
|
|
|
|
|
|
['bg', 'fg', 'bold', 'underline',
|
|
|
|
'blink', 'inverse', 'invisible'].forEach(function(name) {
|
|
|
|
options[name] = function() {
|
2013-07-16 09:23:11 +00:00
|
|
|
var attr = self.items[self.selected] === item
|
2013-07-16 22:50:02 +00:00
|
|
|
? self.style.selected[name]
|
|
|
|
: self.style.item[name];
|
2013-07-16 09:23:11 +00:00
|
|
|
if (typeof attr === 'function') attr = attr(item);
|
|
|
|
return attr;
|
2013-07-16 05:52:13 +00:00
|
|
|
};
|
2013-06-01 10:47:18 +00:00
|
|
|
});
|
2013-06-06 09:03:25 +00:00
|
|
|
|
2013-07-16 05:52:13 +00:00
|
|
|
var item = new Box(options);
|
|
|
|
|
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) {
|
2013-07-12 03:52:46 +00:00
|
|
|
if (self.items[self.selected] === item) {
|
2013-07-12 10:39:22 +00:00
|
|
|
self.emit('action', item, self.selected);
|
2013-07-12 03:52:46 +00:00
|
|
|
self.emit('select', item, self.selected);
|
|
|
|
return;
|
|
|
|
}
|
2013-06-06 15:27:19 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2013-06-18 15:52:48 +00:00
|
|
|
List.prototype.setItems = function(items) {
|
|
|
|
var i = 0
|
2013-06-25 11:34:10 +00:00
|
|
|
, original = this.items.slice()
|
|
|
|
//, selected = this.selected
|
|
|
|
, sel = this.ritems[this.selected];
|
|
|
|
|
|
|
|
this.ritems = items;
|
2013-06-18 15:52:48 +00:00
|
|
|
|
|
|
|
this.select(0);
|
|
|
|
|
|
|
|
for (; i < items.length; i++) {
|
|
|
|
if (this.items[i]) {
|
|
|
|
this.items[i].setContent(items[i]);
|
|
|
|
} else {
|
|
|
|
this.add(items[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; i < original.length; i++) {
|
|
|
|
this.remove(original[i]);
|
|
|
|
}
|
2013-06-25 11:34:10 +00:00
|
|
|
|
|
|
|
// Try to find our old item if it still exists.
|
|
|
|
sel = items.indexOf(sel);
|
|
|
|
if (~sel) this.select(sel);
|
|
|
|
//this.select(~sel ? sel : selected);
|
2013-06-18 15:52:48 +00:00
|
|
|
};
|
|
|
|
|
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-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-14 20:58:54 +00:00
|
|
|
if (!(this instanceof ScrollableText)) {
|
|
|
|
return new ScrollableText(options);
|
|
|
|
}
|
|
|
|
|
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-18 15:52:48 +00:00
|
|
|
if (options.keys) {
|
|
|
|
this.on('keypress', function(ch, key) {
|
|
|
|
if (key.name === 'up' || (options.vi && key.name === 'k')) {
|
|
|
|
self.scroll(-1);
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (key.name === 'down' || (options.vi && key.name === 'j')) {
|
|
|
|
self.scroll(1);
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'u' && key.ctrl) {
|
|
|
|
self.scroll(-(self.height / 2 | 0) || -1);
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'd' && key.ctrl) {
|
|
|
|
self.scroll(self.height / 2 | 0 || 1);
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'b' && key.ctrl) {
|
|
|
|
self.scroll(-self.height || -1);
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'f' && key.ctrl) {
|
|
|
|
self.scroll(self.height || 1);
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
2013-07-12 12:48:55 +00:00
|
|
|
if (options.vi && key.name === 'g' && !key.shift) {
|
|
|
|
self.scroll(-self._clines.length);
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'g' && key.shift) {
|
|
|
|
self.scroll(self._clines.length);
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
2013-06-18 15:52:48 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-06-20 12:18:04 +00:00
|
|
|
this.on('parsed content', function() {
|
2013-06-11 19:30:36 +00:00
|
|
|
self._recalculateIndex();
|
2013-06-11 14:09:57 +00:00
|
|
|
});
|
2013-07-14 16:04:48 +00:00
|
|
|
|
|
|
|
self._recalculateIndex();
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ScrollableText.prototype.__proto__ = ScrollableBox.prototype;
|
|
|
|
|
2013-06-25 11:34:10 +00:00
|
|
|
ScrollableText.prototype.type = 'scrollable-text';
|
|
|
|
|
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-07 10:58:00 +00:00
|
|
|
if (this.content != null) {
|
2013-06-20 12:18:04 +00:00
|
|
|
this.parseContent();
|
2013-06-07 10:58:00 +00:00
|
|
|
|
2013-07-15 04:37:36 +00:00
|
|
|
max = this._clines.length - (this.height - (this.border ? 2 : 0) - this.padding * 2);
|
2013-06-18 10:08:31 +00:00
|
|
|
if (max < 0) max = 0;
|
|
|
|
|
|
|
|
if (cb > max) {
|
|
|
|
this.childBase = cb = max;
|
|
|
|
diff = cb - base;
|
|
|
|
}
|
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-11 20:01:52 +00:00
|
|
|
ScrollableText.prototype._recalculateIndex = function() {
|
2013-06-20 11:43:56 +00:00
|
|
|
if (this.detached) return;
|
2013-06-10 02:32:24 +00:00
|
|
|
|
2013-07-15 04:37:36 +00:00
|
|
|
var max = this._clines.length - (this.height - (this.border ? 2 : 0) - this.padding * 2);
|
2013-06-18 10:44:36 +00:00
|
|
|
if (max < 0) max = 0;
|
|
|
|
|
|
|
|
if (this.childBase > max) {
|
|
|
|
this.childBase = max;
|
2013-06-18 10:08:31 +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
|
|
|
}
|
2013-07-12 01:48:30 +00:00
|
|
|
|
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) {
|
2013-06-14 20:58:54 +00:00
|
|
|
if (!(this instanceof Input)) {
|
|
|
|
return new Input(options);
|
|
|
|
}
|
2013-06-01 02:09:07 +00:00
|
|
|
Box.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Input.prototype.__proto__ = Box.prototype;
|
|
|
|
|
2013-06-25 11:34:10 +00:00
|
|
|
Input.prototype.type = 'input';
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Textbox
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Textbox(options) {
|
2013-06-14 20:58:54 +00:00
|
|
|
if (!(this instanceof Textbox)) {
|
|
|
|
return new Textbox(options);
|
|
|
|
}
|
2013-06-25 22:31:13 +00:00
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
Input.call(this, options);
|
2013-06-25 22:31:13 +00:00
|
|
|
|
2013-06-09 20:02:12 +00:00
|
|
|
this.screen._listenKeys(this);
|
2013-06-25 22:31:13 +00:00
|
|
|
|
2013-06-20 12:32:50 +00:00
|
|
|
this.value = options.value || '';
|
2013-06-20 16:48:12 +00:00
|
|
|
this.secret = options.secret;
|
2013-06-25 11:34:10 +00:00
|
|
|
this.censor = options.censor;
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.on('resize', updateCursor);
|
|
|
|
this.on('move', updateCursor);
|
|
|
|
|
|
|
|
function updateCursor() {
|
|
|
|
if (self.screen.focused !== self) return;
|
|
|
|
self.screen.program.cup(
|
2013-07-12 05:36:36 +00:00
|
|
|
self.top + (self.border ? 1 : 0),
|
|
|
|
self.left + (self.border ? 1 : 0)
|
2013-06-25 11:34:10 +00:00
|
|
|
+ self.value.length);
|
|
|
|
}
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Textbox.prototype.__proto__ = Input.prototype;
|
|
|
|
|
2013-06-25 11:34:10 +00:00
|
|
|
Textbox.prototype.type = 'textbox';
|
|
|
|
|
2013-07-03 23:46:06 +00:00
|
|
|
Textbox.prototype.input =
|
|
|
|
Textbox.prototype.readInput =
|
2013-06-09 18:14:42 +00:00
|
|
|
Textbox.prototype.setInput = function(callback) {
|
2013-07-12 01:48:30 +00:00
|
|
|
var self = this
|
|
|
|
, focused = this.screen.focused === this;
|
2013-06-09 18:14:42 +00:00
|
|
|
|
2013-07-12 01:48:30 +00:00
|
|
|
if (!focused) {
|
|
|
|
this.screen.saveFocus();
|
|
|
|
this.focus();
|
|
|
|
}
|
2013-06-18 15:52:48 +00:00
|
|
|
|
2013-06-09 20:02:12 +00:00
|
|
|
this.screen.grabKeys = true;
|
2013-06-09 18:14:42 +00:00
|
|
|
|
2013-07-12 01:48:30 +00:00
|
|
|
// Could possibly save and restore cursor.
|
|
|
|
|
2013-06-09 19:34:13 +00:00
|
|
|
this.screen.program.cup(
|
2013-07-14 13:48:17 +00:00
|
|
|
this.top + (this.border ? 1 : 0) + this.padding,
|
|
|
|
this.left + (this.border ? 1 : 0) + this.padding
|
2013-06-20 12:32:50 +00:00
|
|
|
+ this.value.length);
|
2013-06-09 18:14:42 +00:00
|
|
|
this.screen.program.showCursor();
|
|
|
|
this.screen.program.sgr('normal');
|
|
|
|
|
|
|
|
this._callback = function(err, value) {
|
|
|
|
self.screen.program.hideCursor();
|
2013-07-10 17:48:18 +00:00
|
|
|
self.screen.grabKeys = false;
|
2013-06-18 15:52:48 +00:00
|
|
|
|
2013-07-12 01:48:30 +00:00
|
|
|
if (!focused) {
|
|
|
|
self.screen.restoreFocus();
|
|
|
|
}
|
2013-06-18 15:52:48 +00:00
|
|
|
|
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
|
2013-06-20 12:32:50 +00:00
|
|
|
, value = this.value;
|
2013-06-09 18:14:42 +00:00
|
|
|
|
|
|
|
if (key.name === 'escape' || key.name === 'enter') {
|
|
|
|
delete this._callback;
|
2013-06-20 12:32:50 +00:00
|
|
|
this.value = '';
|
2013-06-09 18:14:42 +00:00
|
|
|
this.removeListener('keypress', this.__listener);
|
|
|
|
delete this.__listener;
|
|
|
|
callback(null, key.name === 'enter' ? value : null);
|
|
|
|
} else if (key.name === 'backspace') {
|
2013-06-20 12:32:50 +00:00
|
|
|
if (this.value.length) {
|
|
|
|
this.value = this.value.slice(0, -1);
|
2013-06-20 16:48:12 +00:00
|
|
|
if (this.secret) return;
|
2013-07-14 13:48:17 +00:00
|
|
|
if (this.value.length < this.width - (this.border ? 2 : 0) - this.padding * 2 - 1) {
|
2013-06-09 19:32:29 +00:00
|
|
|
this.screen.program.cub();
|
|
|
|
}
|
2013-06-09 18:14:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ch) {
|
2013-07-12 01:48:30 +00:00
|
|
|
// Tabs only work with textareas.
|
|
|
|
if (ch === '\t') ch = ' ';
|
2013-06-20 12:32:50 +00:00
|
|
|
this.value += ch;
|
2013-06-20 16:48:12 +00:00
|
|
|
if (this.secret) return;
|
2013-07-14 13:48:17 +00:00
|
|
|
if (this.value.length < this.width - (this.border ? 2 : 0) - this.padding * 2) {
|
2013-06-09 19:32:29 +00:00
|
|
|
this.screen.program.cuf();
|
|
|
|
}
|
2013-06-09 18:14:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 12:32:50 +00:00
|
|
|
// Maybe just use this instead of render hook:
|
|
|
|
// Problem - user can't set .value willy nilly.
|
|
|
|
// if (this.value !== value) {
|
2013-07-14 13:48:17 +00:00
|
|
|
// this.setContent(this.value.slice(-(this.width - (this.border ? 2 : 0) - this.padding * 2 - 1)));
|
2013-06-20 12:32:50 +00:00
|
|
|
// }
|
|
|
|
|
2013-06-09 18:14:42 +00:00
|
|
|
this.screen.render();
|
|
|
|
};
|
|
|
|
|
2013-07-04 06:50:50 +00:00
|
|
|
Textbox.prototype.clearInput = function() {
|
|
|
|
this.value = '';
|
|
|
|
this.setContent('');
|
|
|
|
};
|
|
|
|
|
2013-06-20 16:48:12 +00:00
|
|
|
Textbox.prototype.submit = function() {
|
|
|
|
return this._listener(null, { name: 'enter' });
|
|
|
|
};
|
|
|
|
|
|
|
|
Textbox.prototype.cancel = function() {
|
|
|
|
return this._listener('\x1b', { name: 'escape' });
|
|
|
|
};
|
|
|
|
|
2013-06-09 19:32:29 +00:00
|
|
|
Textbox.prototype._render = Input.prototype.render;
|
|
|
|
Textbox.prototype.render = function(stop) {
|
2013-06-20 12:32:50 +00:00
|
|
|
// setContent is necessary to clear the area in case
|
|
|
|
// .shrink is being used and the new content is smaller.
|
|
|
|
// Could technically optimize this.
|
2013-06-20 16:48:12 +00:00
|
|
|
if (this.secret) {
|
|
|
|
this.setContent('');
|
2013-06-25 11:34:10 +00:00
|
|
|
return this._render(stop);
|
|
|
|
}
|
|
|
|
if (this.censor) {
|
|
|
|
this.setContent(Array(this.value.length + 1).join('*'));
|
2013-06-20 16:48:12 +00:00
|
|
|
return this._render(stop);
|
|
|
|
}
|
2013-07-14 13:48:17 +00:00
|
|
|
this.setContent(this.value.slice(-(this.width - (this.border ? 2 : 0) - this.padding * 2 - 1)));
|
2013-06-20 12:32:50 +00:00
|
|
|
return this._render(stop);
|
2013-06-09 19:32:29 +00:00
|
|
|
};
|
|
|
|
|
2013-07-03 23:46:06 +00:00
|
|
|
Textbox.prototype.editor =
|
|
|
|
Textbox.prototype.readEditor =
|
2013-06-09 18:14:42 +00:00
|
|
|
Textbox.prototype.setEditor = function(callback) {
|
|
|
|
var self = this;
|
2013-07-04 05:10:01 +00:00
|
|
|
return this.screen.readEditor({ value: this.value }, function(err, value) {
|
2013-06-09 18:14:42 +00:00
|
|
|
if (err) return callback(err);
|
2013-06-20 12:32:50 +00:00
|
|
|
value = value.replace(/[\r\n]/g, '');
|
|
|
|
self.value = value;
|
2013-07-12 01:48:30 +00:00
|
|
|
return self.readInput(callback);
|
2013-06-09 18:14:42 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
/**
|
|
|
|
* Textarea
|
|
|
|
*/
|
2013-07-04 04:15:09 +00:00
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
function Textarea(options) {
|
|
|
|
if (!(this instanceof Textarea)) {
|
|
|
|
return new Textarea(options);
|
|
|
|
}
|
2013-07-04 04:15:09 +00:00
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
ScrollableText.call(this, options);
|
2013-07-04 04:15:09 +00:00
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
this.screen._listenKeys(this);
|
2013-07-04 04:15:09 +00:00
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
this.value = options.value || '';
|
2013-07-04 04:15:09 +00:00
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
this.__updateCursor = this.updateCursor.bind(this);
|
|
|
|
this.on('resize', this.__updateCursor);
|
|
|
|
this.on('move', this.__updateCursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
Textarea.prototype.__proto__ = ScrollableText.prototype;
|
|
|
|
|
|
|
|
Textarea.prototype.type = 'textarea';
|
|
|
|
|
|
|
|
Textarea.prototype.updateCursor = function() {
|
2013-07-12 04:00:03 +00:00
|
|
|
if (this.screen.focused !== this) {
|
|
|
|
return;
|
|
|
|
}
|
2013-07-04 05:10:01 +00:00
|
|
|
|
2013-07-12 04:00:03 +00:00
|
|
|
var last = this._clines[this._clines.length-1]
|
|
|
|
, program = this.screen.program
|
|
|
|
, line
|
|
|
|
, cx
|
|
|
|
, cy;
|
2013-07-06 07:54:04 +00:00
|
|
|
|
|
|
|
// Stop a situation where the textarea begins scrolling
|
|
|
|
// and the last cline appears to always be empty from the
|
|
|
|
// _typeScroll `+ '\n'` thing.
|
2013-07-16 06:54:29 +00:00
|
|
|
// Maybe not necessary anymore?
|
2013-07-06 07:54:04 +00:00
|
|
|
if (last === '' && this.value[this.value.length-1] !== '\n') {
|
|
|
|
last = this._clines[this._clines.length-2] || '';
|
|
|
|
}
|
2013-07-04 05:10:01 +00:00
|
|
|
|
2013-07-12 04:00:03 +00:00
|
|
|
line = Math.min(
|
2013-07-06 07:54:04 +00:00
|
|
|
this._clines.length - 1 - this.childBase,
|
2013-07-14 13:48:17 +00:00
|
|
|
this.height - (this.border ? 2 : 0) - this.padding * 2 - 1);
|
2013-07-04 05:10:01 +00:00
|
|
|
|
2013-07-14 13:48:17 +00:00
|
|
|
cy = this.top + (this.border ? 1 : 0) + this.padding + line;
|
|
|
|
cx = this.left + (this.border ? 1 : 0) + this.padding + last.length;
|
2013-07-12 04:00:03 +00:00
|
|
|
|
2013-07-12 06:04:57 +00:00
|
|
|
if (cy === program.y && cx === program.x) {
|
2013-07-12 04:00:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-12 06:04:57 +00:00
|
|
|
if (cy === program.y) {
|
|
|
|
if (cx > program.x) {
|
|
|
|
program.cuf(cx - program.x);
|
|
|
|
} else if (cx < program.x) {
|
|
|
|
program.cub(program.x - cx);
|
2013-07-12 04:00:03 +00:00
|
|
|
}
|
2013-07-12 06:04:57 +00:00
|
|
|
} else if (cx === program.x) {
|
|
|
|
if (cy > program.y) {
|
|
|
|
program.cud(cy - program.y);
|
|
|
|
} else if (cy < program.y) {
|
|
|
|
program.cuu(program.y - cy);
|
2013-07-12 04:00:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
program.cup(cy, cx);
|
|
|
|
}
|
2013-07-04 04:15:09 +00:00
|
|
|
};
|
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
Textarea.prototype.input =
|
|
|
|
Textarea.prototype.readInput =
|
|
|
|
Textarea.prototype.setInput = function(callback) {
|
2013-07-12 01:48:30 +00:00
|
|
|
var self = this
|
|
|
|
, focused = this.screen.focused === this;
|
2013-07-04 04:15:09 +00:00
|
|
|
|
2013-07-12 01:48:30 +00:00
|
|
|
if (!focused) {
|
|
|
|
this.screen.saveFocus();
|
|
|
|
this.focus();
|
|
|
|
}
|
2013-07-04 04:15:09 +00:00
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
this.screen.grabKeys = true;
|
2013-07-04 04:15:09 +00:00
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
this.updateCursor();
|
|
|
|
this.screen.program.showCursor();
|
|
|
|
this.screen.program.sgr('normal');
|
2013-07-04 04:15:09 +00:00
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
this._callback = function(err, value) {
|
|
|
|
self.screen.program.hideCursor();
|
2013-07-10 17:48:18 +00:00
|
|
|
self.screen.grabKeys = false;
|
2013-07-12 01:48:30 +00:00
|
|
|
|
|
|
|
if (!focused) {
|
|
|
|
self.screen.restoreFocus();
|
|
|
|
}
|
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
return err
|
|
|
|
? callback(err)
|
|
|
|
: callback(null, value);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.__listener = this._listener.bind(this);
|
|
|
|
this.on('keypress', this.__listener);
|
2013-07-04 04:15:09 +00:00
|
|
|
};
|
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
Textarea.prototype._listener = function(ch, key) {
|
|
|
|
var callback = this._callback
|
|
|
|
, value = this.value;
|
2013-06-09 18:14:42 +00:00
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
if (key.name === 'enter') {
|
|
|
|
ch = '\n';
|
2013-06-14 20:58:54 +00:00
|
|
|
}
|
2013-06-09 18:14:42 +00:00
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
// TODO: Handle directional keys.
|
|
|
|
if (key.name === 'left' || key.name === 'right'
|
|
|
|
|| key.name === 'up' || key.name === 'down') {
|
|
|
|
;
|
|
|
|
}
|
2013-06-09 18:14:42 +00:00
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
if (key.name === 'escape') {
|
|
|
|
delete this._callback;
|
|
|
|
this.removeListener('keypress', this.__listener);
|
|
|
|
delete this.__listener;
|
|
|
|
callback(null, key.name === 'enter' ? value : null);
|
|
|
|
} else if (key.name === 'backspace') {
|
|
|
|
if (this.value.length) {
|
|
|
|
this.value = this.value.slice(0, -1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ch) {
|
|
|
|
this.value += ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.value !== value) {
|
|
|
|
this.setContent(this.value);
|
2013-07-04 06:50:50 +00:00
|
|
|
this._typeScroll();
|
2013-07-04 05:10:01 +00:00
|
|
|
this.updateCursor();
|
|
|
|
this.screen.render();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-04 06:50:50 +00:00
|
|
|
Textarea.prototype._typeScroll = function() {
|
|
|
|
// XXX Workaround
|
2013-07-14 13:48:17 +00:00
|
|
|
if (this._clines.length - this.childBase > this.height - (this.border ? 2 : 0) - this.padding * 2) {
|
2013-07-16 06:54:29 +00:00
|
|
|
//this.setContent(this.value + '\n');
|
2013-07-04 06:50:50 +00:00
|
|
|
this.scroll(this._clines.length);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Textarea.prototype.clearInput = function() {
|
|
|
|
this.value = '';
|
|
|
|
this.setContent('');
|
|
|
|
};
|
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
Textarea.prototype.submit = function() {
|
|
|
|
return this._listener('\x1b', { name: 'escape' });
|
|
|
|
};
|
|
|
|
|
|
|
|
Textarea.prototype.cancel = function() {
|
|
|
|
return this._listener('\x1b', { name: 'escape' });
|
|
|
|
};
|
|
|
|
|
|
|
|
Textarea.prototype.editor =
|
|
|
|
Textarea.prototype.readEditor =
|
|
|
|
Textarea.prototype.setEditor = function(callback) {
|
|
|
|
var self = this;
|
|
|
|
return this.screen.readEditor({ value: this.value }, function(err, value) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
self.value = value;
|
|
|
|
self.setContent(self.value);
|
2013-07-04 06:50:50 +00:00
|
|
|
self._typeScroll();
|
2013-07-04 06:51:16 +00:00
|
|
|
self.updateCursor();
|
2013-07-04 05:10:01 +00:00
|
|
|
self.screen.render();
|
2013-07-12 01:48:30 +00:00
|
|
|
return self.readInput(callback);
|
2013-07-04 05:10:01 +00:00
|
|
|
});
|
|
|
|
};
|
2013-06-25 11:34:10 +00:00
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Button
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Button(options) {
|
2013-06-16 09:21:57 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-06-14 20:58:54 +00:00
|
|
|
if (!(this instanceof Button)) {
|
|
|
|
return new Button(options);
|
|
|
|
}
|
2013-06-16 09:21:57 +00:00
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
Input.call(this, options);
|
2013-06-16 09:21:57 +00:00
|
|
|
|
|
|
|
this.on('keypress', function(ch, key) {
|
|
|
|
if (key.name === 'enter' || key.name === 'space') {
|
|
|
|
self.press();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-07-12 09:59:58 +00:00
|
|
|
if (this.options.defaultEffects) {
|
2013-06-29 17:07:51 +00:00
|
|
|
this.on('mouseover', function() {
|
|
|
|
self.inverse = !self.options.inverse;
|
|
|
|
self.screen.render();
|
|
|
|
});
|
2013-06-16 09:21:57 +00:00
|
|
|
|
2013-06-29 17:07:51 +00:00
|
|
|
this.on('mouseout', function() {
|
|
|
|
self.inverse = self.options.inverse;
|
|
|
|
self.screen.render();
|
|
|
|
});
|
|
|
|
}
|
2013-06-29 22:46:47 +00:00
|
|
|
|
|
|
|
if (this.options.mouse) {
|
|
|
|
this.on('click', function() {
|
|
|
|
self.press();
|
|
|
|
});
|
|
|
|
}
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Button.prototype.__proto__ = Input.prototype;
|
|
|
|
|
2013-06-25 11:34:10 +00:00
|
|
|
Button.prototype.type = 'button';
|
|
|
|
|
2013-06-16 09:21:57 +00:00
|
|
|
Button.prototype.press = function() {
|
|
|
|
var self = this;
|
|
|
|
this.emit('press');
|
2013-07-12 09:59:58 +00:00
|
|
|
if (this.border && this.options.defaultEffects) {
|
2013-06-29 17:07:51 +00:00
|
|
|
var color = this.border.fg;
|
2013-07-14 13:05:42 +00:00
|
|
|
this.border.fg = 'green';
|
2013-06-16 09:21:57 +00:00
|
|
|
this.screen.render();
|
|
|
|
setTimeout(function() {
|
2013-06-29 17:07:51 +00:00
|
|
|
self.border.fg = color;
|
2013-06-16 09:21:57 +00:00
|
|
|
self.screen.render();
|
|
|
|
}, 300);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-01 07:06:04 +00:00
|
|
|
/**
|
|
|
|
* ProgressBar
|
|
|
|
*/
|
|
|
|
|
|
|
|
function ProgressBar(options) {
|
2013-06-14 20:58:54 +00:00
|
|
|
if (!(this instanceof ProgressBar)) {
|
|
|
|
return new ProgressBar(options);
|
|
|
|
}
|
2013-06-01 07:06:04 +00:00
|
|
|
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-07-16 22:50:02 +00:00
|
|
|
if (!this.style.bar) {
|
|
|
|
this.style.bar = {};
|
|
|
|
this.style.bar.fg = options.barFg;
|
|
|
|
this.style.bar.bg = 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;
|
|
|
|
|
2013-06-25 11:34:10 +00:00
|
|
|
ProgressBar.prototype.type = 'progress-bar';
|
|
|
|
|
2013-06-01 07:06:04 +00:00
|
|
|
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-07-17 00:23:39 +00:00
|
|
|
dattr = this.sattr(this, this.style.bar.fg, this.style.bar.bg);
|
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-07-12 12:48:55 +00:00
|
|
|
/**
|
|
|
|
* FileManager
|
|
|
|
*/
|
|
|
|
|
|
|
|
function FileManager(options) {
|
|
|
|
if (!(this instanceof FileManager)) {
|
|
|
|
return new FileManager(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
options.parseTags = true;
|
|
|
|
|
|
|
|
List.call(this, options);
|
|
|
|
|
|
|
|
this.cwd = options.cwd || process.cwd();
|
|
|
|
|
|
|
|
this.on('select', function(item) {
|
|
|
|
var value = item.content.replace(/\{[^{}]+\}/g, '').replace(/@$/, '')
|
|
|
|
, file = path.resolve(self.cwd, value);
|
|
|
|
|
|
|
|
return fs.stat(file, function(err, stat) {
|
|
|
|
if (err) {
|
|
|
|
return self.emit('error', err, file);
|
|
|
|
}
|
|
|
|
if (stat.isDirectory()) {
|
|
|
|
self.emit('cd', file, self.cwd);
|
|
|
|
self.cwd = file;
|
|
|
|
self.refresh();
|
|
|
|
} else {
|
|
|
|
self.emit('file', file);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
FileManager.prototype.__proto__ = List.prototype;
|
|
|
|
|
|
|
|
FileManager.prototype.type = 'file-manager';
|
|
|
|
|
|
|
|
FileManager.prototype.refresh = function(cwd, callback) {
|
|
|
|
if (!callback) {
|
|
|
|
callback = cwd;
|
|
|
|
cwd = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this
|
|
|
|
, cwd = cwd || this.cwd;
|
|
|
|
|
|
|
|
return fs.readdir(cwd, function(err, list) {
|
|
|
|
if (err && err.code === 'ENOENT') {
|
|
|
|
self.cwd = cwd !== process.env.HOME
|
|
|
|
? process.env.HOME
|
|
|
|
: '/';
|
|
|
|
return self.refresh(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
if (callback) return callback(err);
|
|
|
|
return self.emit('error', err, cwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
var dirs = []
|
|
|
|
, files = [];
|
|
|
|
|
|
|
|
list.unshift('..');
|
|
|
|
|
|
|
|
list.forEach(function(name) {
|
|
|
|
var f = path.resolve(cwd, name)
|
|
|
|
, stat;
|
|
|
|
|
|
|
|
try {
|
|
|
|
stat = fs.lstatSync(f);
|
|
|
|
} catch (e) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((stat && stat.isDirectory()) || name === '..') {
|
|
|
|
dirs.push({
|
|
|
|
name: name,
|
|
|
|
text: '{light-blue-fg}' + name + '{/light-blue-fg}/',
|
|
|
|
dir: true
|
|
|
|
});
|
|
|
|
} else if (stat && stat.isSymbolicLink()) {
|
|
|
|
files.push({
|
|
|
|
name: name,
|
|
|
|
text: '{light-cyan-fg}' + name + '{/light-cyan-fg}@',
|
|
|
|
dir: false
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
files.push({
|
|
|
|
name: name,
|
|
|
|
text: name,
|
|
|
|
dir: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
dirs = asort(dirs);
|
|
|
|
files = asort(files);
|
|
|
|
|
|
|
|
list = dirs.concat(files).map(function(data) {
|
|
|
|
return data.text;
|
|
|
|
});
|
|
|
|
|
|
|
|
self.setItems(list);
|
|
|
|
self.select(0);
|
|
|
|
self.screen.render();
|
|
|
|
|
|
|
|
if (callback) callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
FileManager.prototype.pick = function(cwd, callback) {
|
|
|
|
if (!callback) {
|
|
|
|
callback = cwd;
|
|
|
|
cwd = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this
|
|
|
|
, focused = this.screen.focused === this
|
2013-07-12 16:30:20 +00:00
|
|
|
, hidden = this.hidden
|
|
|
|
, onfile
|
|
|
|
, oncancel;
|
2013-07-12 12:48:55 +00:00
|
|
|
|
|
|
|
function resume() {
|
|
|
|
self.removeListener('file', onfile);
|
|
|
|
self.removeListener('cancel', oncancel);
|
|
|
|
if (hidden) {
|
|
|
|
self.hide();
|
|
|
|
}
|
|
|
|
if (!focused) {
|
|
|
|
self.screen.restoreFocus();
|
|
|
|
}
|
|
|
|
self.screen.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.on('file', onfile = function(file) {
|
|
|
|
resume();
|
|
|
|
return callback(null, file);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on('cancel', oncancel = function() {
|
|
|
|
resume();
|
|
|
|
return callback();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.refresh(cwd, function(err) {
|
|
|
|
if (err) return callback(err);
|
2013-07-12 16:30:20 +00:00
|
|
|
|
|
|
|
if (hidden) {
|
|
|
|
self.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!focused) {
|
|
|
|
self.screen.saveFocus();
|
|
|
|
self.focus();
|
|
|
|
}
|
|
|
|
|
2013-07-12 12:48:55 +00:00
|
|
|
self.screen.render();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
FileManager.prototype.reset = function(cwd, callback) {
|
|
|
|
if (!callback) {
|
|
|
|
callback = cwd;
|
|
|
|
cwd = null;
|
|
|
|
}
|
|
|
|
this.cwd = cwd || this.options.cwd;
|
|
|
|
this.refresh(callback);
|
|
|
|
};
|
|
|
|
|
2013-07-14 01:44:30 +00:00
|
|
|
/**
|
|
|
|
* Checkbox
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Checkbox(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!(this instanceof Checkbox)) {
|
|
|
|
return new Checkbox(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Input.call(this, options);
|
|
|
|
|
|
|
|
this.value = options.value || '';
|
|
|
|
this.checked = options.checked || false;
|
|
|
|
|
|
|
|
this.on('keypress', function(ch, key) {
|
|
|
|
if (key.name === 'enter' || key.name === 'space') {
|
|
|
|
self.check();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.options.mouse) {
|
|
|
|
this.on('click', function() {
|
|
|
|
self.check();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.on('focus', function() {
|
|
|
|
self.program.saveCursor();
|
|
|
|
self.program.cup(this.top, this.left + 1);
|
|
|
|
self.program.showCursor();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on('blur', function() {
|
|
|
|
self.program.hideCursor();
|
|
|
|
self.program.restoreCursor();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Checkbox.prototype.__proto__ = Input.prototype;
|
|
|
|
|
|
|
|
Checkbox.prototype.type = 'checkbox';
|
|
|
|
|
|
|
|
Checkbox.prototype._render = Checkbox.prototype.render;
|
|
|
|
Checkbox.prototype.render = function(stop) {
|
|
|
|
this.setContent('[' + (this.checked ? 'x' : ' ') + '] ' + this.value);
|
|
|
|
return this._render(stop);
|
|
|
|
};
|
|
|
|
|
|
|
|
Checkbox.prototype.check = function() {
|
|
|
|
if (this.checked) return;
|
|
|
|
this.checked = true;
|
|
|
|
this.emit('check');
|
|
|
|
};
|
|
|
|
|
|
|
|
Checkbox.prototype.uncheck = function() {
|
|
|
|
if (!this.checked) return;
|
|
|
|
this.checked = false;
|
|
|
|
this.emit('uncheck');
|
|
|
|
};
|
|
|
|
|
|
|
|
Checkbox.prototype.toggle = function() {
|
|
|
|
return this.checked
|
|
|
|
? this.uncheck()
|
|
|
|
: this.check();
|
|
|
|
};
|
|
|
|
|
|
|
|
Checkbox.prototype.setChecked = function(val) {
|
|
|
|
val = !!val;
|
|
|
|
if (this.checked === val) return;
|
|
|
|
this.checked = val;
|
|
|
|
this.emit('check', val);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RadioButton
|
|
|
|
*/
|
|
|
|
|
|
|
|
function RadioButton(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!(this instanceof RadioButton)) {
|
|
|
|
return new RadioButton(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Checkbox.call(this, options);
|
|
|
|
|
|
|
|
self.group = options.group || [];
|
|
|
|
|
|
|
|
this.on('check', function() {
|
|
|
|
self.group.forEach(function(el) {
|
2013-07-15 10:00:26 +00:00
|
|
|
if (el === self) return;
|
2013-07-14 01:44:30 +00:00
|
|
|
el.uncheck();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
RadioButton.prototype.__proto__ = Checkbox.prototype;
|
|
|
|
|
|
|
|
RadioButton.prototype.type = 'radio-button';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prompt
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Prompt(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!(this instanceof Prompt)) {
|
|
|
|
return new Prompt(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Box.call(this, options);
|
|
|
|
|
|
|
|
this._.input = new Textbox({
|
|
|
|
parent: this,
|
|
|
|
top: 3,
|
|
|
|
height: 1,
|
|
|
|
left: 2,
|
|
|
|
right: 2,
|
|
|
|
bg: 'black'
|
|
|
|
});
|
|
|
|
|
|
|
|
this._.okay = new Button({
|
|
|
|
parent: this,
|
|
|
|
top: 5,
|
|
|
|
height: 1,
|
|
|
|
left: 2,
|
|
|
|
width: 6,
|
|
|
|
content: 'Okay',
|
|
|
|
align: 'center',
|
|
|
|
bg: 'black',
|
|
|
|
hoverBg: 'blue',
|
|
|
|
autoFocus: false,
|
|
|
|
mouse: true
|
|
|
|
});
|
|
|
|
|
|
|
|
this._.cancel = new Button({
|
|
|
|
parent: this,
|
|
|
|
top: 5,
|
|
|
|
height: 1,
|
|
|
|
shrink: true,
|
|
|
|
left: 10,
|
|
|
|
width: 8,
|
|
|
|
content: 'Cancel',
|
|
|
|
align: 'center',
|
|
|
|
bg: 'black',
|
|
|
|
hoverBg: 'blue',
|
|
|
|
autoFocus: false,
|
|
|
|
mouse: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Prompt.prototype.__proto__ = Box.prototype;
|
|
|
|
|
|
|
|
Prompt.prototype.type = 'prompt';
|
|
|
|
|
|
|
|
Prompt.prototype.type = function(text, value, callback) {
|
|
|
|
var self = this;
|
|
|
|
var okay, cancel;
|
|
|
|
|
|
|
|
if (!callback) {
|
|
|
|
callback = value;
|
|
|
|
value = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.show();
|
|
|
|
this.setContent(' ' + text);
|
|
|
|
|
|
|
|
if (value) this._.input.value = value;
|
|
|
|
|
|
|
|
this.screen.saveFocus();
|
|
|
|
|
|
|
|
this._.okay.on('press', okay = function() {
|
|
|
|
self._.input.submit();
|
|
|
|
});
|
|
|
|
|
|
|
|
this._.cancel.on('press', cancel = function() {
|
|
|
|
self._.input.cancel();
|
|
|
|
});
|
|
|
|
|
|
|
|
this._.input.readInput(function(err, data) {
|
|
|
|
self.hide();
|
|
|
|
self.screen.restoreFocus();
|
|
|
|
self._.okay.removeListener('press', okay);
|
|
|
|
self._.cancel.removeListener('press', cancel);
|
|
|
|
return callback(err, data);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.screen.render();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Question
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Question(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!(this instanceof Question)) {
|
|
|
|
return new Question(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Box.call(this, options);
|
|
|
|
|
|
|
|
this._.okay = new Button({
|
|
|
|
parent: this,
|
|
|
|
top: 3,
|
|
|
|
height: 1,
|
|
|
|
left: 2,
|
|
|
|
width: 6,
|
|
|
|
content: 'Okay',
|
|
|
|
align: 'center',
|
|
|
|
bg: 'black',
|
|
|
|
hoverBg: 'blue',
|
|
|
|
autoFocus: false,
|
|
|
|
mouse: true
|
|
|
|
});
|
|
|
|
|
|
|
|
this._.cancel = new Button({
|
|
|
|
parent: this,
|
|
|
|
top: 3,
|
|
|
|
height: 1,
|
|
|
|
shrink: true,
|
|
|
|
left: 10,
|
|
|
|
width: 8,
|
|
|
|
content: 'Cancel',
|
|
|
|
align: 'center',
|
|
|
|
bg: 'black',
|
|
|
|
hoverBg: 'blue',
|
|
|
|
autoFocus: false,
|
|
|
|
mouse: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Question.prototype.__proto__ = Box.prototype;
|
|
|
|
|
|
|
|
Question.prototype.type = 'question';
|
|
|
|
|
|
|
|
Question.prototype.ask = function(text, callback) {
|
|
|
|
var self = this;
|
|
|
|
var press, okay, cancel;
|
|
|
|
|
|
|
|
this.show();
|
|
|
|
this.setContent(' ' + text);
|
|
|
|
|
|
|
|
this.screen.on('keypress', press = function(ch, key) {
|
|
|
|
if (key.name === 'mouse') return;
|
|
|
|
if (key.name !== 'enter'
|
|
|
|
&& key.name !== 'escape'
|
|
|
|
&& key.name !== 'q'
|
|
|
|
&& key.name !== 'y'
|
|
|
|
&& key.name !== 'n') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
done(null, key.name === 'enter' || key.name === 'y');
|
|
|
|
});
|
|
|
|
|
|
|
|
this._.okay.on('press', okay = function() {
|
|
|
|
done(null, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
this._.cancel.on('press', cancel = function() {
|
|
|
|
done(null, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.screen.saveFocus();
|
|
|
|
this.focus();
|
|
|
|
|
|
|
|
function done(err, data) {
|
|
|
|
self.hide();
|
|
|
|
self.screen.restoreFocus();
|
|
|
|
self.screen.removeListener('keypress', press);
|
|
|
|
self._.okay.removeListener('press', okay);
|
|
|
|
self._.cancel.removeListener('press', cancel);
|
|
|
|
return callback(err, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.screen.render();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Message / Error
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Message(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!(this instanceof Message)) {
|
|
|
|
return new Message(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
options.tags = true;
|
|
|
|
|
|
|
|
Box.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Message.prototype.__proto__ = Box.prototype;
|
|
|
|
|
|
|
|
Message.prototype.type = 'message';
|
|
|
|
|
|
|
|
Message.prototype.log =
|
|
|
|
Message.prototype.display = function(text, time, callback) {
|
|
|
|
var self = this;
|
|
|
|
if (typeof time === 'function') {
|
|
|
|
callback = time;
|
|
|
|
time = null;
|
|
|
|
}
|
|
|
|
time = time || 3;
|
|
|
|
this.show();
|
|
|
|
this.setContent(text);
|
|
|
|
this.screen.render();
|
|
|
|
setTimeout(function() {
|
|
|
|
self.hide();
|
|
|
|
self.screen.render();
|
|
|
|
if (callback) callback();
|
|
|
|
}, time * 1000);
|
|
|
|
};
|
|
|
|
|
|
|
|
Message.prototype.error = function(text, callback) {
|
2013-07-15 10:00:26 +00:00
|
|
|
return this.display('{red-fg}Error: ' + text + '{/red-fg}', callback);
|
2013-07-14 01:44:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Info - TODO: Merge display method into Message?
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Info(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!(this instanceof Info)) {
|
|
|
|
return new Info(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Box.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Info.prototype.__proto__ = Box.prototype;
|
|
|
|
|
|
|
|
Info.prototype.type = 'info';
|
|
|
|
|
|
|
|
Info.prototype.display = function(text, callback) {
|
|
|
|
this.show();
|
|
|
|
|
|
|
|
this.setContent(text);
|
|
|
|
|
|
|
|
this.screen.render();
|
|
|
|
|
|
|
|
function end() {
|
|
|
|
if (end.done) return;
|
|
|
|
end.done = true;
|
|
|
|
self.hide();
|
|
|
|
self.screen.render();
|
|
|
|
if (callback) callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
return setTimeout(function() {
|
|
|
|
self.screen.on('keypress', function fn(ch, key) {
|
|
|
|
if (key.name === 'mouse') return;
|
|
|
|
self.screen.removeListener('keypress', fn);
|
|
|
|
end();
|
|
|
|
});
|
2013-07-15 10:00:26 +00:00
|
|
|
if (!self.options.mouse) return;
|
|
|
|
self.screen.on('mouse', function fn(data) {
|
2013-07-14 01:44:30 +00:00
|
|
|
if (data.action === 'mousemove') return;
|
|
|
|
self.screen.removeListener('mouse', fn);
|
|
|
|
end();
|
|
|
|
});
|
|
|
|
}, 10);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loading
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Loading(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!(this instanceof Loading)) {
|
|
|
|
return new Loading(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Box.call(this, options);
|
|
|
|
|
|
|
|
this._.icon = new Text({
|
|
|
|
parent: this,
|
|
|
|
align: 'center',
|
|
|
|
top: 2,
|
|
|
|
left: 1,
|
|
|
|
right: 1,
|
|
|
|
height: 1,
|
|
|
|
content: '|'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Loading.prototype.__proto__ = Box.prototype;
|
|
|
|
|
|
|
|
Loading.prototype.type = 'loading';
|
|
|
|
|
|
|
|
Loading.prototype.load = function(text) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.show();
|
|
|
|
this.setContent(text);
|
|
|
|
|
|
|
|
if (this._.timer) {
|
|
|
|
this._.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.screen.lockKeys = true;
|
|
|
|
|
|
|
|
this._.timer = setInterval(function() {
|
|
|
|
if (self._.icon.content === '|') {
|
|
|
|
self._.icon.setContent('/');
|
|
|
|
} else if (self._.icon.content === '/') {
|
|
|
|
self._.icon.setContent('-');
|
|
|
|
} else if (self._.icon.content === '-') {
|
|
|
|
self._.icon.setContent('\\');
|
|
|
|
} else if (self._.icon.content === '\\') {
|
|
|
|
self._.icon.setContent('|');
|
|
|
|
}
|
|
|
|
self.screen.render();
|
|
|
|
}, 200);
|
|
|
|
};
|
|
|
|
|
|
|
|
Loading.prototype.stop = function() {
|
|
|
|
this.screen.lockKeys = false;
|
|
|
|
this.hide();
|
|
|
|
if (this._.timer) {
|
|
|
|
clearInterval(this._.timer);
|
|
|
|
delete this._.timer;
|
|
|
|
}
|
|
|
|
this.screen.render();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PickList - TODO Merge into List
|
|
|
|
*/
|
|
|
|
|
|
|
|
function PickList(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!(this instanceof PickList)) {
|
|
|
|
return new PickList(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
List.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
PickList.prototype.__proto__ = List.prototype;
|
|
|
|
|
|
|
|
PickList.prototype.type = 'popup-menu';
|
|
|
|
|
|
|
|
PickList.prototype.pick = function(callback) {
|
|
|
|
this.screen.saveFocus();
|
|
|
|
this.focus();
|
|
|
|
this.show();
|
|
|
|
this.screen.render();
|
|
|
|
this.once('action', function(el, selected) {
|
|
|
|
self.screen.restoreFocus();
|
|
|
|
self.hide();
|
|
|
|
self.screen.render();
|
|
|
|
if (!el) return callback();
|
|
|
|
return callback(null, selected);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listbar / HorizontalList
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Listbar(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!(this instanceof Listbar)) {
|
|
|
|
return new Listbar(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.items = [];
|
|
|
|
|
|
|
|
Box.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Listbar.prototype.__proto__ = Box.prototype;
|
|
|
|
|
|
|
|
Listbar.prototype.type = 'menubar';
|
|
|
|
|
|
|
|
Listbar.prototype.setOptions =
|
|
|
|
Listbar.prototype.setCommands =
|
|
|
|
Listbar.prototype.setItems = function(commands) {
|
|
|
|
this.children.forEach(function(el) {
|
|
|
|
el.detach();
|
|
|
|
});
|
|
|
|
|
|
|
|
var self = this
|
|
|
|
, drawn = 0;
|
|
|
|
|
|
|
|
this.commands = commands;
|
|
|
|
|
|
|
|
Object.keys(commands).forEach(function(name) {
|
|
|
|
var cmd = commands[name]
|
|
|
|
, title
|
|
|
|
, len
|
|
|
|
, button;
|
|
|
|
|
|
|
|
title = '{light-black-fg}'
|
|
|
|
+ cmd.prefix
|
|
|
|
+ '{/light-black-fg}'
|
|
|
|
+ ':'
|
|
|
|
+ name;
|
|
|
|
|
|
|
|
len = (cmd.prefix + ':' + name).length;
|
|
|
|
|
|
|
|
button = new Button({
|
|
|
|
parent: self,
|
|
|
|
top: 0,
|
|
|
|
left: drawn + 1,
|
|
|
|
height: 1,
|
|
|
|
content: title,
|
|
|
|
width: len + 2,
|
|
|
|
align: 'center',
|
|
|
|
tags: true,
|
|
|
|
autoFocus: false,
|
|
|
|
hoverEffects: {
|
|
|
|
bg: 'blue'
|
|
|
|
},
|
|
|
|
focusEffects: {
|
|
|
|
bg: 'blue'
|
|
|
|
},
|
|
|
|
mouse: true
|
|
|
|
});
|
|
|
|
|
|
|
|
self._[name] = button;
|
|
|
|
cmd.element = button;
|
|
|
|
self.items.push(button);
|
|
|
|
|
|
|
|
if (cmd.callback) {
|
|
|
|
button.on('press', cmd.callback);
|
|
|
|
if (cmd.keys) {
|
|
|
|
screen.key(cmd.keys[0], cmd.callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
drawn += len + 3;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DirManager - Merge into FileManager?
|
|
|
|
*/
|
|
|
|
|
|
|
|
function DirManager(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!(this instanceof DirManager)) {
|
|
|
|
return new DirManager(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileManager.call(this, options);
|
|
|
|
|
|
|
|
this.on('cd', function(dir) {
|
|
|
|
if (dir === self.cwd) return;
|
|
|
|
self.emit('file', dir);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
DirManager.prototype.__proto__ = FileManager.prototype;
|
|
|
|
|
|
|
|
DirManager.prototype.type = 'dir-manager';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Passbox - Useless
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Passbox(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!(this instanceof Passbox)) {
|
|
|
|
return new Passbox(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
options.censor = true;
|
|
|
|
|
|
|
|
Textbox.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Passbox.prototype.__proto__ = Textbox.prototype;
|
|
|
|
|
|
|
|
Passbox.prototype.type = 'passbox';
|
|
|
|
|
2013-06-06 14:24:03 +00:00
|
|
|
/**
|
|
|
|
* Helpers
|
|
|
|
*/
|
|
|
|
|
2013-06-20 11:43:56 +00:00
|
|
|
function sp(line, width, align) {
|
|
|
|
if (!align) return line;
|
|
|
|
|
|
|
|
var len = line.replace(/\x1b\[[\d;]*m/g, '').length
|
|
|
|
, s = width - len;
|
|
|
|
|
|
|
|
if (len === 0) return line;
|
|
|
|
if (s < 0) return line;
|
|
|
|
|
|
|
|
if (align === 'center') {
|
|
|
|
s = Array(((s / 2) | 0) + 1).join(' ');
|
|
|
|
return s + line + s;
|
|
|
|
} else if (align === 'right') {
|
|
|
|
s = Array(s + 1).join(' ');
|
|
|
|
return s + line;
|
|
|
|
}
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2013-06-25 22:31:13 +00:00
|
|
|
// TODO: Add text padding.
|
2013-07-04 06:50:50 +00:00
|
|
|
// TODO: Fix a bug where, in a box with a width of 3, `jjj` is:
|
|
|
|
// |jjj|
|
|
|
|
// But `jjjj` is:
|
|
|
|
// |jj |
|
|
|
|
// |jj |
|
2013-07-12 09:59:58 +00:00
|
|
|
// A possibly related bug:
|
|
|
|
// For some reason (see jitsu-ui):
|
|
|
|
// {red-fg}my-app2{/red-fg} gets wrapped to:
|
|
|
|
// {red-fg}my-app\n2{/red-fg} when my-app2
|
|
|
|
// does not. Since escape codes are not printable
|
|
|
|
// characters, this means wrapContent is doing
|
|
|
|
// something wrong and determining length including
|
|
|
|
// at least 1 char from the escape code.
|
2013-07-16 06:54:29 +00:00
|
|
|
function wrapContent(content, width, tags, state, margin) {
|
2013-06-10 01:19:32 +00:00
|
|
|
var lines = content.split('\n')
|
|
|
|
, out = [];
|
|
|
|
|
2013-06-20 12:18:04 +00:00
|
|
|
if (!content) {
|
|
|
|
out.push(content || '');
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2013-07-16 06:54:29 +00:00
|
|
|
// Useful for textareas.
|
|
|
|
if (margin && width > 1) width--;
|
|
|
|
|
2013-06-10 01:19:32 +00:00
|
|
|
lines.forEach(function(line) {
|
2013-06-20 11:43:56 +00:00
|
|
|
var align = state
|
|
|
|
, cap;
|
|
|
|
|
2013-06-20 13:12:39 +00:00
|
|
|
if (tags) {
|
2013-07-16 06:42:21 +00:00
|
|
|
//if (cap = /^(\x1b\[[\d;]*m)*^{(left|center|right)}/.exec(line)) {
|
|
|
|
// line = (cap[1] || '') + line.substring(cap[0].length);
|
|
|
|
// align = state = cap[2] !== 'left'
|
|
|
|
// ? cap[2]
|
|
|
|
// : null;
|
|
|
|
//}
|
|
|
|
|
|
|
|
//if (cap = /{\/(left|center|right)}(\x1b\[[\d;]*m)*$/.exec(line)) {
|
|
|
|
// line = line.slice(0, -cap[0].length) + (cap[2] || '');
|
|
|
|
// state = null;
|
|
|
|
//}
|
|
|
|
|
2013-06-20 13:12:39 +00:00
|
|
|
if (cap = /^{(left|center|right)}/.exec(line)) {
|
|
|
|
line = line.substring(cap[0].length);
|
|
|
|
align = state = cap[1] !== 'left'
|
|
|
|
? cap[1]
|
|
|
|
: null;
|
|
|
|
}
|
2013-06-20 11:43:56 +00:00
|
|
|
|
2013-06-20 13:12:39 +00:00
|
|
|
if (cap = /{\/(left|center|right)}$/.exec(line)) {
|
|
|
|
line = line.slice(0, -cap[0].length);
|
|
|
|
state = null;
|
|
|
|
}
|
2013-06-20 11:43:56 +00:00
|
|
|
}
|
|
|
|
|
2013-06-10 01:19:32 +00:00
|
|
|
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;
|
2013-07-16 00:35:36 +00:00
|
|
|
if (++total === width) {
|
2013-06-20 11:43:56 +00:00
|
|
|
// Try to find a space to break on:
|
|
|
|
if (line[i] !== ' ') {
|
|
|
|
var j = i;
|
|
|
|
while (j > i - 10 && j > 0 && line[j] !== ' ') j--;
|
|
|
|
if (line[j] === ' ') i = j + 1;
|
2013-07-16 00:35:36 +00:00
|
|
|
else i++;
|
2013-06-20 11:43:56 +00:00
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-06-10 01:19:32 +00:00
|
|
|
}
|
|
|
|
|
2013-07-12 01:48:30 +00:00
|
|
|
// XXX This shouldn't be required, but is.
|
|
|
|
i++;
|
2013-06-27 00:15:49 +00:00
|
|
|
|
2013-06-10 01:19:32 +00:00
|
|
|
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);
|
2013-06-20 11:43:56 +00:00
|
|
|
out.push(sp(part, width, align));
|
2013-06-10 01:19:32 +00:00
|
|
|
} else {
|
|
|
|
line = line.substring(i - 1);
|
2013-06-20 11:43:56 +00:00
|
|
|
out.push(sp(part, width, align));
|
2013-06-10 01:19:32 +00:00
|
|
|
}
|
2013-07-15 22:45:08 +00:00
|
|
|
|
|
|
|
// Make sure we didn't wrap the line to the very end, otherwise
|
|
|
|
// we get a pointless empty line after a newline.
|
|
|
|
if (line === '') return;
|
2013-06-10 01:19:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If only an escape code got cut off, at it to `part`.
|
|
|
|
if (/^(?:\x1b[\[\d;]*m)+$/.test(line)) {
|
|
|
|
out[out.length-1] += line;
|
|
|
|
return;
|
|
|
|
}
|
2013-07-12 01:48:30 +00:00
|
|
|
|
2013-06-20 11:43:56 +00:00
|
|
|
out.push(sp(line, width, align));
|
2013-06-10 01:19:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2013-07-12 12:48:55 +00:00
|
|
|
function asort(obj) {
|
|
|
|
return obj.sort(function(a, b) {
|
|
|
|
a = a.name.toLowerCase();
|
|
|
|
b = b.name.toLowerCase();
|
|
|
|
|
|
|
|
if (a[0] === '.' && b[0] === '.') {
|
|
|
|
a = a[1];
|
|
|
|
b = b[1];
|
|
|
|
} else {
|
|
|
|
a = a[0];
|
|
|
|
b = b[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return a > b ? 1 : (a < b ? -1 : 0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-07-12 13:58:25 +00:00
|
|
|
function hsort(obj) {
|
|
|
|
return obj.sort(function(a, b) {
|
|
|
|
return b.index - a.index;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Expose
|
|
|
|
*/
|
|
|
|
|
2013-07-14 11:57:15 +00:00
|
|
|
exports.colors = colors;
|
|
|
|
|
2013-07-03 23:46:06 +00:00
|
|
|
exports.Screen = exports.screen = Screen;
|
|
|
|
exports.Box = exports.box = Box;
|
|
|
|
exports.Text = exports.text = Text;
|
|
|
|
exports.Line = exports.line = Line;
|
|
|
|
exports.ScrollableBox = exports.scrollablebox = ScrollableBox;
|
|
|
|
exports.List = exports.list = List;
|
|
|
|
exports.ScrollableText = exports.scrollabletext = ScrollableText;
|
|
|
|
exports.Input = exports.input = Input;
|
|
|
|
exports.Textbox = exports.textbox = Textbox;
|
|
|
|
exports.Textarea = exports.textarea = Textarea;
|
|
|
|
exports.Button = exports.button = Button;
|
|
|
|
exports.ProgressBar = exports.progressbar = ProgressBar;
|
2013-07-12 12:48:55 +00:00
|
|
|
exports.FileManager = exports.filemanager = FileManager;
|
2013-07-14 01:44:30 +00:00
|
|
|
|
|
|
|
exports.Checkbox = exports.checkbox = Checkbox;
|
|
|
|
exports.RadioButton = exports.radiobutton = RadioButton;
|
|
|
|
exports.Prompt = exports.prompt = Prompt;
|
|
|
|
exports.Question = exports.question = Question;
|
|
|
|
exports.Message = exports.message = Message;
|
|
|
|
exports.Info = exports.info = Info;
|
|
|
|
exports.Loading = exports.loading = Loading;
|
|
|
|
exports.PickList = exports.picklist = PickList;
|
|
|
|
exports.Listbar = exports.listbar = Listbar;
|
|
|
|
exports.DirManager = exports.dirmanager = DirManager;
|
|
|
|
exports.Passbox = exports.passbox = Passbox;
|