2013-06-01 02:09:07 +00:00
|
|
|
/**
|
2013-07-24 19:40:05 +00:00
|
|
|
* widget.js - high-level interface for blessed
|
2015-02-01 15:22:39 +00:00
|
|
|
* Copyright (c) 2013-2015, Christopher Jeffrey and contributors (MIT License).
|
2013-07-24 19:40:05 +00:00
|
|
|
* https://github.com/chjj/blessed
|
2013-06-01 02:09:07 +00:00
|
|
|
*/
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
/**
|
|
|
|
* Modules
|
|
|
|
*/
|
|
|
|
|
2013-08-01 00:52:25 +00:00
|
|
|
var EventEmitter = require('./events').EventEmitter
|
2013-07-19 01:55:16 +00:00
|
|
|
, assert = require('assert')
|
2013-07-12 12:48:55 +00:00
|
|
|
, path = require('path')
|
2013-08-27 13:58:53 +00:00
|
|
|
, fs = require('fs');
|
|
|
|
|
|
|
|
var colors = require('./colors')
|
|
|
|
, program = require('./program')
|
2013-07-17 06:37:57 +00:00
|
|
|
, widget = exports;
|
2013-06-06 09:03:25 +00:00
|
|
|
|
2015-01-31 20:24:55 +00:00
|
|
|
var nextTick = global.setImmediate || process.nextTick.bind(process);
|
|
|
|
|
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-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
|
|
|
this.options = options;
|
2013-06-09 13:33:23 +00:00
|
|
|
this.screen = this.screen
|
2013-07-29 15:22:08 +00:00
|
|
|
|| options.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-07-29 15:22:08 +00:00
|
|
|
if (this.type !== 'screen') {
|
|
|
|
this.detached = true;
|
2013-06-18 15:52:48 +00:00
|
|
|
}
|
|
|
|
|
2013-07-29 15:22:08 +00:00
|
|
|
if (this.parent) {
|
|
|
|
this.parent.append(this);
|
2013-06-20 11:43:56 +00:00
|
|
|
}
|
|
|
|
|
2013-06-09 13:33:23 +00:00
|
|
|
(options.children || []).forEach(this.append.bind(this));
|
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) {
|
2013-07-29 15:22:08 +00:00
|
|
|
var self = this;
|
2013-07-15 03:11:48 +00:00
|
|
|
|
|
|
|
element.detach();
|
|
|
|
element.parent = this;
|
|
|
|
|
2013-07-29 15:22:08 +00:00
|
|
|
if (i === 0) {
|
|
|
|
this.children.unshift(element);
|
|
|
|
} else if (i === this.children.length) {
|
|
|
|
this.children.push(element);
|
|
|
|
} else {
|
|
|
|
this.children.splice(i, 0, element);
|
2013-07-15 03:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
element.emit('reparent', this);
|
|
|
|
this.emit('adopt', element);
|
|
|
|
|
2013-07-29 15:22:08 +00:00
|
|
|
(function emit(el) {
|
|
|
|
var n = el.detached !== self.detached;
|
|
|
|
el.detached = self.detached;
|
|
|
|
if (n) el.emit('attach');
|
|
|
|
el.children.forEach(emit);
|
|
|
|
})(element);
|
|
|
|
|
|
|
|
if (!this.screen.focused) {
|
|
|
|
this.screen.focused = element;
|
2013-07-18 20:06:33 +00:00
|
|
|
}
|
2013-07-15 03:11:48 +00:00
|
|
|
};
|
|
|
|
|
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-18 20:06:33 +00:00
|
|
|
if (element.parent !== this) return;
|
|
|
|
|
2013-06-01 02:39:11 +00:00
|
|
|
var i = this.children.indexOf(element);
|
2013-07-29 15:22:08 +00:00
|
|
|
if (!~i) return;
|
|
|
|
|
2013-08-08 18:47:59 +00:00
|
|
|
element.clearPos();
|
2013-06-09 13:33:23 +00:00
|
|
|
|
2013-07-29 15:22:08 +00:00
|
|
|
element.parent = null;
|
|
|
|
|
|
|
|
this.children.splice(i, 1);
|
|
|
|
|
2013-08-08 18:47:59 +00:00
|
|
|
i = this.screen.clickable.indexOf(element);
|
|
|
|
if (~i) this.screen.clickable.splice(i, 1);
|
|
|
|
i = this.screen.keyable.indexOf(element);
|
|
|
|
if (~i) this.screen.keyable.splice(i, 1);
|
2013-06-18 15:52:48 +00:00
|
|
|
|
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) {
|
2013-07-29 15:22:08 +00:00
|
|
|
var n = el.detached !== true;
|
|
|
|
el.detached = true;
|
|
|
|
if (n) el.emit('detach');
|
|
|
|
el.children.forEach(emit);
|
2013-06-20 11:43:56 +00:00
|
|
|
})(element);
|
2013-06-20 12:18:04 +00:00
|
|
|
|
2013-07-29 15:22:08 +00:00
|
|
|
if (this.screen.focused === element) {
|
|
|
|
this.screen.rewindFocus();
|
|
|
|
}
|
2013-06-01 02:39:11 +00:00
|
|
|
};
|
|
|
|
|
2013-06-20 16:48:12 +00:00
|
|
|
Node.prototype.detach = function() {
|
2013-07-18 22:56:05 +00:00
|
|
|
if (this.parent) this.parent.remove(this);
|
2013-06-01 02:39:11 +00:00
|
|
|
};
|
2013-06-01 02:09:07 +00:00
|
|
|
|
2013-07-19 01:55:16 +00:00
|
|
|
Node.prototype.forDescendants = function(iter, s) {
|
|
|
|
if (s) iter(this);
|
2013-07-24 22:16:55 +00:00
|
|
|
this.children.forEach(function emit(el) {
|
2013-07-19 01:55:16 +00:00
|
|
|
iter(el);
|
|
|
|
el.children.forEach(emit);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Node.prototype.forAncestors = function(iter, s) {
|
|
|
|
var el = this;
|
|
|
|
if (s) iter(this);
|
|
|
|
while (el = el.parent) {
|
|
|
|
iter(el);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Node.prototype.collectDescendants = function(s) {
|
|
|
|
var out = [];
|
|
|
|
this.forDescendants(function(el) {
|
|
|
|
out.push(el);
|
|
|
|
}, s);
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
Node.prototype.collectAncestors = function(s) {
|
|
|
|
var out = [];
|
|
|
|
this.forAncestors(function(el) {
|
|
|
|
out.push(el);
|
|
|
|
}, s);
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2013-07-19 01:55:16 +00:00
|
|
|
return this.forDescendants(function(el) {
|
2013-06-20 12:18:04 +00:00
|
|
|
if (iter) iter(el);
|
|
|
|
el.emit.apply(el, args);
|
2013-07-19 01:55:16 +00:00
|
|
|
}, true);
|
2013-06-20 12:18:04 +00:00
|
|
|
};
|
|
|
|
|
2013-07-12 13:58:25 +00:00
|
|
|
Node.prototype.emitAncestors = function() {
|
|
|
|
var args = Array.prototype.slice(arguments)
|
|
|
|
, iter;
|
|
|
|
|
|
|
|
if (typeof args[args.length-1] === 'function') {
|
|
|
|
iter = args.pop();
|
|
|
|
}
|
|
|
|
|
2013-07-19 01:55:16 +00:00
|
|
|
return this.forAncestors(function(el) {
|
2013-07-12 13:58:25 +00:00
|
|
|
if (iter) iter(el);
|
|
|
|
el.emit.apply(el, args);
|
2013-07-19 01:55:16 +00:00
|
|
|
}, true);
|
2013-07-12 13:58:25 +00:00
|
|
|
};
|
|
|
|
|
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-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-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-06-14 20:58:54 +00:00
|
|
|
return new Screen(options);
|
|
|
|
}
|
|
|
|
|
2013-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
|
|
|
if (options.rsety && options.listen) {
|
2013-06-14 20:58:54 +00:00
|
|
|
options = { program: options };
|
|
|
|
}
|
|
|
|
|
2013-08-01 08:13:20 +00:00
|
|
|
this.program = options.program || program.global;
|
|
|
|
|
|
|
|
if (!this.program) {
|
|
|
|
this.program = program({
|
|
|
|
input: options.input,
|
|
|
|
output: options.output,
|
|
|
|
log: options.log,
|
|
|
|
debug: options.debug,
|
|
|
|
dump: options.dump,
|
2013-08-03 01:59:49 +00:00
|
|
|
term: options.term,
|
2013-08-01 08:13:20 +00:00
|
|
|
tput: true,
|
|
|
|
buffer: true,
|
|
|
|
zero: true
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.program.setupTput();
|
|
|
|
this.program.useBuffer = true;
|
|
|
|
this.program.zero = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.tput = this.program.tput;
|
2013-06-14 20:58:54 +00:00
|
|
|
|
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-07-30 13:50:49 +00:00
|
|
|
this.autoPadding = options.autoPadding;
|
2013-07-24 23:05:56 +00:00
|
|
|
this.tabc = Array((options.tabSize || 4) + 1).join(' ');
|
2013-08-01 08:13:20 +00:00
|
|
|
|
2014-06-04 04:51:16 +00:00
|
|
|
this.ignoreLocked = options.ignoreLocked || [];
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
this.dattr = ((0 << 18) | (0x1ff << 9)) | 0x1ff;
|
2013-07-12 05:36:36 +00:00
|
|
|
|
2014-01-11 22:52:33 +00:00
|
|
|
this.renders = 0;
|
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-08-04 03:58:09 +00:00
|
|
|
//get height() { return self.height; },
|
|
|
|
//get width() { return self.width; }
|
2013-06-01 05:29:31 +00:00
|
|
|
};
|
|
|
|
|
2013-07-30 13:50:49 +00:00
|
|
|
this.ileft = 0;
|
|
|
|
this.itop = 0;
|
|
|
|
this.iright = 0;
|
|
|
|
this.ibottom = 0;
|
|
|
|
this.iheight = 0;
|
|
|
|
this.iwidth = 0;
|
|
|
|
|
|
|
|
this.padding = {
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
right: 0,
|
|
|
|
bottom: 0
|
|
|
|
};
|
|
|
|
|
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 = [];
|
2013-07-17 05:53:34 +00:00
|
|
|
this.keyable = [];
|
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-07-23 22:57:04 +00:00
|
|
|
this._buf = '';
|
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');
|
2013-07-29 15:22:08 +00:00
|
|
|
el.children.forEach(emit);
|
2013-06-24 11:16:02 +00:00
|
|
|
})(self);
|
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');
|
|
|
|
});
|
|
|
|
|
2014-01-02 04:42:38 +00:00
|
|
|
this.enter();
|
2013-06-09 16:47:02 +00:00
|
|
|
|
2015-01-31 20:32:33 +00:00
|
|
|
// XXX Not used right now since we're using our own EventEmitter:
|
|
|
|
this.setMaxListeners(Infinity);
|
2013-06-24 11:16:02 +00:00
|
|
|
|
2015-01-31 20:19:00 +00:00
|
|
|
Screen.total++;
|
2013-06-18 11:30:26 +00:00
|
|
|
|
2015-01-31 20:19:00 +00:00
|
|
|
process.on('uncaughtException', function(err) {
|
|
|
|
if (process.listeners('uncaughtException').length > Screen.total) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-31 20:24:55 +00:00
|
|
|
self.leave();
|
2015-01-31 20:19:00 +00:00
|
|
|
err = err || new Error('Uncaught Exception.');
|
|
|
|
console.error(err.stack ? err.stack + '' : err + '');
|
2015-01-31 20:24:55 +00:00
|
|
|
nextTick(function() {
|
|
|
|
process.exit(1);
|
|
|
|
});
|
2015-01-31 20:19:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
['SIGTERM', 'SIGINT', 'SIGQUIT'].forEach(function(signal) {
|
|
|
|
process.on(signal, function() {
|
|
|
|
if (process.listeners(signal).length > Screen.total) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-31 20:24:55 +00:00
|
|
|
nextTick(function() {
|
|
|
|
process.exit(0);
|
|
|
|
});
|
2015-01-31 20:19:00 +00:00
|
|
|
});
|
2013-07-29 20:49:25 +00:00
|
|
|
});
|
|
|
|
|
2013-06-18 11:30:26 +00:00
|
|
|
process.on('exit', function() {
|
2015-01-31 20:24:55 +00:00
|
|
|
self.leave();
|
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
|
|
|
|
2015-01-31 20:19:00 +00:00
|
|
|
Screen.total = 0;
|
|
|
|
|
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';
|
|
|
|
|
2015-01-28 04:39:00 +00:00
|
|
|
Screen.prototype.__defineGetter__('title', function() {
|
|
|
|
return this.program.title;
|
|
|
|
});
|
|
|
|
|
|
|
|
Screen.prototype.__defineSetter__('title', function(title) {
|
|
|
|
return this.program.title = title;
|
|
|
|
});
|
|
|
|
|
2014-01-02 04:42:38 +00:00
|
|
|
Screen.prototype.enter = function() {
|
|
|
|
if (this.program.isAlt) return;
|
|
|
|
this.program.alternateBuffer();
|
|
|
|
this.program.hideCursor();
|
|
|
|
this.program.cup(0, 0);
|
|
|
|
//this.program.csr(0, this.height - 1);
|
|
|
|
this.alloc();
|
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.leave = function() {
|
|
|
|
if (!this.program.isAlt) return;
|
|
|
|
if (this.program.scrollTop !== 0
|
|
|
|
|| this.program.scrollBottom !== this.rows - 1) {
|
|
|
|
this.program.csr(0, this.height - 1);
|
|
|
|
}
|
|
|
|
// this.program.clear();
|
|
|
|
this.alloc();
|
|
|
|
this.program.showCursor();
|
|
|
|
if (this._listenedMouse) {
|
|
|
|
this.program.disableMouse();
|
|
|
|
}
|
2015-02-01 17:51:05 +00:00
|
|
|
this.program.normalBuffer();
|
2014-01-02 04:42:38 +00:00
|
|
|
this.program.flush();
|
|
|
|
};
|
|
|
|
|
2013-07-31 05:14:13 +00:00
|
|
|
Screen.prototype.log = function() {
|
|
|
|
return this.program.log.apply(this.program, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.debug = function() {
|
|
|
|
return this.program.debug.apply(this.program, arguments);
|
|
|
|
};
|
|
|
|
|
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)) {
|
2013-07-17 05:53:34 +00:00
|
|
|
el.clickable = true;
|
2013-06-16 14:31:55 +00:00
|
|
|
this.clickable.push(el);
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this._listenedMouse) return;
|
|
|
|
this._listenedMouse = true;
|
|
|
|
|
|
|
|
this.program.enableMouse();
|
|
|
|
|
2013-08-01 06:52:28 +00:00
|
|
|
this.on('render', function() {
|
|
|
|
self._needsClickableSort = true;
|
|
|
|
});
|
|
|
|
|
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-08-01 06:52:28 +00:00
|
|
|
if (self._needsClickableSort) {
|
|
|
|
self.clickable = hsort(self.clickable);
|
|
|
|
self._needsClickableSort = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var i = 0
|
2013-06-16 14:31:55 +00:00
|
|
|
, el
|
|
|
|
, set
|
2013-08-04 09:34:39 +00:00
|
|
|
, pos;
|
2013-06-16 14:31:55 +00:00
|
|
|
|
2013-08-01 06:52:28 +00:00
|
|
|
for (; i < self.clickable.length; i++) {
|
|
|
|
el = self.clickable[i];
|
2013-07-12 15:22:06 +00:00
|
|
|
|
2013-08-04 09:34:39 +00:00
|
|
|
if (el.detached || !el.visible) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-16 14:31:55 +00:00
|
|
|
|
2013-08-01 06:52:28 +00:00
|
|
|
// if (self.grabMouse && self.focused !== el
|
2013-07-12 15:22:06 +00:00
|
|
|
// && !el.hasAncestor(self.focused)) continue;
|
|
|
|
|
2013-08-04 09:34:39 +00:00
|
|
|
pos = el.lpos;
|
|
|
|
if (!pos) continue;
|
2013-06-16 14:31:55 +00:00
|
|
|
|
2013-08-04 09:34:39 +00:00
|
|
|
if (data.x >= pos.xi && data.x < pos.xl
|
|
|
|
&& data.y >= pos.yi && data.y < pos.yl) {
|
2013-06-06 09:03:25 +00:00
|
|
|
el.emit('mouse', data);
|
2014-05-30 13:32:19 +00:00
|
|
|
if (data.action === 'mousedown') {
|
|
|
|
self.mouseDown = el;
|
|
|
|
} else if (data.action === 'mouseup') {
|
|
|
|
(self.mouseDown || el).emit('click', data);
|
|
|
|
self.mouseDown = null;
|
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);
|
|
|
|
}
|
|
|
|
el.emit('mouseover', 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
|
|
|
|
|
|
|
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.hover = null;
|
|
|
|
}
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
self.emit('mouse', data);
|
|
|
|
});
|
2013-08-01 06:52:28 +00:00
|
|
|
|
2013-08-04 09:34:39 +00:00
|
|
|
// Autofocus highest element.
|
|
|
|
// this.on('element click', function(el, data) {
|
|
|
|
// var target;
|
|
|
|
// do {
|
|
|
|
// if (el.clickable === true && el.options.autoFocus !== false) {
|
|
|
|
// target = el;
|
|
|
|
// }
|
|
|
|
// } while (el = el.parent);
|
|
|
|
// if (target) target.focus();
|
|
|
|
// });
|
|
|
|
|
2013-08-01 06:52:28 +00:00
|
|
|
// Autofocus elements with the appropriate option.
|
|
|
|
this.on('element click', function(el, data) {
|
|
|
|
if (el.clickable === true && el.options.autoFocus !== false) {
|
2013-08-04 09:34:39 +00:00
|
|
|
el.focus();
|
2013-08-01 06:52:28 +00:00
|
|
|
}
|
|
|
|
});
|
2013-06-06 09:03:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype._listenKeys = function(el) {
|
|
|
|
var self = this;
|
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
if (el && !~this.keyable.indexOf(el)) {
|
|
|
|
el.keyable = true;
|
|
|
|
this.keyable.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) {
|
2014-06-04 04:51:16 +00:00
|
|
|
if (self.lockKeys && !~self.ignoreLocked.indexOf(key.full)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-12 11:08:16 +00:00
|
|
|
if (focused && focused.keyable) {
|
2013-06-25 22:31:13 +00:00
|
|
|
focused.emit('keypress', ch, key);
|
2013-07-12 09:59:58 +00:00
|
|
|
focused.emit('key ' + key.full, 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-08-27 13:58:53 +00:00
|
|
|
// TODO: Possibly get rid of .dirty altogether.
|
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-08-15 12:14:56 +00:00
|
|
|
//el._rendering = true;
|
2013-06-01 02:09:07 +00:00
|
|
|
el.render();
|
2013-08-15 12:14:56 +00:00
|
|
|
//el._rendering = false;
|
2013-06-01 02:09:07 +00:00
|
|
|
});
|
2013-06-29 18:09:22 +00:00
|
|
|
this._ci = -1;
|
2013-07-15 23:53:04 +00:00
|
|
|
|
2015-02-13 08:59:47 +00:00
|
|
|
// Cannot use this.rows here because of the resize delay:
|
|
|
|
// `rows` and `cols` get set instantly by the resize, but `alloc` has a 300ms
|
|
|
|
// delay before the new cells are added, which will sometimes throw. Measure
|
|
|
|
// by lines.
|
|
|
|
this.draw(0, this.lines.length - 1);
|
2013-07-15 23:53:04 +00:00
|
|
|
|
2014-01-12 00:54:26 +00:00
|
|
|
// XXX Workaround to deal with cursor pos before the screen has rendered and
|
|
|
|
// lpos is not reliable (stale).
|
2014-01-11 22:52:33 +00:00
|
|
|
if (this.focused && this.focused._updateCursor) {
|
|
|
|
this.focused._updateCursor(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.renders++;
|
|
|
|
|
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-23 22:57:04 +00:00
|
|
|
// if (y === top) return this.insertLineNC(n, y, top, bottom);
|
|
|
|
|
2013-08-27 13:58:53 +00:00
|
|
|
if (!this.tput.strings.change_scroll_region
|
2013-07-14 16:04:48 +00:00
|
|
|
|| !this.tput.strings.delete_line
|
|
|
|
|| !this.tput.strings.insert_line) return;
|
|
|
|
|
2013-07-23 22:57:04 +00:00
|
|
|
this._buf += this.tput.csr(top, bottom);
|
|
|
|
this._buf += this.tput.cup(y, 0);
|
|
|
|
this._buf += this.tput.il(n);
|
|
|
|
this._buf += this.tput.csr(0, this.height - 1);
|
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-23 22:57:04 +00:00
|
|
|
// if (y === top) return this.deleteLineNC(n, y, top, bottom);
|
|
|
|
|
2013-08-27 13:58:53 +00:00
|
|
|
if (!this.tput.strings.change_scroll_region
|
2013-07-14 16:04:48 +00:00
|
|
|
|| !this.tput.strings.delete_line
|
|
|
|
|| !this.tput.strings.insert_line) return;
|
|
|
|
|
2013-07-23 22:57:04 +00:00
|
|
|
this._buf += this.tput.csr(top, bottom);
|
|
|
|
this._buf += this.tput.cup(y, 0);
|
|
|
|
this._buf += this.tput.dl(n);
|
|
|
|
this._buf += this.tput.csr(0, this.height - 1);
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-22 02:00:13 +00:00
|
|
|
// This is how ncurses does it.
|
|
|
|
// Scroll down (up cursor-wise).
|
|
|
|
// This will only work for top line deletion as opposed to arbitrary lines.
|
|
|
|
Screen.prototype.insertLineNC = function(n, y, top, bottom) {
|
2013-08-27 13:58:53 +00:00
|
|
|
if (!this.tput.strings.change_scroll_region
|
2013-08-04 03:58:09 +00:00
|
|
|
|| !this.tput.strings.delete_line) return;
|
2013-07-22 02:00:13 +00:00
|
|
|
|
2013-07-23 22:57:04 +00:00
|
|
|
this._buf += this.tput.csr(top, bottom);
|
|
|
|
this._buf += this.tput.cup(top, 0);
|
|
|
|
this._buf += this.tput.dl(n);
|
|
|
|
this._buf += this.tput.csr(0, this.height - 1);
|
2013-07-22 02:00:13 +00:00
|
|
|
|
|
|
|
var j = bottom + 1;
|
|
|
|
|
|
|
|
while (n--) {
|
|
|
|
this.lines.splice(j, 0, this.blankLine());
|
|
|
|
this.lines.splice(y, 1);
|
|
|
|
this.olines.splice(j, 0, this.blankLine());
|
|
|
|
this.olines.splice(y, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This is how ncurses does it.
|
|
|
|
// Scroll up (down cursor-wise).
|
|
|
|
// This will only work for bottom line deletion as opposed to arbitrary lines.
|
|
|
|
Screen.prototype.deleteLineNC = function(n, y, top, bottom) {
|
2013-08-27 13:58:53 +00:00
|
|
|
if (!this.tput.strings.change_scroll_region
|
2013-08-04 03:58:09 +00:00
|
|
|
|| !this.tput.strings.delete_line) return;
|
2013-07-22 02:00:13 +00:00
|
|
|
|
2013-07-23 22:57:04 +00:00
|
|
|
this._buf += this.tput.csr(top, bottom);
|
|
|
|
this._buf += this.tput.cup(bottom, 0);
|
|
|
|
this._buf += Array(n + 1).join('\n');
|
|
|
|
this._buf += this.tput.csr(0, this.height - 1);
|
2013-07-22 02:00:13 +00:00
|
|
|
|
|
|
|
var j = bottom + 1;
|
|
|
|
|
|
|
|
while (n--) {
|
|
|
|
this.lines.splice(j, 0, this.blankLine());
|
|
|
|
this.lines.splice(y, 1);
|
|
|
|
this.olines.splice(j, 0, this.blankLine());
|
|
|
|
this.olines.splice(y, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-13 06:33:10 +00:00
|
|
|
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-17 09:38:11 +00:00
|
|
|
var pos = el.lpos;
|
2013-07-14 16:58:44 +00:00
|
|
|
|
|
|
|
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-08-10 14:49:39 +00:00
|
|
|
if (this.options.fastCSR) {
|
|
|
|
// Maybe just do this instead of parsing.
|
|
|
|
if (this.width - (pos.xl - pos.xi) < 40) {
|
|
|
|
return pos._cleanSides = true;
|
|
|
|
}
|
|
|
|
return pos._cleanSides = false;
|
|
|
|
}
|
|
|
|
|
2013-07-15 06:44:11 +00:00
|
|
|
if (!this.options.smartCSR) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-18 07:37:18 +00:00
|
|
|
// The scrollbar can't update properly, and there's also a
|
|
|
|
// chance that the scrollbar may get moved around senselessly.
|
2013-07-27 13:13:00 +00:00
|
|
|
// NOTE: In pratice, this doesn't seem to be the case.
|
|
|
|
// if (this.scrollbar) {
|
2013-08-10 14:49:39 +00:00
|
|
|
// return pos._cleanSides = false;
|
2013-07-27 13:13:00 +00:00
|
|
|
// }
|
|
|
|
|
|
|
|
// Doesn't matter if we're only a height of 1.
|
|
|
|
// if ((pos.yl - el.ibottom) - (pos.yi + el.itop) <= 1) {
|
|
|
|
// return pos._cleanSides = false;
|
|
|
|
// }
|
2013-07-18 07:37:18 +00:00
|
|
|
|
2013-07-19 07:28:31 +00:00
|
|
|
var yi = pos.yi + el.itop
|
|
|
|
, yl = pos.yl - el.ibottom
|
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) {
|
2013-10-16 11:27:03 +00:00
|
|
|
// this.emit('predraw');
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
var x
|
|
|
|
, y
|
|
|
|
, line
|
|
|
|
, out
|
|
|
|
, ch
|
|
|
|
, data
|
|
|
|
, attr
|
2013-07-15 18:15:41 +00:00
|
|
|
, fg
|
|
|
|
, bg
|
2013-07-22 04:53:10 +00:00
|
|
|
, flags;
|
|
|
|
|
2013-08-01 08:13:20 +00:00
|
|
|
var main = ''
|
|
|
|
, pre
|
|
|
|
, post;
|
2013-07-23 22:57:04 +00:00
|
|
|
|
2013-07-22 04:53:10 +00:00
|
|
|
var clr
|
|
|
|
, neq
|
|
|
|
, xx;
|
2013-06-01 02:09:07 +00:00
|
|
|
|
2013-06-06 10:19:52 +00:00
|
|
|
var lx = -1
|
|
|
|
, ly = -1
|
|
|
|
, o;
|
|
|
|
|
2013-07-22 04:53:10 +00:00
|
|
|
var acs;
|
2013-07-22 02:00:13 +00:00
|
|
|
|
2013-07-23 22:57:04 +00:00
|
|
|
if (this._buf) {
|
|
|
|
main += this._buf;
|
|
|
|
this._buf = '';
|
|
|
|
}
|
2013-06-01 02:09:07 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if (!line.dirty) continue;
|
|
|
|
line.dirty = false;
|
|
|
|
|
|
|
|
out = '';
|
|
|
|
attr = this.dattr;
|
|
|
|
|
2015-02-13 08:59:47 +00:00
|
|
|
// Cannot use this.cols here because of the resize delay:
|
|
|
|
// `rows` and `cols` get set instantly by the resize, but `alloc` has a
|
|
|
|
// 300ms delay before the new cells are added, which will sometimes throw.
|
|
|
|
// Measure by line length.
|
|
|
|
for (x = 0; x < line.length; x++) {
|
2013-06-01 05:29:31 +00:00
|
|
|
data = line[x][0];
|
|
|
|
ch = line[x][1];
|
2013-06-01 02:09:07 +00:00
|
|
|
|
2013-07-22 04:53:10 +00:00
|
|
|
// Take advantage of xterm's back_color_erase feature by using a
|
|
|
|
// lookahead. Stop spitting out so many damn spaces. NOTE: Is checking
|
|
|
|
// the bg for non BCE terminals worth the overhead?
|
2013-07-23 10:04:04 +00:00
|
|
|
if (this.options.useBCE
|
|
|
|
&& ch === ' '
|
2013-08-27 13:58:53 +00:00
|
|
|
&& (this.tput.bools.back_color_erase
|
2013-07-22 04:53:10 +00:00
|
|
|
|| (data & 0x1ff) === (this.dattr & 0x1ff))
|
|
|
|
&& ((data >> 18) & 8) === ((this.dattr >> 18) & 8)) {
|
|
|
|
clr = true;
|
|
|
|
neq = false;
|
|
|
|
|
|
|
|
for (xx = x; xx < this.cols; xx++) {
|
|
|
|
if (line[xx][0] !== data || line[xx][1] !== ' ') {
|
|
|
|
clr = false;
|
2013-07-22 02:00:13 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-07-22 04:53:10 +00:00
|
|
|
if (line[xx][0] !== o[xx][0] || line[xx][1] !== o[xx][1]) {
|
|
|
|
neq = true;
|
2013-07-22 02:00:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-22 04:53:10 +00:00
|
|
|
if (clr && neq) {
|
2013-08-18 19:42:39 +00:00
|
|
|
lx = -1, ly = -1;
|
2013-07-22 02:00:13 +00:00
|
|
|
if (data !== attr) {
|
|
|
|
out += this.codeAttr(data);
|
|
|
|
attr = data;
|
|
|
|
}
|
2013-08-27 13:58:53 +00:00
|
|
|
out += this.tput.cup(y, x);
|
|
|
|
out += this.tput.el();
|
2013-07-22 04:53:10 +00:00
|
|
|
for (xx = x; xx < this.cols; xx++) {
|
|
|
|
o[xx][0] = data;
|
|
|
|
o[xx][1] = ' ';
|
2013-07-22 02:00:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-07-22 04:53:10 +00:00
|
|
|
// If there's more than 10 spaces, use EL regardless
|
|
|
|
// and start over drawing the rest of line. Might
|
2013-07-27 11:53:09 +00:00
|
|
|
// not be worth it. Try to use ECH if the terminal
|
|
|
|
// supports it. Maybe only try to use ECH here.
|
2013-08-27 13:58:53 +00:00
|
|
|
// //if (this.tput.strings.erase_chars)
|
2013-07-22 04:53:10 +00:00
|
|
|
// if (!clr && neq && (xx - x) > 10) {
|
2013-08-18 19:42:39 +00:00
|
|
|
// lx = -1, ly = -1;
|
2013-07-22 04:53:10 +00:00
|
|
|
// if (data !== attr) {
|
|
|
|
// out += this.codeAttr(data);
|
|
|
|
// attr = data;
|
|
|
|
// }
|
2013-08-27 13:58:53 +00:00
|
|
|
// out += this.tput.cup(y, x);
|
|
|
|
// if (this.tput.strings.erase_chars) {
|
2013-07-27 11:53:09 +00:00
|
|
|
// // Use erase_chars to avoid erasing the whole line.
|
2013-08-27 13:58:53 +00:00
|
|
|
// out += this.tput.ech(xx - x);
|
2013-07-27 11:53:09 +00:00
|
|
|
// } else {
|
2013-08-27 13:58:53 +00:00
|
|
|
// out += this.tput.el();
|
2013-07-27 11:53:09 +00:00
|
|
|
// }
|
2013-08-27 13:58:53 +00:00
|
|
|
// out += this.tput.cuf(xx - x);
|
2013-07-27 11:53:09 +00:00
|
|
|
// this.fillRegion(data, ' ',
|
2013-08-27 13:58:53 +00:00
|
|
|
// x, this.tput.strings.erase_chars ? xx : this.cols,
|
2013-07-27 11:58:50 +00:00
|
|
|
// y, y + 1);
|
2013-07-27 11:53:09 +00:00
|
|
|
// x = xx - 1;
|
2013-07-22 04:53:10 +00:00
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Skip to the next line if the
|
|
|
|
// rest of the line is already drawn.
|
2013-07-27 11:53:09 +00:00
|
|
|
// if (!neq) {
|
|
|
|
// for (; xx < this.cols; xx++) {
|
|
|
|
// if (line[xx][0] !== o[xx][0] || line[xx][1] !== o[xx][1]) {
|
|
|
|
// neq = true;
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// if (!neq) {
|
|
|
|
// attr = data;
|
2013-07-22 04:53:10 +00:00
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// }
|
2013-07-22 02:00:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Optimize by comparing the real output
|
|
|
|
// buffer to the pending output buffer.
|
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-08-27 13:58:53 +00:00
|
|
|
out += y === ly
|
|
|
|
? this.tput.cuf(x - lx)
|
|
|
|
: this.tput.cup(y, x);
|
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
|
2013-07-29 22:30:13 +00:00
|
|
|
// support UTF8 get their unicode characters
|
2013-07-16 22:50:02 +00:00
|
|
|
// 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.
|
2015-01-30 05:53:06 +00:00
|
|
|
if (this.tput.strings.enter_alt_charset_mode && !this.tput.brokenACS) {
|
2015-02-09 06:01:24 +00:00
|
|
|
// Fun fact: even if this.tput.brokenACS wasn't checked here,
|
|
|
|
// the linux console would still work fine because the acs
|
|
|
|
// table would fail the check of: this.tput.acscr[ch]
|
2015-01-30 05:53:06 +00:00
|
|
|
if (this.tput.acscr[ch]) {
|
|
|
|
if (acs) {
|
|
|
|
ch = this.tput.acscr[ch];
|
|
|
|
} else {
|
|
|
|
ch = this.tput.smacs()
|
|
|
|
+ this.tput.acscr[ch];
|
|
|
|
acs = true;
|
2013-08-10 14:30:05 +00:00
|
|
|
}
|
2015-01-30 05:53:06 +00:00
|
|
|
} else if (acs) {
|
|
|
|
ch = this.tput.rmacs() + ch;
|
|
|
|
acs = false;
|
2013-08-10 14:30:05 +00:00
|
|
|
}
|
2013-08-27 13:58:53 +00:00
|
|
|
} else {
|
|
|
|
// 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.
|
2015-02-08 22:35:08 +00:00
|
|
|
// NOTE: It could be the case that the $LANG
|
|
|
|
// is all that matters in some cases:
|
2015-02-08 22:29:38 +00:00
|
|
|
// if (!this.tput.unicode && ch > '~') {
|
|
|
|
if (!this.tput.unicode && this.tput.numbers.U8 !== 1 && ch > '~') {
|
2013-08-27 13:58:53 +00:00
|
|
|
ch = this.tput.utoa[ch] || '?';
|
|
|
|
}
|
2013-08-10 14:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if (wideChars.test(ch)) x++;
|
|
|
|
|
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-07-23 22:57:04 +00:00
|
|
|
if (out) {
|
2013-08-27 13:58:53 +00:00
|
|
|
main += this.tput.cup(y, 0) + out;
|
2013-06-11 18:13:49 +00:00
|
|
|
}
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
2013-07-31 22:09:41 +00:00
|
|
|
if (acs) {
|
|
|
|
main += this.tput.rmacs();
|
|
|
|
acs = false;
|
|
|
|
}
|
|
|
|
|
2013-07-23 22:57:04 +00:00
|
|
|
if (main) {
|
2013-08-01 08:13:20 +00:00
|
|
|
pre = '';
|
|
|
|
post = '';
|
2013-07-23 22:57:04 +00:00
|
|
|
|
2013-08-27 13:58:53 +00:00
|
|
|
pre += this.tput.sc();
|
|
|
|
post += this.tput.rc();
|
2013-07-23 22:57:04 +00:00
|
|
|
|
|
|
|
if (!this.program.cursorHidden) {
|
2013-08-27 13:58:53 +00:00
|
|
|
pre += this.tput.civis();
|
|
|
|
post += this.tput.cnorm();
|
2013-07-23 22:57:04 +00:00
|
|
|
}
|
2013-07-14 12:02:59 +00:00
|
|
|
|
2013-08-01 06:52:28 +00:00
|
|
|
// this.program.flush();
|
2013-07-31 22:09:41 +00:00
|
|
|
// this.program.output.write(pre + main + post);
|
|
|
|
this.program._write(pre + main + post);
|
2013-07-23 22:57:04 +00:00
|
|
|
}
|
2013-10-16 11:27:03 +00:00
|
|
|
|
|
|
|
// this.emit('draw');
|
2013-07-22 02:00:13 +00:00
|
|
|
};
|
|
|
|
|
2013-06-16 09:21:57 +00:00
|
|
|
Screen.prototype._reduceColor = function(col) {
|
2013-08-27 13:58:53 +00:00
|
|
|
if (col >= 16 && this.tput.colors <= 16) {
|
|
|
|
col = colors.ccolors[col];
|
|
|
|
} else if (col >= 8 && this.tput.colors <= 8) {
|
|
|
|
col -= 8;
|
|
|
|
} else if (col >= 2 && this.tput.colors <= 2) {
|
|
|
|
col %= 2;
|
2013-06-16 09:21:57 +00:00
|
|
|
}
|
|
|
|
return col;
|
|
|
|
};
|
|
|
|
|
2013-07-15 06:44:11 +00:00
|
|
|
// Convert an SGR string to our own attribute format.
|
2013-07-28 15:52:13 +00:00
|
|
|
Screen.prototype.attrCode = function(code, cur, def) {
|
2013-07-15 06:44:11 +00:00
|
|
|
var flags = (cur >> 18) & 0x1ff
|
|
|
|
, fg = (cur >> 9) & 0x1ff
|
|
|
|
, bg = cur & 0x1ff
|
|
|
|
, c
|
|
|
|
, i;
|
|
|
|
|
2013-07-28 15:52:13 +00:00
|
|
|
code = code.slice(2, -1).split(';');
|
2013-07-15 06:44:11 +00:00
|
|
|
if (!code[0]) code[0] = '0';
|
|
|
|
|
|
|
|
for (i = 0; i < code.length; i++) {
|
|
|
|
c = +code[i] || 0;
|
|
|
|
switch (c) {
|
|
|
|
case 0: // normal
|
2013-07-28 15:52:13 +00:00
|
|
|
bg = def & 0x1ff;
|
|
|
|
fg = (def >> 9) & 0x1ff;
|
|
|
|
flags = (def >> 18) & 0x1ff;
|
2013-07-15 06:44:11 +00:00
|
|
|
break;
|
|
|
|
case 1: // bold
|
|
|
|
flags |= 1;
|
|
|
|
break;
|
|
|
|
case 22:
|
2013-07-28 15:52:13 +00:00
|
|
|
flags = (def >> 18) & 0x1ff;
|
2013-07-15 06:44:11 +00:00
|
|
|
break;
|
|
|
|
case 4: // underline
|
|
|
|
flags |= 2;
|
|
|
|
break;
|
|
|
|
case 24:
|
2013-07-28 15:52:13 +00:00
|
|
|
flags = (def >> 18) & 0x1ff;
|
2013-07-15 06:44:11 +00:00
|
|
|
break;
|
|
|
|
case 5: // blink
|
|
|
|
flags |= 4;
|
|
|
|
break;
|
|
|
|
case 25:
|
2013-07-28 15:52:13 +00:00
|
|
|
flags = (def >> 18) & 0x1ff;
|
2013-07-15 06:44:11 +00:00
|
|
|
break;
|
|
|
|
case 7: // inverse
|
|
|
|
flags |= 8;
|
|
|
|
break;
|
|
|
|
case 27:
|
2013-07-28 15:52:13 +00:00
|
|
|
flags = (def >> 18) & 0x1ff;
|
2013-07-15 06:44:11 +00:00
|
|
|
break;
|
|
|
|
case 8: // invisible
|
|
|
|
flags |= 16;
|
|
|
|
break;
|
|
|
|
case 28:
|
2013-07-28 15:52:13 +00:00
|
|
|
flags = (def >> 18) & 0x1ff;
|
|
|
|
break;
|
|
|
|
case 39: // default fg
|
|
|
|
fg = (def >> 9) & 0x1ff;
|
|
|
|
break;
|
|
|
|
case 49: // default bg
|
|
|
|
bg = def & 0x1ff;
|
2013-07-15 06:44:11 +00:00
|
|
|
break;
|
2013-07-22 04:53:10 +00:00
|
|
|
case 100: // default fg/bg
|
2013-07-28 15:52:13 +00:00
|
|
|
fg = (def >> 9) & 0x1ff;
|
|
|
|
bg = def & 0x1ff;
|
2013-07-22 04:53:10 +00:00
|
|
|
break;
|
2013-07-15 06:44:11 +00:00
|
|
|
default: // color
|
2013-07-22 04:53:10 +00:00
|
|
|
if (c === 48 && +code[i+1] === 5) {
|
2013-07-15 06:44:11 +00:00
|
|
|
i += 2;
|
|
|
|
bg = +code[i];
|
|
|
|
break;
|
2013-07-22 04:53:10 +00:00
|
|
|
} else if (c === 48 && +code[i+1] === 2) {
|
|
|
|
i += 2;
|
2013-07-23 10:04:04 +00:00
|
|
|
bg = colors.match(+code[i], +code[i+1], +code[i+2]);
|
2013-07-29 09:19:35 +00:00
|
|
|
if (bg === -1) bg = def & 0x1ff;
|
2013-07-22 04:53:10 +00:00
|
|
|
i += 2;
|
|
|
|
break;
|
|
|
|
} else if (c === 38 && +code[i+1] === 5) {
|
2013-07-15 06:44:11 +00:00
|
|
|
i += 2;
|
|
|
|
fg = +code[i];
|
|
|
|
break;
|
2013-07-22 04:53:10 +00:00
|
|
|
} else if (c === 38 && +code[i+1] === 2) {
|
|
|
|
i += 2;
|
2013-07-23 10:04:04 +00:00
|
|
|
fg = colors.match(+code[i], +code[i+1], +code[i+2]);
|
2013-07-29 09:19:35 +00:00
|
|
|
if (fg === -1) fg = (def >> 9) & 0x1ff;
|
2013-07-22 04:53:10 +00:00
|
|
|
i += 2;
|
|
|
|
break;
|
2013-07-15 06:44:11 +00:00
|
|
|
}
|
|
|
|
if (c >= 40 && c <= 47) {
|
|
|
|
bg = c - 40;
|
|
|
|
} else if (c >= 100 && c <= 107) {
|
|
|
|
bg = c - 100;
|
|
|
|
bg += 8;
|
|
|
|
} else if (c === 49) {
|
2013-07-28 15:52:13 +00:00
|
|
|
bg = def & 0x1ff;
|
2013-07-15 06:44:11 +00:00
|
|
|
} else if (c >= 30 && c <= 37) {
|
|
|
|
fg = c - 30;
|
|
|
|
} else if (c >= 90 && c <= 97) {
|
|
|
|
fg = c - 90;
|
|
|
|
fg += 8;
|
|
|
|
} else if (c === 39) {
|
2013-07-28 15:52:13 +00:00
|
|
|
fg = (def >> 9) & 0x1ff;
|
|
|
|
} else if (c === 100) {
|
|
|
|
fg = (def >> 9) & 0x1ff;
|
|
|
|
bg = def & 0x1ff;
|
2013-07-15 06:44:11 +00:00
|
|
|
}
|
|
|
|
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-07-29 15:22:08 +00:00
|
|
|
Screen.prototype.focusOffset = function(offset) {
|
2013-07-17 05:53:34 +00:00
|
|
|
var shown = this.keyable.filter(function(el) {
|
2013-08-04 09:34:39 +00:00
|
|
|
return !el.detached && el.visible;
|
2013-07-29 15:22:08 +00:00
|
|
|
}).length;
|
|
|
|
|
|
|
|
if (!shown || !offset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
var i = this.keyable.indexOf(this.focused);
|
2013-06-06 09:03:25 +00:00
|
|
|
if (!~i) return;
|
2013-07-29 15:22:08 +00:00
|
|
|
|
2013-06-18 10:08:31 +00:00
|
|
|
if (offset > 0) {
|
|
|
|
while (offset--) {
|
2013-07-17 05:53:34 +00:00
|
|
|
if (++i > this.keyable.length - 1) i = 0;
|
2013-08-04 09:34:39 +00:00
|
|
|
if (this.keyable[i].detached || !this.keyable[i].visible) offset++;
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-06-18 10:08:31 +00:00
|
|
|
offset = -offset;
|
|
|
|
while (offset--) {
|
2013-07-17 05:53:34 +00:00
|
|
|
if (--i < 0) i = this.keyable.length - 1;
|
2013-08-04 09:34:39 +00:00
|
|
|
if (this.keyable[i].detached || !this.keyable[i].visible) offset++;
|
2013-06-18 10:08:31 +00:00
|
|
|
}
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
2013-07-29 15:22:08 +00:00
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
return this.keyable[i].focus();
|
2013-06-06 09:03:25 +00:00
|
|
|
};
|
|
|
|
|
2013-07-29 15:22:08 +00:00
|
|
|
Screen.prototype.focusPrev =
|
|
|
|
Screen.prototype.focusPrevious = function() {
|
|
|
|
return this.focusOffset(-1);
|
2013-06-06 09:03:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.focusNext = function() {
|
2013-07-29 15:22:08 +00:00
|
|
|
return this.focusOffset(1);
|
2013-06-06 09:03:25 +00:00
|
|
|
};
|
|
|
|
|
2013-06-09 20:52:22 +00:00
|
|
|
Screen.prototype.focusPush = function(el) {
|
2013-07-29 15:22:08 +00:00
|
|
|
if (!el) return;
|
|
|
|
var old = this.history[this.history.length-1];
|
2013-06-09 20:52:22 +00:00
|
|
|
if (this.history.length === 10) {
|
|
|
|
this.history.shift();
|
|
|
|
}
|
|
|
|
this.history.push(el);
|
2013-07-29 22:39:50 +00:00
|
|
|
this._focus(el, old);
|
2013-06-09 20:52:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Screen.prototype.focusPop = function() {
|
2013-07-29 15:22:08 +00:00
|
|
|
var old = this.history.pop();
|
2013-07-29 22:39:50 +00:00
|
|
|
if (this.history.length) {
|
|
|
|
this._focus(this.history[this.history.length-1], old);
|
|
|
|
}
|
2013-07-29 15:22:08 +00:00
|
|
|
return old;
|
2013-06-09 20:52:22 +00:00
|
|
|
};
|
|
|
|
|
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-07-29 15:22:08 +00:00
|
|
|
Screen.prototype.rewindFocus = function() {
|
2013-07-29 22:39:50 +00:00
|
|
|
var old = this.history.pop()
|
2013-07-29 15:22:08 +00:00
|
|
|
, el;
|
|
|
|
|
|
|
|
while (this.history.length) {
|
|
|
|
el = this.history.pop();
|
2013-07-29 20:49:25 +00:00
|
|
|
if (!el.detached && el.visible) {
|
2013-07-29 15:22:08 +00:00
|
|
|
this.history.push(el);
|
2013-07-29 22:39:50 +00:00
|
|
|
this._focus(el, old);
|
2013-07-29 15:22:08 +00:00
|
|
|
return el;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old) {
|
|
|
|
old.emit('blur');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-29 22:39:50 +00:00
|
|
|
Screen.prototype._focus = function(self, old) {
|
|
|
|
// Find a scrollable ancestor if we have one.
|
|
|
|
var el = self;
|
|
|
|
while (el = el.parent) {
|
|
|
|
if (el.scrollable) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're in a scrollable element,
|
|
|
|
// automatically scroll to the focused element.
|
|
|
|
if (el) {
|
2015-01-16 18:16:00 +00:00
|
|
|
// NOTE: This is different from the other "visible" values - it needs the
|
|
|
|
// visible height of the scrolling element itself, not the element within
|
|
|
|
// it.
|
2015-01-15 00:24:55 +00:00
|
|
|
var visible = self.screen.height - el.top - el.itop - el.bottom - el.ibottom;
|
|
|
|
if (self.rtop < el.childBase) {
|
2015-01-13 23:04:24 +00:00
|
|
|
el.scrollTo(self.rtop);
|
2015-01-15 00:24:55 +00:00
|
|
|
self.screen.render();
|
|
|
|
} else if (self.rtop + self.height - self.ibottom > el.childBase + visible) {
|
2015-01-16 18:16:00 +00:00
|
|
|
el.scrollTo(self.rtop - (el.height - self.height), true);
|
2015-01-15 00:24:55 +00:00
|
|
|
self.screen.render();
|
|
|
|
}
|
2013-07-29 22:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (old) {
|
|
|
|
old.emit('blur', self);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emit('focus', old);
|
|
|
|
};
|
|
|
|
|
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 = [];
|
|
|
|
}
|
|
|
|
|
2013-07-23 21:37:54 +00:00
|
|
|
var screen = this
|
|
|
|
, program = screen.program
|
|
|
|
, options = options || {}
|
2013-07-04 04:15:09 +00:00
|
|
|
, spawn = require('child_process').spawn
|
2013-07-24 19:17:52 +00:00
|
|
|
, mouse = program.mouseEnabled
|
2013-07-23 21:37:54 +00:00
|
|
|
, ps;
|
2013-07-04 04:15:09 +00:00
|
|
|
|
2015-02-09 07:42:44 +00:00
|
|
|
options.stdio = options.stdio || 'inherit';
|
2013-07-04 04:15:09 +00:00
|
|
|
|
2013-07-23 21:37:54 +00:00
|
|
|
program.lsaveCursor('spawn');
|
2015-01-25 05:33:30 +00:00
|
|
|
// program.csr(0, program.rows - 1);
|
2013-07-23 21:37:54 +00:00
|
|
|
program.normalBuffer();
|
|
|
|
program.showCursor();
|
|
|
|
if (mouse) program.disableMouse();
|
2013-07-04 04:15:09 +00:00
|
|
|
|
2013-07-23 21:37:54 +00:00
|
|
|
var write = program.output.write;
|
|
|
|
program.output.write = function() {};
|
|
|
|
program.input.pause();
|
|
|
|
program.input.setRawMode(false);
|
2013-07-04 04:15:09 +00:00
|
|
|
|
|
|
|
var resume = function() {
|
|
|
|
if (resume.done) return;
|
|
|
|
resume.done = true;
|
|
|
|
|
2013-07-23 21:37:54 +00:00
|
|
|
program.input.setRawMode(true);
|
|
|
|
program.input.resume();
|
|
|
|
program.output.write = write;
|
|
|
|
|
|
|
|
program.alternateBuffer();
|
2015-01-25 05:33:30 +00:00
|
|
|
// program.csr(0, program.rows - 1);
|
2013-07-23 21:37:54 +00:00
|
|
|
if (mouse) program.enableMouse();
|
2013-07-04 04:15:09 +00:00
|
|
|
|
|
|
|
screen.alloc();
|
|
|
|
screen.render();
|
2013-07-23 21:37:54 +00:00
|
|
|
|
|
|
|
screen.program.lrestoreCursor('spawn', true);
|
2013-07-04 04:15:09 +00:00
|
|
|
};
|
|
|
|
|
2013-07-23 21:37:54 +00:00
|
|
|
ps = spawn(file, args, options);
|
2013-07-04 04:15:09 +00:00
|
|
|
|
|
|
|
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) {
|
2015-02-01 21:43:52 +00:00
|
|
|
if (!callback) return;
|
2013-07-04 04:15:09 +00:00
|
|
|
return callback(err, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
ps.on('exit', function(code) {
|
2015-02-01 21:43:52 +00:00
|
|
|
if (!callback) return;
|
2013-07-04 04:15:09 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-09-14 05:01:24 +00:00
|
|
|
if (!callback) {
|
|
|
|
callback = function() {};
|
|
|
|
}
|
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-08-04 03:58:09 +00:00
|
|
|
return writeFile(function(err) {
|
|
|
|
if (err) return callback(err);
|
2013-07-04 05:10:01 +00:00
|
|
|
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() {
|
2013-09-14 05:01:24 +00:00
|
|
|
if (!success) return callback(new Error('Unsuccessful.'));
|
2013-07-04 05:10:01 +00:00
|
|
|
if (err) return callback(err);
|
|
|
|
return callback(null, data);
|
|
|
|
});
|
2013-07-04 04:15:09 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-02-10 03:49:33 +00:00
|
|
|
Screen.prototype.displayImage = function(file, callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(new Error('No image.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
var file = path.resolve(process.cwd(), file);
|
|
|
|
|
|
|
|
if (!~file.indexOf('://')) {
|
|
|
|
file = 'file://' + file;
|
|
|
|
}
|
|
|
|
|
|
|
|
var args = ['w3m', '-T', 'text/html'];
|
|
|
|
|
|
|
|
var input = '<title>press q to exit</title>'
|
|
|
|
+ '<img align="center" src="' + file + '">';
|
|
|
|
|
|
|
|
var opt = {
|
|
|
|
stdio: ['pipe', 1, 2],
|
|
|
|
env: process.env,
|
|
|
|
cwd: process.env.HOME
|
|
|
|
};
|
|
|
|
|
|
|
|
var ps = this.spawn(args[0], args.slice(1), opt);
|
|
|
|
|
|
|
|
ps.on('error', function(err) {
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
ps.on('exit', function(code) {
|
|
|
|
if (!callback) return;
|
|
|
|
if (code !== 0) return callback(new Error('Exit Code: ' + code));
|
|
|
|
return callback(null, code === 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
ps.stdin.write(input + '\n');
|
|
|
|
ps.stdin.end();
|
|
|
|
};
|
|
|
|
|
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() {
|
2013-07-29 09:19:35 +00:00
|
|
|
var element = el();
|
2013-07-12 08:34:32 +00:00
|
|
|
Object.keys(effects).forEach(function(key) {
|
|
|
|
var val = effects[key];
|
|
|
|
if (val !== null && typeof val === 'object') {
|
|
|
|
tmp[key] = tmp[key] || {};
|
2015-01-25 02:34:57 +00:00
|
|
|
// element.style[key] = element.style[key] || {};
|
2013-07-12 08:34:32 +00:00
|
|
|
Object.keys(val).forEach(function(k) {
|
|
|
|
var v = val[k];
|
2013-07-29 09:19:35 +00:00
|
|
|
tmp[key][k] = element.style[key][k];
|
|
|
|
element.style[key][k] = v;
|
2013-07-12 08:34:32 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2013-07-29 09:19:35 +00:00
|
|
|
tmp[key] = element.style[key];
|
|
|
|
element.style[key] = val;
|
2013-07-12 08:34:32 +00:00
|
|
|
});
|
2013-07-29 09:19:35 +00:00
|
|
|
element.screen.render();
|
2013-07-12 08:34:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
fel.on(out, function() {
|
2013-07-29 09:19:35 +00:00
|
|
|
var element = el();
|
2013-07-12 08:34:32 +00:00
|
|
|
Object.keys(effects).forEach(function(key) {
|
|
|
|
var val = effects[key];
|
|
|
|
if (val !== null && typeof val === 'object') {
|
|
|
|
tmp[key] = tmp[key] || {};
|
2015-01-25 02:34:57 +00:00
|
|
|
// element.style[key] = element.style[key] || {};
|
2013-07-12 08:34:32 +00:00
|
|
|
Object.keys(val).forEach(function(k) {
|
2013-07-16 22:50:02 +00:00
|
|
|
if (tmp[key].hasOwnProperty(k)) {
|
2013-07-29 09:19:35 +00:00
|
|
|
element.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)) {
|
2013-07-29 09:19:35 +00:00
|
|
|
element.style[key] = tmp[key];
|
2013-07-12 08:34:32 +00:00
|
|
|
}
|
|
|
|
});
|
2013-07-29 09:19:35 +00:00
|
|
|
element.screen.render();
|
2013-07-12 08:34:32 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-07-14 15:06:28 +00:00
|
|
|
Screen.prototype.sigtstp = function(callback) {
|
|
|
|
var self = this;
|
|
|
|
this.program.sigtstp(function() {
|
|
|
|
self.alloc();
|
|
|
|
self.render();
|
2013-07-24 19:17:52 +00:00
|
|
|
self.program.lrestoreCursor('pause', true);
|
2013-07-14 15:06:28 +00:00
|
|
|
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-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-06-14 20:58:54 +00:00
|
|
|
return new Element(options);
|
|
|
|
}
|
|
|
|
|
2013-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
|
|
|
|
2013-07-24 20:39:22 +00:00
|
|
|
// Workaround to get a `scrollable` option.
|
2013-07-27 10:34:59 +00:00
|
|
|
if (options.scrollable && !this._ignore && this.type !== 'scrollable-box') {
|
2013-07-24 20:39:22 +00:00
|
|
|
Object.getOwnPropertyNames(ScrollableBox.prototype).forEach(function(key) {
|
|
|
|
if (key === 'type') return;
|
|
|
|
Object.defineProperty(this, key,
|
|
|
|
Object.getOwnPropertyDescriptor(ScrollableBox.prototype, key));
|
|
|
|
}, this);
|
|
|
|
this._ignore = true;
|
|
|
|
ScrollableBox.call(this, options);
|
|
|
|
delete this._ignore;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
Node.call(this, options);
|
2013-06-09 13:33:23 +00:00
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
this.name = options.name;
|
|
|
|
|
2013-07-23 11:54:21 +00:00
|
|
|
options.position = options.position || {
|
|
|
|
left: options.left,
|
|
|
|
right: options.right,
|
|
|
|
top: options.top,
|
|
|
|
bottom: options.bottom,
|
|
|
|
width: options.width,
|
|
|
|
height: options.height
|
|
|
|
};
|
|
|
|
|
|
|
|
if (options.position.width === 'shrink'
|
|
|
|
|| options.position.height === 'shrink') {
|
|
|
|
if (options.position.width === 'shrink') {
|
|
|
|
delete options.position.width;
|
|
|
|
}
|
|
|
|
if (options.position.height === 'shrink') {
|
|
|
|
delete options.position.height;
|
|
|
|
}
|
2013-07-17 09:20:22 +00:00
|
|
|
options.shrink = true;
|
|
|
|
}
|
|
|
|
|
2013-07-23 11:54:21 +00:00
|
|
|
this.position = options.position;
|
2013-06-01 02:09:07 +00:00
|
|
|
|
2014-01-12 02:47:40 +00:00
|
|
|
this.noOverflow = options.noOverflow;
|
|
|
|
|
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-07-17 09:20:22 +00:00
|
|
|
this.valign = options.valign || 'top';
|
2013-07-23 19:43:14 +00:00
|
|
|
this.wrap = options.wrap !== false;
|
2013-06-13 09:22:45 +00:00
|
|
|
this.shrink = options.shrink;
|
2013-07-27 10:34:59 +00:00
|
|
|
this.fixed = options.fixed;
|
2013-08-12 14:04:20 +00:00
|
|
|
this.ch = options.ch || ' ';
|
2013-07-19 07:28:31 +00:00
|
|
|
|
|
|
|
if (typeof options.padding === 'number' || !options.padding) {
|
|
|
|
options.padding = {
|
|
|
|
left: options.padding,
|
|
|
|
top: options.padding,
|
|
|
|
right: options.padding,
|
|
|
|
bottom: options.padding
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.padding = {
|
|
|
|
left: options.padding.left || 0,
|
|
|
|
top: options.padding.top || 0,
|
|
|
|
right: options.padding.right || 0,
|
|
|
|
bottom: options.padding.bottom || 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) {
|
2013-07-29 22:22:09 +00:00
|
|
|
if (typeof this.border === 'string') {
|
|
|
|
this.border = { type: this.border };
|
|
|
|
}
|
2013-06-01 07:06:04 +00:00
|
|
|
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 || ' ';
|
2015-02-12 01:21:41 +00:00
|
|
|
this.style.border = this.style.border || this.border.style;
|
|
|
|
if (!this.style.border) {
|
|
|
|
this.style.border = {};
|
|
|
|
this.style.border.fg = this.border.fg;
|
|
|
|
this.style.border.bg = this.border.bg;
|
2013-07-16 22:50:02 +00:00
|
|
|
}
|
2015-02-12 01:21:41 +00:00
|
|
|
//this.border.style = this.style.border;
|
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);
|
|
|
|
}
|
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
if (options.input || options.keyable) {
|
2013-06-06 09:03:25 +00:00
|
|
|
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
|
|
|
|
2013-07-23 21:37:54 +00:00
|
|
|
this.setContent(options.content || '', true);
|
2013-06-09 20:02:12 +00:00
|
|
|
|
|
|
|
if (options.label) {
|
2014-06-02 12:52:40 +00:00
|
|
|
this.setLabel(options.label);
|
2013-06-09 20:02:12 +00:00
|
|
|
}
|
2013-06-11 14:27:50 +00:00
|
|
|
|
2015-01-25 02:34:57 +00:00
|
|
|
if (options.hoverText) {
|
|
|
|
this.setHover(options.hoverText);
|
|
|
|
}
|
|
|
|
|
2013-06-11 14:27:50 +00:00
|
|
|
// TODO: Possibly move this to Node for screen.on('mouse', ...).
|
|
|
|
this.on('newListener', function fn(type) {
|
2015-01-25 05:33:30 +00:00
|
|
|
// type = type.split(' ').slice(1).join(' ');
|
2013-06-11 14:27:50 +00:00
|
|
|
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-08-15 12:14:56 +00:00
|
|
|
this.on('detach', function() {
|
2013-08-18 19:42:39 +00:00
|
|
|
delete self.lpos;
|
2013-08-15 12:14:56 +00:00
|
|
|
});
|
2013-08-13 18:02:02 +00:00
|
|
|
|
2013-07-17 06:37:57 +00:00
|
|
|
if (options.hoverBg != null) {
|
|
|
|
options.hoverEffects = options.hoverEffects || {};
|
|
|
|
options.hoverEffects.bg = options.hoverBg;
|
2013-06-29 17:07:51 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 06:37:57 +00:00
|
|
|
if (this.style.hover) {
|
|
|
|
options.hoverEffects = this.style.hover;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.style.focus) {
|
|
|
|
options.focusEffects = this.style.focus;
|
|
|
|
}
|
|
|
|
|
2015-01-25 05:33:30 +00:00
|
|
|
if (options.effects) {
|
|
|
|
if (options.effects.hover) options.hoverEffects = options.effects.hover;
|
|
|
|
if (options.effects.focus) options.focusEffects = options.effects.focus;
|
|
|
|
}
|
2013-07-17 06:37:57 +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';
|
|
|
|
|
2014-06-11 02:41:22 +00:00
|
|
|
Element.prototype.__defineGetter__('focused', function() {
|
|
|
|
return this.screen.focused === this;
|
|
|
|
});
|
|
|
|
|
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-07-04 06:50:50 +00:00
|
|
|
this.clearPos();
|
2013-07-23 20:26:57 +00:00
|
|
|
this.hidden = true;
|
2013-06-11 18:13:49 +00:00
|
|
|
this.emit('hide');
|
2013-07-29 15:22:08 +00:00
|
|
|
if (this.screen.focused === this) {
|
|
|
|
this.screen.rewindFocus();
|
|
|
|
}
|
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-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() {
|
2013-07-29 15:22:08 +00:00
|
|
|
return this.screen.focused = this;
|
2013-07-21 07:12:16 +00:00
|
|
|
};
|
|
|
|
|
2013-08-04 09:50:13 +00:00
|
|
|
Element.prototype.setContent = function(content, noClear, noTags) {
|
2013-07-23 20:26:57 +00:00
|
|
|
if (!noClear) this.clearPos();
|
2013-06-20 17:07:35 +00:00
|
|
|
this.content = content || '';
|
2013-08-04 09:50:13 +00:00
|
|
|
this.parseContent(noTags);
|
2013-06-09 18:14:42 +00:00
|
|
|
};
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.prototype.getContent = function() {
|
|
|
|
return this._clines.fake.join('\n');
|
|
|
|
};
|
|
|
|
|
2013-08-04 09:50:13 +00:00
|
|
|
Element.prototype.setText = function(content, noClear) {
|
|
|
|
content = content || '';
|
|
|
|
content = content.replace(/\x1b\[[\d;]*m/g, '');
|
|
|
|
return this.setContent(content, noClear, true);
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.getText = function() {
|
|
|
|
return this.getContent().replace(/\x1b\[[\d;]*m/g, '');
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.parseContent = function(noTags) {
|
2013-06-20 17:07:35 +00:00
|
|
|
if (this.detached) return false;
|
|
|
|
|
2013-07-19 07:28:31 +00:00
|
|
|
var width = this.width - this.iwidth;
|
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-08-04 09:50:13 +00:00
|
|
|
|
2013-07-25 02:21:20 +00:00
|
|
|
content = content
|
|
|
|
.replace(/[\x00-\x08\x0b-\x0c\x0e-\x1a\x1c-\x1f\x7f]/g, '')
|
|
|
|
.replace(/\x1b(?!\[[\d;]*m)/g, '')
|
|
|
|
.replace(/\r\n|\r/g, '\n')
|
2013-07-28 15:19:04 +00:00
|
|
|
.replace(/\t/g, this.screen.tabc)
|
2013-08-10 14:30:05 +00:00
|
|
|
.replace(wideChars, '?');
|
2013-08-04 09:50:13 +00:00
|
|
|
|
|
|
|
if (!noTags) {
|
|
|
|
content = this._parseTags(content);
|
|
|
|
}
|
|
|
|
|
2013-07-18 07:37:39 +00:00
|
|
|
this._clines = this._wrapContent(content, width);
|
2013-06-20 17:07:35 +00:00
|
|
|
this._clines.width = width;
|
|
|
|
this._clines.content = this.content;
|
2013-07-19 01:55:16 +00:00
|
|
|
this._clines.attr = this._parseAttr(this._clines);
|
2013-07-25 02:21:20 +00:00
|
|
|
this._clines.ci = [];
|
|
|
|
this._clines.reduce(function(total, line) {
|
|
|
|
this._clines.ci.push(total);
|
|
|
|
return total + line.length + 1;
|
|
|
|
}.bind(this), 0);
|
2013-08-04 09:50:13 +00:00
|
|
|
|
2013-06-20 17:07:35 +00:00
|
|
|
this._pcontent = this._clines.join('\n');
|
|
|
|
this.emit('parsed content');
|
2013-08-04 09:50:13 +00:00
|
|
|
|
2013-06-20 17:07:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-28 15:51:43 +00:00
|
|
|
// Need to calculate this every time because the default fg/bg may change.
|
2013-07-19 01:55:16 +00:00
|
|
|
this._clines.attr = this._parseAttr(this._clines) || this._clines.attr;
|
|
|
|
|
2013-06-20 17:07:35 +00:00
|
|
|
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) {
|
2013-07-28 15:52:13 +00:00
|
|
|
if (!this.parseTags) return text;
|
|
|
|
if (!/{\/?[\w\-,;!#]*}/.test(text)) return text;
|
|
|
|
|
2013-07-28 19:18:28 +00:00
|
|
|
var program = this.screen.program
|
|
|
|
, out = ''
|
2013-07-28 15:52:13 +00:00
|
|
|
, state
|
|
|
|
, bg = []
|
|
|
|
, fg = []
|
|
|
|
, flag = []
|
|
|
|
, cap
|
|
|
|
, slash
|
|
|
|
, param
|
2014-04-26 11:47:41 +00:00
|
|
|
, attr
|
|
|
|
, esc;
|
2013-07-28 15:52:13 +00:00
|
|
|
|
|
|
|
for (;;) {
|
2014-04-26 11:47:41 +00:00
|
|
|
if (!esc && (cap = /^{escape}/.exec(text))) {
|
|
|
|
text = text.substring(cap[0].length);
|
|
|
|
esc = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (esc && (cap = /^([\s\S]+?){\/escape}/.exec(text))) {
|
|
|
|
text = text.substring(cap[0].length);
|
|
|
|
out += cap[1];
|
|
|
|
esc = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (esc) {
|
|
|
|
// throw new Error('Unterminated escape tag.');
|
|
|
|
out += text;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-04-26 05:33:05 +00:00
|
|
|
if (cap = /^{(\/?)([\w\-,;!#]*)}/.exec(text)) {
|
2013-07-28 15:52:13 +00:00
|
|
|
text = text.substring(cap[0].length);
|
|
|
|
slash = cap[1] === '/';
|
|
|
|
param = cap[2].replace(/-/g, ' ');
|
|
|
|
|
2014-04-26 05:22:19 +00:00
|
|
|
if (param === 'open') {
|
|
|
|
out += '{';
|
|
|
|
continue;
|
|
|
|
} else if (param === 'close') {
|
|
|
|
out += '}';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-07-28 15:52:13 +00:00
|
|
|
if (param.slice(-3) === ' bg') state = bg;
|
|
|
|
else if (param.slice(-3) === ' fg') state = fg;
|
|
|
|
else state = flag;
|
|
|
|
|
|
|
|
if (slash) {
|
|
|
|
if (!param) {
|
2013-07-28 19:18:28 +00:00
|
|
|
out += program._attr('normal');
|
2013-07-28 15:52:13 +00:00
|
|
|
bg.length = 0;
|
|
|
|
fg.length = 0;
|
|
|
|
flag.length = 0;
|
|
|
|
} else {
|
2013-07-28 19:18:28 +00:00
|
|
|
attr = program._attr(param, false);
|
2013-07-28 15:52:13 +00:00
|
|
|
if (attr == null) {
|
|
|
|
out += cap[0];
|
|
|
|
} else {
|
2013-07-28 19:18:28 +00:00
|
|
|
// if (param !== state[state.length-1]) {
|
|
|
|
// throw new Error('Misnested tags.');
|
|
|
|
// }
|
2013-07-28 15:52:13 +00:00
|
|
|
state.pop();
|
|
|
|
if (state.length) {
|
2013-07-28 19:18:28 +00:00
|
|
|
out += program._attr(state[state.length-1]);
|
2013-07-28 15:52:13 +00:00
|
|
|
} else {
|
|
|
|
out += attr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!param) {
|
|
|
|
out += cap[0];
|
|
|
|
} else {
|
2013-07-28 19:18:28 +00:00
|
|
|
attr = program._attr(param);
|
2013-07-28 15:52:13 +00:00
|
|
|
if (attr == null) {
|
|
|
|
out += cap[0];
|
|
|
|
} else {
|
|
|
|
state.push(param);
|
|
|
|
out += attr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-14 01:21:41 +00:00
|
|
|
|
2013-07-28 15:52:13 +00:00
|
|
|
continue;
|
2013-06-14 01:21:41 +00:00
|
|
|
}
|
|
|
|
|
2014-04-26 05:33:05 +00:00
|
|
|
if (cap = /^[\s\S]+?(?={\/?[\w\-,;!#]*})/.exec(text)) {
|
2013-07-28 15:52:13 +00:00
|
|
|
text = text.substring(cap[0].length);
|
|
|
|
out += cap[0];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
out += text;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
2013-07-19 01:55:16 +00:00
|
|
|
Element.prototype._parseAttr = function(lines) {
|
2013-07-28 15:52:13 +00:00
|
|
|
var dattr = this.sattr(this.style, this.style.fg, this.style.bg)
|
|
|
|
, attr = dattr
|
2013-07-19 01:55:16 +00:00
|
|
|
, attrs = []
|
|
|
|
, line
|
|
|
|
, i
|
|
|
|
, j
|
|
|
|
, c;
|
|
|
|
|
|
|
|
if (lines[0].attr === attr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < lines.length; j++) {
|
|
|
|
line = lines[j];
|
|
|
|
attrs[j] = attr;
|
|
|
|
for (i = 0; i < line.length; i++) {
|
|
|
|
if (line[i] === '\x1b') {
|
|
|
|
if (c = /^\x1b\[[\d;]*m/.exec(line.substring(i))) {
|
2013-07-28 15:52:13 +00:00
|
|
|
attr = this.screen.attrCode(c[0], attr, dattr);
|
2013-07-19 01:55:16 +00:00
|
|
|
i += c[0].length - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return attrs;
|
|
|
|
};
|
|
|
|
|
2013-07-18 07:37:39 +00:00
|
|
|
Element.prototype._align = function(line, width, align) {
|
2013-07-18 07:37:18 +00:00
|
|
|
if (!align) return line;
|
|
|
|
|
2014-06-26 17:46:41 +00:00
|
|
|
var cline = line.replace(/\x1b\[[\d;]*m/g, '')
|
|
|
|
, len = cline.length
|
2013-07-18 07:37:18 +00:00
|
|
|
, 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;
|
2014-06-17 03:41:28 +00:00
|
|
|
} else if (this.parseTags && ~line.indexOf('{|}')) {
|
|
|
|
var parts = line.split('{|}');
|
2014-06-26 17:46:41 +00:00
|
|
|
var cparts = cline.split('{|}');
|
|
|
|
s = Math.max(width - cparts[0].length - cparts[1].length, 0);
|
2014-06-17 03:41:28 +00:00
|
|
|
s = Array(s + 1).join(' ');
|
|
|
|
return parts[0] + s + parts[1];
|
2013-07-18 07:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return line;
|
2013-07-18 07:37:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype._wrapContent = function(content, width) {
|
2013-07-28 14:58:42 +00:00
|
|
|
var tags = this.parseTags
|
2013-07-18 07:37:39 +00:00
|
|
|
, state = this.align
|
2013-07-23 19:43:14 +00:00
|
|
|
, wrap = this.wrap
|
2013-07-23 18:31:05 +00:00
|
|
|
, margin = 0
|
|
|
|
, rtof = []
|
|
|
|
, ftor = []
|
2013-07-23 20:26:57 +00:00
|
|
|
, fake = []
|
|
|
|
, out = []
|
|
|
|
, no = 0
|
|
|
|
, line
|
|
|
|
, align
|
|
|
|
, cap
|
|
|
|
, total
|
|
|
|
, i
|
|
|
|
, part
|
|
|
|
, j
|
2014-04-26 10:06:19 +00:00
|
|
|
, lines
|
|
|
|
, rest;
|
2013-07-18 07:37:18 +00:00
|
|
|
|
2013-07-23 20:26:57 +00:00
|
|
|
lines = content.split('\n');
|
2013-07-18 07:37:18 +00:00
|
|
|
|
|
|
|
if (!content) {
|
2013-07-18 07:37:39 +00:00
|
|
|
out.push(content);
|
2013-07-23 19:10:46 +00:00
|
|
|
out.rtof = [0];
|
|
|
|
out.ftor = [[0]];
|
|
|
|
out.fake = lines;
|
2013-07-23 19:34:29 +00:00
|
|
|
out.real = out;
|
2013-07-29 21:49:44 +00:00
|
|
|
out.mwidth = 0;
|
2013-07-18 07:37:18 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2013-07-23 19:43:14 +00:00
|
|
|
if (this.scrollbar) margin++;
|
|
|
|
if (this.type === 'textarea') margin++;
|
2013-07-18 07:37:39 +00:00
|
|
|
if (width > margin) width -= margin;
|
2013-07-18 07:37:18 +00:00
|
|
|
|
2013-07-28 14:24:08 +00:00
|
|
|
main:
|
2013-07-23 20:26:57 +00:00
|
|
|
for (; no < lines.length; no++) {
|
|
|
|
line = lines[no];
|
|
|
|
align = state;
|
2013-07-18 07:37:18 +00:00
|
|
|
|
2013-07-23 18:31:05 +00:00
|
|
|
ftor.push([]);
|
|
|
|
|
2013-07-28 14:43:28 +00:00
|
|
|
// Handle alignment tags.
|
2013-07-18 07:37:18 +00:00
|
|
|
if (tags) {
|
|
|
|
if (cap = /^{(left|center|right)}/.exec(line)) {
|
|
|
|
line = line.substring(cap[0].length);
|
|
|
|
align = state = cap[1] !== 'left'
|
|
|
|
? cap[1]
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
if (cap = /{\/(left|center|right)}$/.exec(line)) {
|
|
|
|
line = line.slice(0, -cap[0].length);
|
|
|
|
state = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-28 14:43:28 +00:00
|
|
|
// If the string is apparently too long, wrap it.
|
2013-07-18 07:37:18 +00:00
|
|
|
while (line.length > width) {
|
2013-07-28 14:43:28 +00:00
|
|
|
// Measure the real width of the string.
|
2013-07-18 07:37:18 +00:00
|
|
|
for (i = 0, total = 0; i < line.length; i++) {
|
|
|
|
while (line[i] === '\x1b') {
|
|
|
|
while (line[i] && line[i++] !== 'm');
|
|
|
|
}
|
|
|
|
if (!line[i]) break;
|
|
|
|
if (++total === width) {
|
2014-04-26 10:06:19 +00:00
|
|
|
// If we're not wrapping the text, we have to finish up the rest of
|
|
|
|
// the control sequences before cutting off the line.
|
2014-04-26 13:05:49 +00:00
|
|
|
i++;
|
2014-04-26 10:06:19 +00:00
|
|
|
if (!wrap) {
|
|
|
|
rest = line.substring(i).match(/\x1b\[[^m]*m/g);
|
|
|
|
rest = rest ? rest.join('') : '';
|
|
|
|
out.push(this._align(line.substring(0, i) + rest, width, align));
|
|
|
|
ftor[no].push(out.length - 1);
|
|
|
|
rtof.push(no);
|
|
|
|
continue main;
|
|
|
|
}
|
2013-07-28 14:43:28 +00:00
|
|
|
// Try to find a space to break on.
|
2014-04-26 13:05:49 +00:00
|
|
|
if (i !== line.length) {
|
2013-07-23 20:26:57 +00:00
|
|
|
j = i;
|
2014-04-26 13:05:49 +00:00
|
|
|
while (j > i - 10 && j > 0 && line[--j] !== ' ');
|
2013-07-18 07:37:18 +00:00
|
|
|
if (line[j] === ' ') i = j + 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-28 14:43:28 +00:00
|
|
|
part = line.substring(0, i);
|
|
|
|
line = line.substring(i);
|
2013-07-18 07:37:18 +00:00
|
|
|
|
2013-07-28 14:58:42 +00:00
|
|
|
out.push(this._align(part, width, align));
|
2013-07-28 14:24:08 +00:00
|
|
|
ftor[no].push(out.length - 1);
|
|
|
|
rtof.push(no);
|
|
|
|
|
2013-07-18 07:37:18 +00:00
|
|
|
// Make sure we didn't wrap the line to the very end, otherwise
|
|
|
|
// we get a pointless empty line after a newline.
|
2013-07-28 14:24:08 +00:00
|
|
|
if (line === '') continue main;
|
2013-07-18 07:37:18 +00:00
|
|
|
|
2013-07-28 14:24:08 +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;
|
|
|
|
continue main;
|
|
|
|
}
|
2013-07-18 07:37:18 +00:00
|
|
|
}
|
|
|
|
|
2013-07-28 14:58:42 +00:00
|
|
|
out.push(this._align(line, width, align));
|
2013-07-23 18:31:05 +00:00
|
|
|
ftor[no].push(out.length - 1);
|
|
|
|
rtof.push(no);
|
2013-07-23 20:26:57 +00:00
|
|
|
}
|
2013-07-18 07:37:18 +00:00
|
|
|
|
2013-07-23 18:31:05 +00:00
|
|
|
out.rtof = rtof;
|
|
|
|
out.ftor = ftor;
|
|
|
|
out.fake = lines;
|
2013-07-23 19:34:29 +00:00
|
|
|
out.real = out;
|
2013-07-28 15:51:43 +00:00
|
|
|
|
|
|
|
out.mwidth = out.reduce(function(current, line) {
|
|
|
|
line = line.replace(/\x1b\[[\d;]*m/g, '');
|
|
|
|
return line.length > current
|
|
|
|
? line.length
|
|
|
|
: current;
|
|
|
|
}, 0);
|
2013-07-23 18:31:05 +00:00
|
|
|
|
2013-07-18 07:37:18 +00:00
|
|
|
return out;
|
2013-07-18 07:37:39 +00:00
|
|
|
};
|
2013-07-18 07:37:18 +00:00
|
|
|
|
2013-06-18 10:08:31 +00:00
|
|
|
Element.prototype.__defineGetter__('visible', function() {
|
|
|
|
var el = this;
|
|
|
|
do {
|
2014-05-16 23:51:33 +00:00
|
|
|
if (el.detached) return false;
|
2013-06-18 10:08:31 +00:00
|
|
|
if (el.hidden) return false;
|
|
|
|
} while (el = el.parent);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2013-07-29 15:22:08 +00:00
|
|
|
Element.prototype.__defineGetter__('_detached', function() {
|
2013-06-20 11:43:56 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2013-08-13 16:44:04 +00:00
|
|
|
Element.prototype.setIndex = function(index) {
|
|
|
|
if (!this.parent) return;
|
|
|
|
|
|
|
|
if (index < 0) {
|
|
|
|
index = this.parent.children.length + index;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = Math.max(index, 0);
|
|
|
|
index = Math.min(index, this.parent.children.length - 1);
|
|
|
|
|
|
|
|
var i = this.parent.children.indexOf(this);
|
|
|
|
if (!~i) return;
|
|
|
|
|
2014-03-14 03:07:50 +00:00
|
|
|
var item = this.parent.children.splice(i, 1)[0]
|
|
|
|
this.parent.children.splice(index, 0, item);
|
2013-08-13 16:44:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.setFront = function() {
|
|
|
|
return this.setIndex(-1);
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.setBack = function() {
|
|
|
|
return this.setIndex(0);
|
|
|
|
};
|
|
|
|
|
2013-08-15 12:14:56 +00:00
|
|
|
Element.prototype.clearPos = function(get) {
|
2013-07-21 10:31:24 +00:00
|
|
|
if (this.detached) return;
|
2013-08-15 12:14:56 +00:00
|
|
|
var lpos = this._getCoords(get);
|
2013-07-21 10:31:24 +00:00
|
|
|
if (!lpos) return;
|
|
|
|
this.screen.clearRegion(
|
|
|
|
lpos.xi, lpos.xl,
|
|
|
|
lpos.yi, lpos.yl);
|
2013-07-04 06:50:50 +00:00
|
|
|
};
|
|
|
|
|
2015-01-25 02:34:57 +00:00
|
|
|
Element.prototype.setLabel = function(options) {
|
2015-01-16 19:13:12 +00:00
|
|
|
var self = this;
|
|
|
|
|
2015-01-25 02:34:57 +00:00
|
|
|
if (typeof options === 'string') {
|
|
|
|
options = { text: options };
|
|
|
|
}
|
|
|
|
|
2014-06-02 12:52:40 +00:00
|
|
|
if (this._label) {
|
2015-01-25 02:34:57 +00:00
|
|
|
this._label.setContent(options.text);
|
|
|
|
if (options.side !== 'right') {
|
|
|
|
this._label.rleft = 2 + (this.border ? -1 : 0);
|
|
|
|
this._label.position.right = undefined;
|
|
|
|
if (!this.screen.autoPadding) {
|
|
|
|
this._label.rleft = 2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._label.rright = 2 + (this.border ? -1 : 0);
|
|
|
|
this._label.position.left = undefined;
|
|
|
|
if (!this.screen.autoPadding) {
|
|
|
|
this._label.rright = 2;
|
|
|
|
}
|
|
|
|
}
|
2014-06-02 12:52:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-01-16 19:13:12 +00:00
|
|
|
|
2014-06-02 12:52:40 +00:00
|
|
|
this._label = new Box({
|
|
|
|
screen: this.screen,
|
|
|
|
parent: this,
|
2015-01-25 02:34:57 +00:00
|
|
|
content: options.text,
|
2015-01-16 18:55:24 +00:00
|
|
|
top: this.border ? -1 : 0,
|
2014-06-02 12:52:40 +00:00
|
|
|
tags: this.parseTags,
|
|
|
|
shrink: true,
|
2015-01-13 23:04:24 +00:00
|
|
|
style: this.style.label
|
2014-06-02 12:52:40 +00:00
|
|
|
});
|
2015-01-16 19:13:12 +00:00
|
|
|
|
2015-01-25 02:34:57 +00:00
|
|
|
if (options.side !== 'right') {
|
|
|
|
this._label.rleft = 2 + (this.border ? -1 : 0);
|
|
|
|
} else {
|
|
|
|
this._label.rright = 2 + (this.border ? -1 : 0);
|
|
|
|
}
|
|
|
|
|
2015-01-13 23:04:24 +00:00
|
|
|
this._label._isLabel = true;
|
2015-01-16 19:13:12 +00:00
|
|
|
|
2015-01-16 18:55:24 +00:00
|
|
|
if (!this.screen.autoPadding) {
|
2015-01-25 02:34:57 +00:00
|
|
|
if (options.side !== 'right') {
|
|
|
|
this._label.rleft = 2;
|
|
|
|
} else {
|
|
|
|
this._label.rright = 2;
|
|
|
|
}
|
2015-01-16 18:55:24 +00:00
|
|
|
this._label.rtop = 0;
|
2014-06-02 12:52:40 +00:00
|
|
|
}
|
2015-01-16 19:13:12 +00:00
|
|
|
|
|
|
|
this.on('scroll', function() {
|
|
|
|
var visible = self.height - self.iheight;
|
|
|
|
self._label.rtop = self.childBase - (self.border ? 1 : 0);
|
|
|
|
if (!self.screen.autoPadding) {
|
|
|
|
self._label.rtop = self.childBase;
|
|
|
|
}
|
|
|
|
self.screen.render();
|
|
|
|
});
|
2014-06-02 12:52:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.removeLabel = function() {
|
2015-01-13 23:04:24 +00:00
|
|
|
this._label.detach();
|
|
|
|
delete this._label;
|
2014-06-02 12:52:40 +00:00
|
|
|
};
|
|
|
|
|
2015-01-25 02:34:57 +00:00
|
|
|
Element.prototype.setHover = function(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (typeof options === 'string') {
|
|
|
|
options = { text: options };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._hover) {
|
|
|
|
this._hover.setContent(options.text);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._hover = new Box({
|
|
|
|
screen: this.screen,
|
|
|
|
content: options.text,
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
tags: this.parseTags,
|
|
|
|
height: 'shrink',
|
|
|
|
width: 'shrink',
|
|
|
|
border: 'line',
|
|
|
|
style: {
|
|
|
|
border: {
|
|
|
|
fg: 'default'
|
|
|
|
},
|
|
|
|
bg: 'default',
|
|
|
|
fg: 'default'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this._hover._isHover = true;
|
|
|
|
this._hover._.over = false;
|
|
|
|
|
|
|
|
this.on('mouseover', function(data) {
|
|
|
|
self._hover._.over = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on('mouse', function(data) {
|
|
|
|
if (!self._hover._.over) return;
|
|
|
|
|
|
|
|
var el = self._hover
|
|
|
|
, x = data.x
|
|
|
|
, y = data.y;
|
|
|
|
|
|
|
|
self.screen.append(el);
|
|
|
|
|
|
|
|
while (el = el.parent) {
|
|
|
|
x -= el.rleft;
|
|
|
|
y -= el.rtop;
|
|
|
|
}
|
|
|
|
|
|
|
|
self._hover.rleft = x;
|
|
|
|
self._hover.rtop = y;
|
|
|
|
|
|
|
|
self.screen.render();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on('mouseout', function() {
|
|
|
|
self._hover._.over = false;
|
|
|
|
self._hover.detach();
|
|
|
|
self.screen.render();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.removeHover = function() {
|
|
|
|
this._hover.detach();
|
|
|
|
delete this._hover;
|
|
|
|
};
|
|
|
|
|
2013-06-11 15:07:04 +00:00
|
|
|
/**
|
|
|
|
* Positioning
|
|
|
|
*/
|
|
|
|
|
2013-07-19 01:55:16 +00:00
|
|
|
// The below methods are a bit confusing: basically
|
|
|
|
// whenever Box.render is called `lpos` gets set on
|
|
|
|
// the element, an object containing the rendered
|
|
|
|
// coordinates. Since these don't update if the
|
|
|
|
// element is moved somehow, they're unreliable in
|
|
|
|
// that situation. However, if we can guarantee that
|
|
|
|
// lpos is good and up to date, it can be more
|
|
|
|
// accurate than the calculated positions below.
|
|
|
|
// In this case, if the element is being rendered,
|
|
|
|
// it's guaranteed that the parent will have been
|
|
|
|
// rendered first, in which case we can use the
|
|
|
|
// parant's lpos instead of recalculating it's
|
|
|
|
// position (since that might be wrong because
|
|
|
|
// it doesn't handle content shrinkage).
|
|
|
|
|
|
|
|
Screen.prototype._getPos = function() {
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype._getPos = function() {
|
2013-07-17 17:43:13 +00:00
|
|
|
var pos = this.lpos;
|
2013-07-19 01:55:16 +00:00
|
|
|
|
2013-07-30 13:50:49 +00:00
|
|
|
assert.ok(pos);
|
2013-07-19 01:55:16 +00:00
|
|
|
|
|
|
|
if (pos.left != null) return pos;
|
|
|
|
|
2013-07-17 17:43:13 +00:00
|
|
|
pos.left = pos.xi;
|
|
|
|
pos.top = pos.yi;
|
|
|
|
pos.right = this.screen.cols - pos.xl;
|
|
|
|
pos.bottom = this.screen.rows - pos.yl;
|
2013-07-19 01:55:16 +00:00
|
|
|
pos.width = pos.xl - pos.xi;
|
|
|
|
pos.height = pos.yl - pos.yi;
|
|
|
|
|
2013-07-17 17:43:13 +00:00
|
|
|
return pos;
|
|
|
|
};
|
|
|
|
|
2013-07-19 01:55:16 +00:00
|
|
|
/**
|
|
|
|
* Position Getters
|
|
|
|
*/
|
2013-06-01 02:39:11 +00:00
|
|
|
|
2013-07-19 01:55:16 +00:00
|
|
|
Element.prototype._getWidth = function(get) {
|
2013-07-31 04:57:48 +00:00
|
|
|
var parent = get ? this.parent._getPos() : this.parent
|
|
|
|
, width = this.position.width || 0
|
|
|
|
, left;
|
2013-07-19 01:55:16 +00:00
|
|
|
|
2013-06-01 05:29:31 +00:00
|
|
|
if (typeof width === 'string') {
|
|
|
|
if (width === 'half') width = '50%';
|
|
|
|
width = +width.slice(0, -1) / 100;
|
2013-07-19 01:55:16 +00:00
|
|
|
return parent.width * width | 0;
|
2013-06-01 05:29:31 +00:00
|
|
|
}
|
2013-07-19 01:55:16 +00:00
|
|
|
|
|
|
|
// This is for if the element is being streched or shrunken.
|
|
|
|
// Although the width for shrunken elements is calculated
|
|
|
|
// in the render function, it may be calculated based on
|
|
|
|
// the content width, and the content width is initially
|
|
|
|
// decided by the width the element, so it needs to be
|
|
|
|
// calculated here.
|
2013-06-01 05:29:31 +00:00
|
|
|
if (!width) {
|
2013-07-31 04:57:48 +00:00
|
|
|
left = this.position.left || 0;
|
2013-06-01 10:47:18 +00:00
|
|
|
if (typeof left === 'string') {
|
|
|
|
if (left === 'center') left = '50%';
|
|
|
|
left = +left.slice(0, -1) / 100;
|
2013-07-19 01:55:16 +00:00
|
|
|
left = parent.width * left | 0;
|
2013-06-01 10:47:18 +00:00
|
|
|
}
|
2013-07-23 11:54:21 +00:00
|
|
|
width = parent.width - (this.position.right || 0) - left;
|
2013-07-30 13:50:49 +00:00
|
|
|
if (this.screen.autoPadding) {
|
2013-07-31 04:57:48 +00:00
|
|
|
if ((this.position.left != null || this.position.right == null)
|
2013-08-04 03:58:09 +00:00
|
|
|
&& this.position.left !== 'center') {
|
|
|
|
width -= this.parent.ileft;
|
2013-07-31 04:57:48 +00:00
|
|
|
}
|
2015-01-25 05:33:30 +00:00
|
|
|
// if (this.position.right != null) {
|
|
|
|
width -= this.parent.iright;
|
|
|
|
// }
|
2013-07-30 13:50:49 +00:00
|
|
|
}
|
2013-06-01 05:29:31 +00:00
|
|
|
}
|
2013-07-19 01:55:16 +00:00
|
|
|
|
2013-06-01 02:39:11 +00:00
|
|
|
return width;
|
2013-07-19 01:55:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('width', function() {
|
|
|
|
return this._getWidth(false);
|
2013-06-01 02:39:11 +00:00
|
|
|
});
|
|
|
|
|
2013-07-19 01:55:16 +00:00
|
|
|
Element.prototype._getHeight = function(get) {
|
2013-07-31 04:57:48 +00:00
|
|
|
var parent = get ? this.parent._getPos() : this.parent
|
|
|
|
, height = this.position.height || 0
|
|
|
|
, top;
|
2013-07-19 01:55:16 +00:00
|
|
|
|
2013-06-01 05:29:31 +00:00
|
|
|
if (typeof height === 'string') {
|
|
|
|
if (height === 'half') height = '50%';
|
|
|
|
height = +height.slice(0, -1) / 100;
|
2013-07-19 01:55:16 +00:00
|
|
|
return parent.height * height | 0;
|
2013-06-01 05:29:31 +00:00
|
|
|
}
|
2013-07-19 01:55:16 +00:00
|
|
|
|
|
|
|
// This is for if the element is being streched or shrunken.
|
|
|
|
// Although the width for shrunken elements is calculated
|
|
|
|
// in the render function, it may be calculated based on
|
|
|
|
// the content width, and the content width is initially
|
|
|
|
// decided by the width the element, so it needs to be
|
|
|
|
// calculated here.
|
2013-06-01 05:29:31 +00:00
|
|
|
if (!height) {
|
2013-07-31 04:57:48 +00:00
|
|
|
top = this.position.top || 0;
|
2013-06-01 10:47:18 +00:00
|
|
|
if (typeof top === 'string') {
|
|
|
|
if (top === 'center') top = '50%';
|
|
|
|
top = +top.slice(0, -1) / 100;
|
2013-07-19 01:55:16 +00:00
|
|
|
top = parent.height * top | 0;
|
2013-06-01 10:47:18 +00:00
|
|
|
}
|
2013-07-23 11:54:21 +00:00
|
|
|
height = parent.height - (this.position.bottom || 0) - top;
|
2013-07-30 13:50:49 +00:00
|
|
|
if (this.screen.autoPadding) {
|
2013-07-31 04:57:48 +00:00
|
|
|
if ((this.position.top != null
|
|
|
|
|| this.position.bottom == null)
|
|
|
|
&& this.position.top !== 'center') {
|
|
|
|
height -= this.parent.itop;
|
|
|
|
}
|
2015-01-25 05:33:30 +00:00
|
|
|
// if (this.position.bottom != null) {
|
|
|
|
height -= this.parent.ibottom;
|
|
|
|
// }
|
2013-07-30 13:50:49 +00:00
|
|
|
}
|
2013-06-01 05:29:31 +00:00
|
|
|
}
|
2013-07-19 01:55:16 +00:00
|
|
|
|
2013-06-01 02:39:11 +00:00
|
|
|
return height;
|
2013-07-19 01:55:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('height', function() {
|
|
|
|
return this._getHeight(false);
|
2013-06-01 02:09:07 +00:00
|
|
|
});
|
|
|
|
|
2013-07-19 01:55:16 +00:00
|
|
|
Element.prototype._getLeft = function(get) {
|
2013-07-31 04:57:48 +00:00
|
|
|
var parent = get ? this.parent._getPos() : this.parent
|
|
|
|
, left = this.position.left || 0;
|
2013-06-01 10:47:18 +00:00
|
|
|
|
|
|
|
if (typeof left === 'string') {
|
|
|
|
if (left === 'center') left = '50%';
|
|
|
|
left = +left.slice(0, -1) / 100;
|
2013-07-19 01:55:16 +00:00
|
|
|
left = parent.width * left | 0;
|
2013-06-11 15:07:04 +00:00
|
|
|
if (this.position.left === 'center') {
|
2013-07-19 01:55:16 +00:00
|
|
|
left -= this._getWidth(get) / 2 | 0;
|
2013-06-11 15:07:04 +00:00
|
|
|
}
|
2013-06-01 10:47:18 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 11:54:21 +00:00
|
|
|
if (this.position.left == null && this.position.right != null) {
|
2013-07-19 01:55:16 +00:00
|
|
|
return this.screen.cols - this._getWidth(get) - this._getRight(get);
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
|
2013-07-30 13:50:49 +00:00
|
|
|
if (this.screen.autoPadding) {
|
2013-07-31 04:57:48 +00:00
|
|
|
if ((this.position.left != null
|
|
|
|
|| this.position.right == null)
|
2013-07-30 13:50:49 +00:00
|
|
|
&& this.position.left !== 'center') {
|
|
|
|
left += this.parent.ileft;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 01:55:16 +00:00
|
|
|
return (parent.left || 0) + left;
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('left', function() {
|
|
|
|
return this._getLeft(false);
|
2013-06-01 10:47:18 +00:00
|
|
|
});
|
|
|
|
|
2013-07-19 01:55:16 +00:00
|
|
|
Element.prototype._getRight = function(get) {
|
2013-07-31 04:57:48 +00:00
|
|
|
var parent = get ? this.parent._getPos() : this.parent
|
|
|
|
, right;
|
|
|
|
|
2013-07-23 11:54:21 +00:00
|
|
|
if (this.position.right == null && this.position.left != null) {
|
2013-07-31 04:57:48 +00:00
|
|
|
right = this.screen.cols - (this._getLeft(get) + this._getWidth(get));
|
2013-07-30 13:50:49 +00:00
|
|
|
if (this.screen.autoPadding) {
|
2015-01-25 05:33:30 +00:00
|
|
|
// if (this.position.right != null) {
|
|
|
|
right += this.parent.iright;
|
|
|
|
// }
|
2013-07-30 13:50:49 +00:00
|
|
|
}
|
|
|
|
return right;
|
|
|
|
}
|
2013-07-31 04:57:48 +00:00
|
|
|
|
|
|
|
right = (parent.right || 0) + (this.position.right || 0);
|
|
|
|
|
2013-07-30 13:50:49 +00:00
|
|
|
if (this.screen.autoPadding) {
|
2015-01-25 05:33:30 +00:00
|
|
|
// if (this.position.right != null) {
|
|
|
|
right += this.parent.iright;
|
|
|
|
// }
|
2013-06-11 15:07:04 +00:00
|
|
|
}
|
2013-07-31 04:57:48 +00:00
|
|
|
|
2013-07-30 13:50:49 +00:00
|
|
|
return right;
|
2013-07-19 01:55:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('right', function() {
|
|
|
|
return this._getRight(false);
|
2013-06-01 10:47:18 +00:00
|
|
|
});
|
|
|
|
|
2013-07-19 01:55:16 +00:00
|
|
|
Element.prototype._getTop = function(get) {
|
2013-07-31 04:57:48 +00:00
|
|
|
var parent = get ? this.parent._getPos() : this.parent
|
|
|
|
, top = this.position.top || 0;
|
2013-06-01 10:47:18 +00:00
|
|
|
|
|
|
|
if (typeof top === 'string') {
|
|
|
|
if (top === 'center') top = '50%';
|
|
|
|
top = +top.slice(0, -1) / 100;
|
2013-07-19 01:55:16 +00:00
|
|
|
top = parent.height * top | 0;
|
2013-06-11 15:07:04 +00:00
|
|
|
if (this.position.top === 'center') {
|
2013-07-19 01:55:16 +00:00
|
|
|
top -= this._getHeight(get) / 2 | 0;
|
2013-06-11 15:07:04 +00:00
|
|
|
}
|
2013-06-01 10:47:18 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 11:54:21 +00:00
|
|
|
if (this.position.top == null && this.position.bottom != null) {
|
2013-07-19 01:55:16 +00:00
|
|
|
return this.screen.rows - this._getHeight(get) - this._getBottom(get);
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
|
2013-07-30 13:50:49 +00:00
|
|
|
if (this.screen.autoPadding) {
|
2013-07-31 04:57:48 +00:00
|
|
|
if ((this.position.top != null
|
|
|
|
|| this.position.bottom == null)
|
2013-07-30 13:50:49 +00:00
|
|
|
&& this.position.top !== 'center') {
|
|
|
|
top += this.parent.itop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-19 01:55:16 +00:00
|
|
|
return (parent.top || 0) + top;
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('top', function() {
|
|
|
|
return this._getTop(false);
|
2013-06-01 10:47:18 +00:00
|
|
|
});
|
|
|
|
|
2013-07-19 01:55:16 +00:00
|
|
|
Element.prototype._getBottom = function(get) {
|
2013-07-31 04:57:48 +00:00
|
|
|
var parent = get ? this.parent._getPos() : this.parent
|
|
|
|
, bottom;
|
|
|
|
|
2013-07-23 11:54:21 +00:00
|
|
|
if (this.position.bottom == null && this.position.top != null) {
|
2013-07-31 04:57:48 +00:00
|
|
|
bottom = this.screen.rows - (this._getTop(get) + this._getHeight(get));
|
2013-07-30 13:50:49 +00:00
|
|
|
if (this.screen.autoPadding) {
|
2015-01-25 05:33:30 +00:00
|
|
|
// if (this.position.bottom != null) {
|
|
|
|
bottom += this.parent.ibottom;
|
|
|
|
// }
|
2013-07-30 13:50:49 +00:00
|
|
|
}
|
|
|
|
return bottom;
|
|
|
|
}
|
2013-07-31 04:57:48 +00:00
|
|
|
|
|
|
|
bottom = (parent.bottom || 0) + (this.position.bottom || 0);
|
|
|
|
|
2013-07-30 13:50:49 +00:00
|
|
|
if (this.screen.autoPadding) {
|
2015-01-25 05:33:30 +00:00
|
|
|
// if (this.position.bottom != null) {
|
|
|
|
bottom += this.parent.ibottom;
|
|
|
|
// }
|
2013-06-11 15:07:04 +00:00
|
|
|
}
|
2013-07-31 04:57:48 +00:00
|
|
|
|
2013-07-30 13:50:49 +00:00
|
|
|
return bottom;
|
2013-07-19 01:55:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('bottom', function() {
|
|
|
|
return this._getBottom(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('rleft', function() {
|
|
|
|
return this.left - this.parent.left;
|
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('rright', function() {
|
|
|
|
return this.right - this.parent.right;
|
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('rtop', function() {
|
|
|
|
return this.top - this.parent.top;
|
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('rbottom', function() {
|
|
|
|
return this.bottom - this.parent.bottom;
|
2013-06-01 10:47:18 +00:00
|
|
|
});
|
|
|
|
|
2013-07-19 01:55:16 +00:00
|
|
|
/**
|
|
|
|
* Position Setters
|
|
|
|
*/
|
|
|
|
|
|
|
|
// NOTE:
|
|
|
|
// For right, bottom, rright, and rbottom:
|
|
|
|
// If position.bottom is null, we could simply set top instead.
|
|
|
|
// But it wouldn't replicate bottom behavior appropriately if
|
|
|
|
// the parent was resized, etc.
|
|
|
|
Element.prototype.__defineSetter__('width', function(val) {
|
|
|
|
if (this.position.width === val) return;
|
|
|
|
this.emit('resize');
|
|
|
|
this.clearPos();
|
2013-07-23 11:54:21 +00:00
|
|
|
return this.position.width = val;
|
2013-07-19 01:55:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineSetter__('height', function(val) {
|
|
|
|
if (this.position.height === val) return;
|
|
|
|
this.emit('resize');
|
|
|
|
this.clearPos();
|
2013-07-23 11:54:21 +00:00
|
|
|
return this.position.height = val;
|
2013-07-19 01:55:16 +00:00
|
|
|
});
|
2013-06-11 14:09:57 +00:00
|
|
|
|
|
|
|
Element.prototype.__defineSetter__('left', function(val) {
|
2013-06-11 15:07:04 +00:00
|
|
|
if (typeof val === 'string') {
|
2013-07-19 01:55:16 +00:00
|
|
|
if (val === 'center') {
|
|
|
|
val = this.screen.width / 2 | 0;
|
|
|
|
val -= this.width / 2 | 0;
|
|
|
|
} else {
|
|
|
|
val = +val.slice(0, -1) / 100;
|
|
|
|
val = this.screen.width * val | 0;
|
|
|
|
}
|
2013-06-11 15:07:04 +00:00
|
|
|
}
|
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-07-23 11:54:21 +00:00
|
|
|
return this.position.left = val;
|
2013-06-11 14:09:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineSetter__('right', function(val) {
|
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-07-23 11:54:21 +00:00
|
|
|
return 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') {
|
2013-07-19 01:55:16 +00:00
|
|
|
if (val === 'center') {
|
|
|
|
val = this.screen.height / 2 | 0;
|
|
|
|
val -= this.height / 2 | 0;
|
|
|
|
} else {
|
|
|
|
val = +val.slice(0, -1) / 100;
|
|
|
|
val = this.screen.height * val | 0;
|
|
|
|
}
|
2013-06-11 15:07:04 +00:00
|
|
|
}
|
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-07-23 11:54:21 +00:00
|
|
|
return this.position.top = val;
|
2013-06-11 14:09:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineSetter__('bottom', function(val) {
|
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-07-23 11:54:21 +00:00
|
|
|
return this.position.bottom = val;
|
2013-06-11 14:09:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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-07-23 11:54:21 +00:00
|
|
|
return this.position.left = val;
|
2013-06-11 14:09:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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-07-23 11:54:21 +00:00
|
|
|
return this.position.right = val;
|
2013-06-11 14:09:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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-07-23 11:54:21 +00:00
|
|
|
return this.position.top = val;
|
2013-06-11 14:09:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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-07-23 11:54:21 +00:00
|
|
|
return this.position.bottom = val;
|
2013-06-11 14:09:57 +00:00
|
|
|
});
|
|
|
|
|
2013-07-18 19:52:56 +00:00
|
|
|
Element.prototype.__defineGetter__('ileft', function() {
|
2013-07-19 07:28:31 +00:00
|
|
|
return (this.border ? 1 : 0) + this.padding.left;
|
2013-07-18 19:52:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('itop', function() {
|
2013-07-19 07:28:31 +00:00
|
|
|
return (this.border ? 1 : 0) + this.padding.top;
|
2013-07-18 19:52:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('iright', function() {
|
2013-07-19 07:28:31 +00:00
|
|
|
return (this.border ? 1 : 0) + this.padding.right;
|
2013-07-18 19:52:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('ibottom', function() {
|
2013-07-19 07:28:31 +00:00
|
|
|
return (this.border ? 1 : 0) + this.padding.bottom;
|
2013-07-18 19:52:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('iwidth', function() {
|
2013-07-19 07:28:31 +00:00
|
|
|
return (this.border ? 2 : 0) + this.padding.left + this.padding.right;
|
2013-07-18 19:52:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('iheight', function() {
|
2013-07-19 07:28:31 +00:00
|
|
|
return (this.border ? 2 : 0) + this.padding.top + this.padding.bottom;
|
|
|
|
});
|
|
|
|
|
|
|
|
Element.prototype.__defineGetter__('tpadding', function() {
|
|
|
|
return this.padding.left + this.padding.top
|
|
|
|
+ this.padding.right + this.padding.bottom;
|
2013-07-18 19:52:56 +00:00
|
|
|
});
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
2013-07-27 10:14:02 +00:00
|
|
|
* Rendering - here be dragons
|
2013-06-01 02:09:07 +00:00
|
|
|
*/
|
|
|
|
|
2013-08-15 00:56:47 +00:00
|
|
|
Element.prototype._getShrinkBox = function(xi, xl, yi, yl, get) {
|
2013-07-17 08:52:21 +00:00
|
|
|
if (!this.children.length) {
|
2013-08-04 14:06:04 +00:00
|
|
|
return { xi: xi, xl: xi + 1, yi: yi, yl: yi + 1 };
|
2013-07-17 08:52:21 +00:00
|
|
|
}
|
|
|
|
|
2013-08-04 14:06:04 +00:00
|
|
|
var i, el, ret, mxi = xi, mxl = xi + 1, myi = yi, myl = yi + 1;
|
2013-07-17 08:52:21 +00:00
|
|
|
|
2013-08-15 00:56:47 +00:00
|
|
|
// This is a chicken and egg problem. We need to determine how the children
|
|
|
|
// will render in order to determine how this element renders, but it in
|
|
|
|
// order to figure out how the children will render, they need to know
|
|
|
|
// exactly how their parent renders, so, we can give them what we have so
|
|
|
|
// far.
|
|
|
|
var _lpos;
|
|
|
|
if (get) {
|
|
|
|
_lpos = this.lpos;
|
|
|
|
this.lpos = { xi: xi, xl: xl, yi: yi, yl: yl };
|
|
|
|
//this.shrink = false;
|
|
|
|
}
|
|
|
|
|
2013-07-17 08:52:21 +00:00
|
|
|
for (i = 0; i < this.children.length; i++) {
|
|
|
|
el = this.children[i];
|
|
|
|
|
2013-08-15 00:56:47 +00:00
|
|
|
ret = el._getCoords(get);
|
|
|
|
|
|
|
|
// Or just (seemed to work, but probably not good):
|
|
|
|
// ret = el.lpos || this.lpos;
|
|
|
|
|
2013-07-17 09:20:22 +00:00
|
|
|
if (!ret) continue;
|
2013-07-17 08:52:21 +00:00
|
|
|
|
2013-08-15 00:56:47 +00:00
|
|
|
// Since the parent element is shrunk, and the child elements think it's
|
|
|
|
// going to take up as much space as possible, an element anchored to the
|
|
|
|
// right or bottom will inadvertantly make the parent's shrunken size as
|
|
|
|
// large as possible. So, we can just use the height and/or width the of
|
|
|
|
// element.
|
|
|
|
// if (get) {
|
|
|
|
if (el.position.left == null && el.position.right != null) {
|
|
|
|
ret.xl = xi + (ret.xl - ret.xi);
|
|
|
|
ret.xi = xi;
|
|
|
|
if (this.screen.autoPadding) {
|
|
|
|
// Maybe just do this no matter what.
|
|
|
|
ret.xl += this.ileft;
|
2014-06-02 04:56:32 +00:00
|
|
|
ret.xi += this.ileft;
|
2013-08-15 00:56:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (el.position.top == null && el.position.bottom != null) {
|
|
|
|
ret.yl = yi + (ret.yl - ret.yi);
|
|
|
|
ret.yi = yi;
|
|
|
|
if (this.screen.autoPadding) {
|
|
|
|
// Maybe just do this no matter what.
|
|
|
|
ret.yl += this.itop;
|
|
|
|
ret.yi += this.itop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 08:52:21 +00:00
|
|
|
if (ret.xi < mxi) mxi = ret.xi;
|
|
|
|
if (ret.xl > mxl) mxl = ret.xl;
|
|
|
|
if (ret.yi < myi) myi = ret.yi;
|
|
|
|
if (ret.yl > myl) myl = ret.yl;
|
|
|
|
}
|
|
|
|
|
2013-08-15 00:56:47 +00:00
|
|
|
if (get) {
|
|
|
|
this.lpos = _lpos;
|
|
|
|
//this.shrink = true;
|
|
|
|
}
|
|
|
|
|
2013-07-23 11:54:21 +00:00
|
|
|
if (this.position.width == null
|
|
|
|
&& (this.position.left == null
|
|
|
|
|| this.position.right == null)) {
|
|
|
|
if (this.position.left == null && this.position.right != null) {
|
2013-07-17 10:03:50 +00:00
|
|
|
xi = xl - (mxl - mxi);
|
2013-07-30 13:50:49 +00:00
|
|
|
if (!this.screen.autoPadding) {
|
|
|
|
xi -= this.padding.left + this.padding.right;
|
|
|
|
} else {
|
2014-06-02 04:54:58 +00:00
|
|
|
//xi -= this.padding.left;
|
|
|
|
xi -= this.ileft;
|
2013-07-30 13:50:49 +00:00
|
|
|
}
|
2013-07-17 10:03:50 +00:00
|
|
|
} else {
|
|
|
|
xl = mxl;
|
2013-07-30 13:50:49 +00:00
|
|
|
if (!this.screen.autoPadding) {
|
|
|
|
xl += this.padding.left + this.padding.right;
|
|
|
|
} else {
|
2014-01-11 22:52:33 +00:00
|
|
|
//xl += this.padding.right;
|
|
|
|
xl += this.iright;
|
2013-07-30 13:50:49 +00:00
|
|
|
}
|
2013-07-17 10:03:50 +00:00
|
|
|
}
|
2013-07-17 08:52:21 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 11:54:21 +00:00
|
|
|
if (this.position.height == null
|
|
|
|
&& (this.position.top == null
|
|
|
|
|| this.position.bottom == null)
|
2013-07-24 20:39:22 +00:00
|
|
|
&& !this.scrollable) {
|
2013-07-23 11:54:21 +00:00
|
|
|
if (this.position.top == null && this.position.bottom != null) {
|
2013-07-17 10:03:50 +00:00
|
|
|
yi = yl - (myl - myi);
|
2013-07-30 13:50:49 +00:00
|
|
|
if (!this.screen.autoPadding) {
|
|
|
|
yi -= this.padding.top + this.padding.bottom;
|
|
|
|
} else {
|
2014-06-02 04:54:58 +00:00
|
|
|
//yi -= this.padding.top;
|
|
|
|
yi -= this.itop;
|
2013-07-30 13:50:49 +00:00
|
|
|
}
|
2013-07-17 10:03:50 +00:00
|
|
|
} else {
|
|
|
|
yl = myl;
|
2013-07-30 13:50:49 +00:00
|
|
|
if (!this.screen.autoPadding) {
|
|
|
|
yl += this.padding.top + this.padding.bottom;
|
|
|
|
} else {
|
2014-01-11 22:52:33 +00:00
|
|
|
//yl += this.padding.bottom;
|
|
|
|
yl += this.ibottom;
|
2013-07-30 13:50:49 +00:00
|
|
|
}
|
2013-07-17 10:03:50 +00:00
|
|
|
}
|
2013-07-17 08:52:21 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 08:57:30 +00:00
|
|
|
return { xi: xi, xl: xl, yi: yi, yl: yl };
|
2013-07-17 08:52:21 +00:00
|
|
|
};
|
|
|
|
|
2013-08-15 00:56:47 +00:00
|
|
|
Element.prototype._getShrinkContent = function(xi, xl, yi, yl, get) {
|
2013-07-28 14:58:42 +00:00
|
|
|
var h = this._clines.length
|
2013-08-04 14:06:04 +00:00
|
|
|
, w = this._clines.mwidth || 1;
|
2013-07-17 08:52:21 +00:00
|
|
|
|
2013-07-23 11:54:21 +00:00
|
|
|
if (this.position.width == null
|
|
|
|
&& (this.position.left == null
|
|
|
|
|| this.position.right == null)) {
|
|
|
|
if (this.position.left == null && this.position.right != null) {
|
2013-07-19 07:28:31 +00:00
|
|
|
xi = xl - w - this.iwidth;
|
2013-07-17 08:52:21 +00:00
|
|
|
} else {
|
2013-07-19 07:28:31 +00:00
|
|
|
xl = xi + w + this.iwidth;
|
2013-07-17 08:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-23 11:54:21 +00:00
|
|
|
if (this.position.height == null
|
|
|
|
&& (this.position.top == null
|
|
|
|
|| this.position.bottom == null)
|
2013-07-24 20:39:22 +00:00
|
|
|
&& !this.scrollable) {
|
2013-07-23 11:54:21 +00:00
|
|
|
if (this.position.top == null && this.position.bottom != null) {
|
2013-07-19 07:28:31 +00:00
|
|
|
yi = yl - h - this.iheight;
|
2013-07-17 08:52:21 +00:00
|
|
|
} else {
|
2013-07-19 07:28:31 +00:00
|
|
|
yl = yi + h + this.iheight;
|
2013-07-17 08:52:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 08:57:30 +00:00
|
|
|
return { xi: xi, xl: xl, yi: yi, yl: yl };
|
2013-07-17 08:52:21 +00:00
|
|
|
};
|
|
|
|
|
2013-08-15 00:56:47 +00:00
|
|
|
Element.prototype._getShrink = function(xi, xl, yi, yl, get) {
|
|
|
|
var shrinkBox = this._getShrinkBox(xi, xl, yi, yl, get)
|
|
|
|
, shrinkContent = this._getShrinkContent(xi, xl, yi, yl, get)
|
2013-07-17 10:03:50 +00:00
|
|
|
, xll = xl
|
|
|
|
, yll = yl;
|
2013-07-17 08:52:21 +00:00
|
|
|
|
2013-07-17 10:03:50 +00:00
|
|
|
// Figure out which one is bigger and use it.
|
2013-07-17 09:59:21 +00:00
|
|
|
if (shrinkBox.xl - shrinkBox.xi > shrinkContent.xl - shrinkContent.xi) {
|
|
|
|
xi = shrinkBox.xi;
|
|
|
|
xl = shrinkBox.xl;
|
2013-07-17 08:52:21 +00:00
|
|
|
} else {
|
2013-07-17 09:59:21 +00:00
|
|
|
xi = shrinkContent.xi;
|
|
|
|
xl = shrinkContent.xl;
|
2013-07-17 08:52:21 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 09:59:21 +00:00
|
|
|
if (shrinkBox.yl - shrinkBox.yi > shrinkContent.yl - shrinkContent.yi) {
|
|
|
|
yi = shrinkBox.yi;
|
|
|
|
yl = shrinkBox.yl;
|
2013-07-17 08:52:21 +00:00
|
|
|
} else {
|
2013-07-17 09:59:21 +00:00
|
|
|
yi = shrinkContent.yi;
|
|
|
|
yl = shrinkContent.yl;
|
2013-07-17 08:52:21 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 10:03:50 +00:00
|
|
|
// Recenter shrunken elements.
|
2013-07-23 11:54:21 +00:00
|
|
|
if (xl < xll && this.position.left === 'center') {
|
2013-07-17 10:03:50 +00:00
|
|
|
xll = (xll - xl) / 2 | 0;
|
|
|
|
xi += xll;
|
|
|
|
xl += xll;
|
|
|
|
}
|
|
|
|
|
2013-07-23 11:54:21 +00:00
|
|
|
if (yl < yll && this.position.top === 'center') {
|
2013-07-17 10:03:50 +00:00
|
|
|
yll = (yll - yl) / 2 | 0;
|
|
|
|
yi += yll;
|
|
|
|
yl += yll;
|
|
|
|
}
|
|
|
|
|
2013-07-17 08:57:30 +00:00
|
|
|
return { xi: xi, xl: xl, yi: yi, yl: yl };
|
2013-07-17 08:52:21 +00:00
|
|
|
};
|
|
|
|
|
2014-01-12 11:08:16 +00:00
|
|
|
Element.prototype._getCoords = function(get, noscroll) {
|
2013-06-06 10:38:30 +00:00
|
|
|
if (this.hidden) return;
|
|
|
|
|
2015-01-25 05:33:30 +00:00
|
|
|
// if (this.parent._rendering) {
|
|
|
|
// get = true;
|
|
|
|
// }
|
2013-08-15 12:14:56 +00:00
|
|
|
|
2013-07-19 01:55:16 +00:00
|
|
|
var xi = this._getLeft(get)
|
|
|
|
, xl = xi + this._getWidth(get)
|
|
|
|
, yi = this._getTop(get)
|
|
|
|
, yl = yi + this._getHeight(get)
|
2013-07-27 10:16:56 +00:00
|
|
|
, base = this.childBase || 0
|
|
|
|
, el = this
|
2013-07-28 08:01:09 +00:00
|
|
|
, fixed = this.fixed
|
2013-07-19 06:57:17 +00:00
|
|
|
, coords
|
2013-07-25 02:21:20 +00:00
|
|
|
, v
|
2013-08-01 09:11:22 +00:00
|
|
|
, noleft
|
|
|
|
, noright
|
2013-07-26 06:45:39 +00:00
|
|
|
, notop
|
2013-07-27 10:34:59 +00:00
|
|
|
, nobot
|
2013-08-01 12:09:06 +00:00
|
|
|
, ppos
|
|
|
|
, b;
|
2013-06-25 23:17:21 +00:00
|
|
|
|
2013-07-26 05:19:57 +00:00
|
|
|
// Attempt to shrink the element base on the
|
|
|
|
// size of the content and child elements.
|
|
|
|
if (this.shrink) {
|
2013-08-15 00:56:47 +00:00
|
|
|
coords = this._getShrink(xi, xl, yi, yl, get);
|
2013-07-26 05:19:57 +00:00
|
|
|
xi = coords.xi, xl = coords.xl;
|
|
|
|
yi = coords.yi, yl = coords.yl;
|
|
|
|
}
|
|
|
|
|
2013-07-27 10:16:56 +00:00
|
|
|
// Find a scrollable ancestor if we have one.
|
|
|
|
while (el = el.parent) {
|
2013-07-28 08:01:09 +00:00
|
|
|
if (el.scrollable) {
|
|
|
|
if (fixed) {
|
|
|
|
fixed = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-07-27 10:16:56 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 09:20:22 +00:00
|
|
|
// Check to make sure we're visible and
|
|
|
|
// inside of the visible scroll area.
|
2013-07-21 10:31:24 +00:00
|
|
|
// NOTE: Lists have a property where only
|
|
|
|
// the list items are obfuscated.
|
2014-01-12 12:12:13 +00:00
|
|
|
|
|
|
|
// Old way of doing things, this would not render right if a shrunken element
|
|
|
|
// with lots of boxes in it was within a scrollable element.
|
2014-01-12 12:19:54 +00:00
|
|
|
// See: $ node test/widget-shrink-fail.js
|
2014-01-12 12:12:13 +00:00
|
|
|
// var thisparent = this.parent;
|
|
|
|
|
|
|
|
var thisparent = el;
|
2014-01-12 11:08:16 +00:00
|
|
|
if (el && !noscroll) {
|
2014-01-12 12:12:13 +00:00
|
|
|
ppos = thisparent.lpos;
|
2013-07-27 10:34:59 +00:00
|
|
|
|
2013-07-29 15:22:08 +00:00
|
|
|
// The shrink option can cause a stack overflow
|
|
|
|
// by calling _getCoords on the child again.
|
2014-01-12 12:12:13 +00:00
|
|
|
// if (!get && !thisparent.shrink) {
|
|
|
|
// ppos = thisparent._getCoords();
|
2013-07-29 15:22:08 +00:00
|
|
|
// }
|
|
|
|
|
2013-07-28 19:49:01 +00:00
|
|
|
if (!ppos) return;
|
|
|
|
|
2013-08-01 12:09:06 +00:00
|
|
|
// TODO: Figure out how to fix base (and cbase to only
|
|
|
|
// take into account the *parent's* padding.
|
|
|
|
|
2013-07-29 15:22:08 +00:00
|
|
|
yi -= ppos.base;
|
|
|
|
yl -= ppos.base;
|
2013-07-19 14:17:21 +00:00
|
|
|
|
2014-01-12 12:12:13 +00:00
|
|
|
b = thisparent.border ? 1 : 0;
|
2013-08-01 12:09:06 +00:00
|
|
|
|
2015-01-13 23:04:24 +00:00
|
|
|
// XXX
|
|
|
|
// Fixes non-`fixed` labels to work with scrolling (they're ON the border):
|
2015-01-15 21:22:55 +00:00
|
|
|
// if (this.position.left < 0
|
|
|
|
// || this.position.right < 0
|
|
|
|
// || this.position.top < 0
|
|
|
|
// || this.position.bottom < 0) {
|
|
|
|
if (this._isLabel) {
|
2015-01-14 18:46:07 +00:00
|
|
|
b = 0;
|
|
|
|
}
|
2015-01-13 23:04:24 +00:00
|
|
|
|
2013-08-01 12:09:06 +00:00
|
|
|
if (yi < ppos.yi + b) {
|
|
|
|
if (yl - 1 < ppos.yi + b) {
|
2013-07-26 05:19:57 +00:00
|
|
|
// Is above.
|
|
|
|
return;
|
|
|
|
} else {
|
2013-07-27 10:16:56 +00:00
|
|
|
// Is partially covered above.
|
2013-07-26 06:45:39 +00:00
|
|
|
notop = true;
|
2013-07-27 10:34:59 +00:00
|
|
|
v = ppos.yi - yi;
|
2013-08-01 12:09:06 +00:00
|
|
|
if (this.border) v--;
|
2014-01-12 12:12:13 +00:00
|
|
|
if (thisparent.border) v++;
|
2013-07-27 10:16:56 +00:00
|
|
|
base += v;
|
2013-07-25 02:21:20 +00:00
|
|
|
yi += v;
|
2013-07-26 05:19:57 +00:00
|
|
|
}
|
2013-08-01 12:09:06 +00:00
|
|
|
} else if (yl > ppos.yl - b) {
|
|
|
|
if (yi > ppos.yl - 1 - b) {
|
2013-07-26 05:19:57 +00:00
|
|
|
// Is below.
|
2013-07-23 21:57:30 +00:00
|
|
|
return;
|
2013-07-26 05:19:57 +00:00
|
|
|
} else {
|
|
|
|
// Is partially covered below.
|
2013-07-26 06:45:39 +00:00
|
|
|
nobot = true;
|
2013-07-27 10:34:59 +00:00
|
|
|
v = yl - ppos.yl;
|
2013-08-01 12:09:06 +00:00
|
|
|
if (this.border) v--;
|
2014-01-12 12:12:13 +00:00
|
|
|
if (thisparent.border) v++;
|
2013-07-26 05:19:57 +00:00
|
|
|
yl -= v;
|
2013-07-23 21:57:30 +00:00
|
|
|
}
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
2013-07-19 01:55:16 +00:00
|
|
|
|
2013-07-26 05:19:57 +00:00
|
|
|
// Shouldn't be necessary.
|
2013-07-27 10:34:59 +00:00
|
|
|
// assert.ok(yi < yl);
|
2013-07-26 05:19:57 +00:00
|
|
|
if (yi >= yl) return;
|
2013-08-01 09:11:22 +00:00
|
|
|
|
|
|
|
// Could allow overlapping stuff in scrolling elements
|
|
|
|
// if we cleared the pending buffer before every draw.
|
|
|
|
if (xi < el.lpos.xi) {
|
|
|
|
xi = el.lpos.xi;
|
|
|
|
noleft = true;
|
|
|
|
if (this.border) xi--;
|
2014-01-12 12:12:13 +00:00
|
|
|
if (thisparent.border) xi++;
|
2013-08-01 09:11:22 +00:00
|
|
|
}
|
|
|
|
if (xl > el.lpos.xl) {
|
|
|
|
xl = el.lpos.xl;
|
|
|
|
noright = true;
|
|
|
|
if (this.border) xl++;
|
2014-01-12 12:12:13 +00:00
|
|
|
if (thisparent.border) xl--;
|
2013-08-01 09:11:22 +00:00
|
|
|
}
|
2013-08-04 14:06:04 +00:00
|
|
|
//if (xi > xl) return;
|
2013-08-01 09:11:22 +00:00
|
|
|
if (xi >= xl) return;
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
|
2014-01-12 02:47:40 +00:00
|
|
|
if (this.noOverflow && this.parent.lpos) {
|
2014-01-12 02:56:59 +00:00
|
|
|
if (xi < this.parent.lpos.xi + this.parent.ileft) {
|
|
|
|
xi = this.parent.lpos.xi + this.parent.ileft;
|
2014-01-12 02:47:40 +00:00
|
|
|
}
|
|
|
|
if (xl > this.parent.lpos.xl - this.parent.iright) {
|
|
|
|
xl = this.parent.lpos.xl - this.parent.iright;
|
|
|
|
}
|
2014-01-12 02:56:59 +00:00
|
|
|
if (yi < this.parent.lpos.yi + this.parent.itop) {
|
|
|
|
yi = this.parent.lpos.yi + this.parent.itop;
|
2014-01-12 02:47:40 +00:00
|
|
|
}
|
|
|
|
if (yl > this.parent.lpos.yl - this.parent.ibottom) {
|
|
|
|
yl = this.parent.lpos.yl - this.parent.ibottom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-25 05:33:30 +00:00
|
|
|
// if (this.parent.lpos) {
|
|
|
|
// this.parent.lpos._scrollBottom = Math.max(
|
|
|
|
// this.parent.lpos._scrollBottom, yl);
|
|
|
|
// }
|
2014-01-12 11:08:16 +00:00
|
|
|
|
2013-07-18 08:54:41 +00:00
|
|
|
return {
|
2013-07-17 08:57:30 +00:00
|
|
|
xi: xi,
|
2013-06-06 09:03:25 +00:00
|
|
|
xl: xl,
|
2013-07-17 08:57:30 +00:00
|
|
|
yi: yi,
|
2013-07-21 10:31:24 +00:00
|
|
|
yl: yl,
|
2013-07-27 10:16:56 +00:00
|
|
|
base: base,
|
2013-08-01 09:11:22 +00:00
|
|
|
noleft: noleft,
|
|
|
|
noright: noright,
|
2013-07-26 06:45:39 +00:00
|
|
|
notop: notop,
|
2014-01-11 22:52:33 +00:00
|
|
|
nobot: nobot,
|
|
|
|
renders: this.screen.renders
|
2013-06-06 09:03:25 +00:00
|
|
|
};
|
2013-07-18 08:54:41 +00:00
|
|
|
};
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.prototype.render = function() {
|
2013-08-02 10:57:58 +00:00
|
|
|
this._emit('prerender');
|
2013-07-26 06:45:39 +00:00
|
|
|
|
2013-07-18 08:54:41 +00:00
|
|
|
this.parseContent();
|
2013-06-06 09:03:25 +00:00
|
|
|
|
2013-07-19 01:55:16 +00:00
|
|
|
var coords = this._getCoords(true);
|
2013-07-26 06:45:39 +00:00
|
|
|
if (!coords) {
|
|
|
|
delete this.lpos;
|
|
|
|
return;
|
|
|
|
}
|
2013-07-26 05:19:57 +00:00
|
|
|
|
2013-07-18 08:54:41 +00:00
|
|
|
var lines = this.screen.lines
|
|
|
|
, xi = coords.xi
|
|
|
|
, xl = coords.xl
|
|
|
|
, yi = coords.yi
|
|
|
|
, yl = coords.yl
|
|
|
|
, x
|
|
|
|
, y
|
|
|
|
, cell
|
|
|
|
, attr
|
|
|
|
, ch
|
|
|
|
, content = this._pcontent
|
2013-07-27 10:16:56 +00:00
|
|
|
, ci = this._clines.ci[coords.base]
|
2013-07-18 08:54:41 +00:00
|
|
|
, battr
|
|
|
|
, dattr
|
|
|
|
, c
|
|
|
|
, rtop
|
|
|
|
, visible
|
2013-08-12 13:12:18 +00:00
|
|
|
, i
|
2013-08-12 14:04:20 +00:00
|
|
|
, bch = this.ch;
|
2013-07-17 06:32:49 +00:00
|
|
|
|
2013-07-25 02:21:20 +00:00
|
|
|
if (coords.base >= this._clines.ci.length) {
|
|
|
|
ci = this._pcontent.length;
|
|
|
|
}
|
|
|
|
|
2013-07-18 08:54:41 +00:00
|
|
|
this.lpos = coords;
|
2013-06-06 14:24:03 +00:00
|
|
|
|
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-07-19 01:55:16 +00:00
|
|
|
// If we're in a scrollable text box, check to
|
|
|
|
// see which attributes this line starts with.
|
2013-07-25 02:21:20 +00:00
|
|
|
if (ci > 0) {
|
2013-07-27 10:16:56 +00:00
|
|
|
attr = this._clines.attr[Math.min(coords.base, this._clines.length - 1)];
|
2013-06-10 01:19:32 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 09:20:22 +00:00
|
|
|
if (this.border) xi++, xl--, yi++, yl--;
|
2013-06-13 10:30:21 +00:00
|
|
|
|
2013-07-17 09:20:22 +00:00
|
|
|
// If we have padding/valign, that means the
|
|
|
|
// content-drawing loop will skip a few cells/lines.
|
|
|
|
// To deal with this, we can just fill the whole thing
|
|
|
|
// ahead of time. This could be optimized.
|
2013-07-19 07:28:31 +00:00
|
|
|
if (this.tpadding || (this.valign && this.valign !== 'top')) {
|
2013-10-16 11:51:14 +00:00
|
|
|
this.screen.fillRegion(dattr, bch, xi, xl, yi, yl);
|
2013-07-17 01:19:44 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 07:28:31 +00:00
|
|
|
if (this.tpadding) {
|
|
|
|
xi += this.padding.left, xl -= this.padding.right;
|
|
|
|
yi += this.padding.top, yl -= this.padding.bottom;
|
2013-06-14 00:02:02 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 09:20:22 +00:00
|
|
|
// Determine where to place the text if it's vertically aligned.
|
|
|
|
if (this.valign === 'middle' || this.valign === 'bottom') {
|
2013-07-17 08:57:30 +00:00
|
|
|
visible = yl - yi;
|
2013-07-17 01:19:44 +00:00
|
|
|
if (this._clines.length < visible) {
|
2013-07-17 09:20:22 +00:00
|
|
|
if (this.valign === 'middle') {
|
2013-07-17 01:19:44 +00:00
|
|
|
visible = visible / 2 | 0;
|
|
|
|
visible -= this._clines.length / 2 | 0;
|
2013-07-17 09:20:22 +00:00
|
|
|
} else if (this.valign === 'bottom') {
|
2013-07-17 01:19:44 +00:00
|
|
|
visible -= this._clines.length;
|
|
|
|
}
|
2013-07-17 08:57:30 +00:00
|
|
|
yi += visible;
|
2013-07-17 01:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 09:20:22 +00:00
|
|
|
// Draw the content and background.
|
2013-07-17 08:57:30 +00:00
|
|
|
for (y = yi; y < yl; y++) {
|
|
|
|
if (!lines[y]) break;
|
|
|
|
for (x = xi; x < xl; x++) {
|
|
|
|
cell = lines[y][x];
|
2013-06-13 19:58:00 +00:00
|
|
|
if (!cell) break;
|
|
|
|
|
2013-08-12 14:04:20 +00:00
|
|
|
ch = content[ci++] || bch;
|
2013-06-06 14:24:03 +00:00
|
|
|
|
2013-08-13 18:02:02 +00:00
|
|
|
// if (!content[ci] && !coords._contentEnd) {
|
|
|
|
// coords._contentEnd = { x: x - xi, y: y - yi };
|
|
|
|
// }
|
|
|
|
|
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-28 15:52:13 +00:00
|
|
|
attr = this.screen.attrCode(c[0], attr, dattr);
|
2013-08-02 10:57:58 +00:00
|
|
|
// Ignore foreground changes for selected items.
|
2015-02-10 04:11:02 +00:00
|
|
|
if (this.parent._isList && this.parent.interactive
|
2013-08-02 10:57:58 +00:00
|
|
|
&& this.parent.items[this.parent.selected] === this) {
|
|
|
|
attr = (attr & ~(0x1ff << 9)) | (dattr & (0x1ff << 9));
|
|
|
|
}
|
2013-08-12 14:04:20 +00:00
|
|
|
ch = content[ci] || bch;
|
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-10-16 11:51:14 +00:00
|
|
|
if (ch === '\t') ch = bch;
|
2013-07-25 02:21:20 +00:00
|
|
|
if (ch === '\n') {
|
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".
|
2013-07-17 08:57:30 +00:00
|
|
|
if (x === xi && y !== yi && content[ci-2] !== '\n') {
|
|
|
|
x--;
|
2013-06-16 09:21:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-07-17 09:20:22 +00:00
|
|
|
// We could use fillRegion here, name the
|
|
|
|
// outer loop, and continue to it instead.
|
2013-08-12 14:04:20 +00:00
|
|
|
ch = bch;
|
2013-07-17 08:57:30 +00:00
|
|
|
for (; x < xl; x++) {
|
|
|
|
cell = lines[y][x];
|
2013-06-13 19:58:00 +00:00
|
|
|
if (!cell) break;
|
|
|
|
if (attr !== cell[0] || ch !== cell[1]) {
|
2013-07-17 08:57:30 +00:00
|
|
|
lines[y][x][0] = attr;
|
|
|
|
lines[y][x][1] = ch;
|
|
|
|
lines[y].dirty = true;
|
2013-06-13 19:58:00 +00:00
|
|
|
}
|
2013-06-06 12:57:50 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-06 09:33:10 +00:00
|
|
|
|
2013-06-13 19:58:00 +00:00
|
|
|
if (attr !== cell[0] || ch !== cell[1]) {
|
2013-07-17 08:57:30 +00:00
|
|
|
lines[y][x][0] = attr;
|
|
|
|
lines[y][x][1] = ch;
|
|
|
|
lines[y].dirty = true;
|
2013-06-13 19:58:00 +00:00
|
|
|
}
|
2013-08-10 14:30:05 +00:00
|
|
|
|
|
|
|
// if (wideChars.test(ch)) {
|
|
|
|
// x++;
|
|
|
|
// lines[y][x][0] = attr;
|
2013-10-16 11:51:14 +00:00
|
|
|
// lines[y][x][1] = bch;
|
2013-08-10 14:30:05 +00:00
|
|
|
// }
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 09:20:22 +00:00
|
|
|
// Draw the scrollbar.
|
2013-07-27 11:58:50 +00:00
|
|
|
// Could possibly draw this after all child elements.
|
2013-07-19 06:57:17 +00:00
|
|
|
if (this.scrollbar) {
|
2013-07-27 10:49:53 +00:00
|
|
|
i = Math.max(this._clines.length, this._scrollBottom());
|
2013-07-19 06:57:17 +00:00
|
|
|
}
|
2013-07-25 06:20:47 +00:00
|
|
|
if (coords.notop || coords.nobot) i = -Infinity;
|
2013-07-17 09:20:22 +00:00
|
|
|
if (this.scrollbar && (yl - yi) < i) {
|
2013-07-17 08:57:30 +00:00
|
|
|
x = xl - 1;
|
|
|
|
if (this.scrollbar.ignoreBorder && this.border) x++;
|
2013-07-27 10:49:53 +00:00
|
|
|
if (this.alwaysScroll) {
|
|
|
|
y = this.childBase / (i - (yl - yi));
|
|
|
|
} else {
|
2013-08-05 01:25:10 +00:00
|
|
|
y = (this.childBase + this.childOffset) / (i - 1);
|
2013-07-27 10:49:53 +00:00
|
|
|
}
|
2013-07-19 06:57:17 +00:00
|
|
|
y = yi + ((yl - yi) * y | 0);
|
2013-07-27 10:49:53 +00:00
|
|
|
if (y >= yl) y = yl - 1;
|
2013-07-17 08:57:30 +00:00
|
|
|
cell = lines[y] && lines[y][x];
|
2013-06-25 23:17:21 +00:00
|
|
|
if (cell) {
|
2013-07-27 11:58:50 +00:00
|
|
|
if (this.track) {
|
|
|
|
ch = this.track.ch || ' ';
|
|
|
|
attr = this.sattr(this.style.track,
|
|
|
|
this.style.track.fg || this.style.fg,
|
|
|
|
this.style.track.bg || this.style.bg);
|
|
|
|
this.screen.fillRegion(attr, ch, x, x + 1, yi, yl);
|
|
|
|
}
|
2013-06-25 23:17:21 +00:00
|
|
|
ch = this.scrollbar.ch || ' ';
|
2013-07-17 17:01:54 +00:00
|
|
|
attr = this.sattr(this.style.scrollbar,
|
2013-07-16 22:50:02 +00:00
|
|
|
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]) {
|
2013-07-17 08:57:30 +00:00
|
|
|
lines[y][x][0] = attr;
|
|
|
|
lines[y][x][1] = ch;
|
|
|
|
lines[y].dirty = true;
|
2013-06-25 23:17:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 09:20:22 +00:00
|
|
|
if (this.border) xi--, xl++, yi--, yl++;
|
2013-06-14 00:02:02 +00:00
|
|
|
|
2013-07-19 07:28:31 +00:00
|
|
|
if (this.tpadding) {
|
|
|
|
xi -= this.padding.left, xl += this.padding.right;
|
|
|
|
yi -= this.padding.top, yl += this.padding.bottom;
|
2013-06-14 00:02:02 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 09:20:22 +00:00
|
|
|
// Draw the border.
|
2013-06-13 10:30:21 +00:00
|
|
|
if (this.border) {
|
2013-07-18 08:54:41 +00:00
|
|
|
battr = this.sattr(this.style.border,
|
|
|
|
this.style.border.fg, this.style.border.bg);
|
2013-07-17 08:57:30 +00:00
|
|
|
y = yi;
|
2013-07-25 06:20:47 +00:00
|
|
|
if (coords.notop) y = -1;
|
2013-07-17 08:57:30 +00:00
|
|
|
for (x = xi; x < xl; x++) {
|
|
|
|
if (!lines[y]) break;
|
2013-08-01 09:11:22 +00:00
|
|
|
if (coords.noleft && x === xi) continue;
|
|
|
|
if (coords.noright && x === xl - 1) continue;
|
2013-07-17 00:07:46 +00:00
|
|
|
if (this.border.type === 'line') {
|
2013-07-17 08:57:30 +00:00
|
|
|
if (x === xi) ch = '┌';
|
|
|
|
else if (x === xl - 1) ch = '┐';
|
2013-06-13 10:30:21 +00:00
|
|
|
else ch = '─';
|
2013-06-13 19:58:00 +00:00
|
|
|
} else if (this.border.type === 'bg') {
|
|
|
|
ch = this.border.ch;
|
|
|
|
}
|
2013-07-17 08:57:30 +00:00
|
|
|
cell = lines[y][x];
|
2013-06-13 19:58:00 +00:00
|
|
|
if (!cell) break;
|
|
|
|
if (battr !== cell[0] || ch !== cell[1]) {
|
2013-07-17 08:57:30 +00:00
|
|
|
lines[y][x][0] = battr;
|
|
|
|
lines[y][x][1] = ch;
|
|
|
|
lines[y].dirty = true;
|
2013-06-13 10:30:21 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-17 08:57:30 +00:00
|
|
|
y = yi + 1;
|
2013-07-27 10:14:02 +00:00
|
|
|
for (; y < yl - 1; y++) {
|
2013-07-17 08:57:30 +00:00
|
|
|
if (!lines[y]) 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;
|
|
|
|
}
|
2013-07-17 08:57:30 +00:00
|
|
|
cell = lines[y][xi];
|
2013-06-13 19:58:00 +00:00
|
|
|
if (!cell) break;
|
2013-08-01 09:11:22 +00:00
|
|
|
if (!coords.noleft)
|
2013-06-13 19:58:00 +00:00
|
|
|
if (battr !== cell[0] || ch !== cell[1]) {
|
2013-07-17 08:57:30 +00:00
|
|
|
lines[y][xi][0] = battr;
|
|
|
|
lines[y][xi][1] = ch;
|
|
|
|
lines[y].dirty = true;
|
2013-06-13 19:58:00 +00:00
|
|
|
}
|
2013-07-17 08:57:30 +00:00
|
|
|
cell = lines[y][xl - 1];
|
2013-06-13 19:58:00 +00:00
|
|
|
if (!cell) break;
|
2013-08-01 09:11:22 +00:00
|
|
|
if (!coords.noright)
|
2013-06-13 19:58:00 +00:00
|
|
|
if (battr !== cell[0] || ch !== cell[1]) {
|
2013-07-17 08:57:30 +00:00
|
|
|
lines[y][xl - 1][0] = battr;
|
|
|
|
lines[y][xl - 1][1] = ch;
|
|
|
|
lines[y].dirty = true;
|
2013-06-13 19:58:00 +00:00
|
|
|
}
|
2013-06-13 10:30:21 +00:00
|
|
|
}
|
2013-07-17 08:57:30 +00:00
|
|
|
y = yl - 1;
|
2013-07-25 06:20:47 +00:00
|
|
|
if (coords.nobot) y = -1;
|
2013-07-17 08:57:30 +00:00
|
|
|
for (x = xi; x < xl; x++) {
|
|
|
|
if (!lines[y]) break;
|
2013-08-01 09:11:22 +00:00
|
|
|
if (coords.noleft && x === xi) continue;
|
|
|
|
if (coords.noright && x === xl - 1) continue;
|
2013-07-17 00:07:46 +00:00
|
|
|
if (this.border.type === 'line') {
|
2013-07-17 08:57:30 +00:00
|
|
|
if (x === xi) ch = '└';
|
|
|
|
else if (x === xl - 1) ch = '┘';
|
2013-06-13 10:30:21 +00:00
|
|
|
else ch = '─';
|
2013-06-13 19:58:00 +00:00
|
|
|
} else if (this.border.type === 'bg') {
|
|
|
|
ch = this.border.ch;
|
|
|
|
}
|
2013-07-17 08:57:30 +00:00
|
|
|
cell = lines[y][x];
|
2013-06-13 19:58:00 +00:00
|
|
|
if (!cell) break;
|
|
|
|
if (battr !== cell[0] || ch !== cell[1]) {
|
2013-07-17 08:57:30 +00:00
|
|
|
lines[y][x][0] = battr;
|
|
|
|
lines[y][x][1] = ch;
|
|
|
|
lines[y].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
|
|
|
}
|
2015-01-25 05:33:30 +00:00
|
|
|
// if (el.screen._rendering) {
|
|
|
|
// el._rendering = true;
|
|
|
|
// }
|
2013-06-01 02:09:07 +00:00
|
|
|
el.render();
|
2015-01-25 05:33:30 +00:00
|
|
|
// if (el.screen._rendering) {
|
|
|
|
// el._rendering = false;
|
|
|
|
// }
|
2013-06-01 02:09:07 +00:00
|
|
|
});
|
2013-06-01 11:08:18 +00:00
|
|
|
|
2013-08-02 10:57:58 +00:00
|
|
|
this._emit('render', [coords]);
|
2013-07-24 23:05:56 +00:00
|
|
|
|
2013-07-18 08:54:41 +00:00
|
|
|
return coords;
|
2013-06-01 02:09:07 +00:00
|
|
|
};
|
|
|
|
|
2013-07-30 00:22:50 +00:00
|
|
|
Element.prototype._render = Element.prototype.render;
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.prototype.insertLine = function(i, line) {
|
|
|
|
if (typeof line === 'string') line = line.split('\n');
|
2013-07-15 09:40:44 +00:00
|
|
|
|
2013-07-23 19:34:29 +00:00
|
|
|
if (i !== i || i == null) {
|
|
|
|
i = this._clines.ftor.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = Math.max(i, 0);
|
|
|
|
|
2013-07-23 19:10:46 +00:00
|
|
|
while (this._clines.fake.length < i) {
|
|
|
|
this._clines.fake.push('');
|
|
|
|
this._clines.ftor.push([this._clines.push('') - 1]);
|
2013-07-23 19:34:29 +00:00
|
|
|
this._clines.rtof(this._clines.fake.length - 1);
|
2013-07-23 19:10:46 +00:00
|
|
|
}
|
2013-07-15 22:45:08 +00:00
|
|
|
|
2013-07-23 19:34:29 +00:00
|
|
|
// NOTE: Could possibly compare the first and last ftor line numbers to see
|
|
|
|
// if they're the same, or if they fit in the visible region entirely.
|
2013-07-23 19:10:46 +00:00
|
|
|
var start = this._clines.length
|
|
|
|
, diff
|
2013-07-23 19:34:29 +00:00
|
|
|
, real;
|
2013-07-23 19:10:46 +00:00
|
|
|
|
|
|
|
if (i >= this._clines.ftor.length) {
|
2013-07-23 19:34:29 +00:00
|
|
|
real = this._clines.ftor[this._clines.ftor.length - 1];
|
|
|
|
real = real[real.length-1] + 1;
|
2013-07-23 19:10:46 +00:00
|
|
|
} else {
|
2013-07-23 19:34:29 +00:00
|
|
|
real = this._clines.ftor[i][0];
|
2013-07-12 08:52:14 +00:00
|
|
|
}
|
2013-07-15 09:40:44 +00:00
|
|
|
|
2013-07-23 19:34:29 +00:00
|
|
|
for (var j = 0; j < line.length; j++) {
|
|
|
|
this._clines.fake.splice(i + j, 0, line[j]);
|
|
|
|
}
|
2013-07-15 09:40:44 +00:00
|
|
|
|
2013-07-23 19:10:46 +00:00
|
|
|
this.setContent(this._clines.fake.join('\n'), true);
|
|
|
|
|
|
|
|
diff = this._clines.length - start;
|
|
|
|
|
|
|
|
if (diff > 0) {
|
|
|
|
var pos = this._getCoords();
|
|
|
|
if (!pos) return;
|
|
|
|
|
|
|
|
var height = pos.yl - pos.yi - this.iheight
|
|
|
|
, base = this.childBase || 0
|
2013-07-23 19:34:29 +00:00
|
|
|
, visible = real >= base && real - base < height;
|
2013-07-23 19:10:46 +00:00
|
|
|
|
|
|
|
if (pos && visible && this.screen.cleanSides(this)) {
|
|
|
|
this.screen.insertLine(diff,
|
2013-07-23 19:34:29 +00:00
|
|
|
pos.yi + this.itop + real - base,
|
2013-07-23 19:10:46 +00:00
|
|
|
pos.yi,
|
|
|
|
pos.yl - this.ibottom - 1);
|
|
|
|
}
|
|
|
|
}
|
2013-07-12 08:52:14 +00:00
|
|
|
};
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.prototype.deleteLine = function(i, n) {
|
2013-07-23 18:31:05 +00:00
|
|
|
n = n || 1;
|
2013-07-23 19:10:46 +00:00
|
|
|
|
2013-07-23 19:34:29 +00:00
|
|
|
if (i !== i || i == null) {
|
2013-07-23 19:10:46 +00:00
|
|
|
i = this._clines.ftor.length - 1;
|
|
|
|
}
|
|
|
|
|
2013-07-15 22:45:08 +00:00
|
|
|
i = Math.max(i, 0);
|
2013-07-23 19:10:46 +00:00
|
|
|
i = Math.min(i, this._clines.ftor.length - 1);
|
2013-07-15 22:45:08 +00:00
|
|
|
|
2013-07-23 19:34:29 +00:00
|
|
|
// NOTE: Could possibly compare the first and last ftor line numbers to see
|
|
|
|
// if they're the same, or if they fit in the visible region entirely.
|
2013-07-23 19:10:46 +00:00
|
|
|
var start = this._clines.length
|
|
|
|
, diff
|
2013-07-23 19:34:29 +00:00
|
|
|
, real = this._clines.ftor[i][0];
|
2013-07-15 22:45:08 +00:00
|
|
|
|
2013-07-23 19:10:46 +00:00
|
|
|
while (n--) {
|
|
|
|
this._clines.fake.splice(i, 1);
|
2013-07-12 08:52:14 +00:00
|
|
|
}
|
2013-07-15 09:40:44 +00:00
|
|
|
|
2013-07-23 19:10:46 +00:00
|
|
|
this.setContent(this._clines.fake.join('\n'), true);
|
|
|
|
|
|
|
|
diff = start - this._clines.length;
|
|
|
|
|
|
|
|
if (diff > 0) {
|
|
|
|
var pos = this._getCoords();
|
|
|
|
if (!pos) return;
|
|
|
|
|
|
|
|
var height = pos.yl - pos.yi - this.iheight
|
|
|
|
, base = this.childBase || 0
|
2013-07-23 19:34:29 +00:00
|
|
|
, visible = real >= base && real - base < height;
|
2013-07-23 19:10:46 +00:00
|
|
|
|
|
|
|
if (pos && visible && this.screen.cleanSides(this)) {
|
|
|
|
this.screen.deleteLine(diff,
|
2013-07-23 19:34:29 +00:00
|
|
|
pos.yi + this.itop + real - base,
|
2013-07-23 19:10:46 +00:00
|
|
|
pos.yi,
|
|
|
|
pos.yl - this.ibottom - 1);
|
|
|
|
}
|
2013-07-15 09:40:44 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 18:31:05 +00:00
|
|
|
if (this._clines.length < height) {
|
|
|
|
this.clearPos();
|
|
|
|
}
|
2013-07-12 08:52:14 +00:00
|
|
|
};
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.prototype.insertTop = function(line) {
|
2013-07-23 19:10:46 +00:00
|
|
|
var fake = this._clines.rtof[this.childBase || 0];
|
|
|
|
return this.insertLine(fake, line);
|
2013-07-12 08:52:14 +00:00
|
|
|
};
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.prototype.insertBottom = function(line) {
|
2013-07-23 18:31:05 +00:00
|
|
|
var h = (this.childBase || 0) + this.height - this.iheight
|
2013-07-23 19:10:46 +00:00
|
|
|
, i = Math.min(h, this._clines.length)
|
|
|
|
, fake = this._clines.rtof[i - 1] + 1;
|
2013-07-23 18:31:05 +00:00
|
|
|
|
2013-07-23 19:10:46 +00:00
|
|
|
return this.insertLine(fake, line);
|
2013-07-12 08:52:14 +00:00
|
|
|
};
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.prototype.deleteTop = function(n) {
|
2013-07-23 19:10:46 +00:00
|
|
|
var fake = this._clines.rtof[this.childBase || 0];
|
|
|
|
return this.deleteLine(fake, n);
|
2013-07-12 08:52:14 +00:00
|
|
|
};
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.prototype.deleteBottom = function(n) {
|
2013-07-23 18:31:05 +00:00
|
|
|
var h = (this.childBase || 0) + this.height - 1 - this.iheight
|
|
|
|
, i = Math.min(h, this._clines.length - 1)
|
2013-07-23 19:10:46 +00:00
|
|
|
, n = n || 1
|
|
|
|
, fake = this._clines.rtof[i];
|
2013-07-23 18:31:05 +00:00
|
|
|
|
2013-07-23 19:10:46 +00:00
|
|
|
return this.deleteLine(fake - (n - 1), n);
|
2013-07-12 08:52:14 +00:00
|
|
|
};
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.prototype.setLine = function(i, line) {
|
2013-07-23 19:34:29 +00:00
|
|
|
i = Math.max(i, 0);
|
2013-07-23 19:10:46 +00:00
|
|
|
while (this._clines.fake.length < i) {
|
|
|
|
this._clines.fake.push('');
|
2013-07-15 22:45:08 +00:00
|
|
|
}
|
2013-07-23 19:10:46 +00:00
|
|
|
this._clines.fake[i] = line;
|
|
|
|
return this.setContent(this._clines.fake.join('\n'), true);
|
2013-07-12 08:52:14 +00:00
|
|
|
};
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.prototype.setBaseLine = function(i, line) {
|
2013-07-23 19:10:46 +00:00
|
|
|
var fake = this._clines.rtof[this.childBase || 0];
|
|
|
|
return this.setLine(fake + i, line);
|
2013-07-23 18:31:05 +00:00
|
|
|
};
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.prototype.getLine = function(i) {
|
2013-07-12 08:52:14 +00:00
|
|
|
i = Math.max(i, 0);
|
2013-07-23 19:10:46 +00:00
|
|
|
i = Math.min(i, this._clines.fake.length - 1);
|
|
|
|
return this._clines.fake[i];
|
2013-07-23 18:31:05 +00:00
|
|
|
};
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.prototype.getBaseLine = function(i) {
|
2013-07-23 19:10:46 +00:00
|
|
|
var fake = this._clines.rtof[this.childBase || 0];
|
|
|
|
return this.getLine(fake + i);
|
2013-07-12 08:52:14 +00:00
|
|
|
};
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.prototype.clearLine = function(i) {
|
2013-07-23 19:10:46 +00:00
|
|
|
i = Math.min(i, this._clines.fake.length - 1);
|
2013-07-12 08:52:14 +00:00
|
|
|
return this.setLine(i, '');
|
|
|
|
};
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.prototype.clearBaseLine = function(i) {
|
|
|
|
var fake = this._clines.rtof[this.childBase || 0];
|
|
|
|
return this.clearLine(fake + i);
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.unshiftLine = function(line) {
|
2013-07-23 18:31:05 +00:00
|
|
|
return this.insertLine(0, line);
|
|
|
|
};
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.prototype.shiftLine = function(n) {
|
2013-07-23 18:31:05 +00:00
|
|
|
return this.deleteLine(0, n);
|
|
|
|
};
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.prototype.pushLine = function(line) {
|
2013-07-23 19:10:46 +00:00
|
|
|
return this.insertLine(this._clines.fake.length, line);
|
2013-07-23 18:31:05 +00:00
|
|
|
};
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.prototype.popLine = function(n) {
|
2013-07-23 19:10:46 +00:00
|
|
|
return this.deleteLine(this._clines.fake.length - 1, n);
|
2013-07-23 18:31:05 +00:00
|
|
|
};
|
|
|
|
|
2014-01-13 16:25:31 +00:00
|
|
|
Element.prototype.getLines = function() {
|
|
|
|
return this._clines.fake.slice();
|
|
|
|
};
|
|
|
|
|
|
|
|
Element.prototype.getScreenLines = function() {
|
|
|
|
return this._clines.slice();
|
|
|
|
};
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
/**
|
|
|
|
* Box
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Box(options) {
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-07-23 23:37:56 +00:00
|
|
|
return new Box(options);
|
|
|
|
}
|
|
|
|
options = options || {};
|
|
|
|
Element.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Box.prototype.__proto__ = Element.prototype;
|
|
|
|
|
|
|
|
Box.prototype.type = 'box';
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Text
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Text(options) {
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-07-12 08:34:32 +00:00
|
|
|
return new Text(options);
|
|
|
|
}
|
2013-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
2013-06-14 08:35:45 +00:00
|
|
|
options.shrink = true;
|
2013-07-23 23:37:56 +00:00
|
|
|
Element.call(this, options);
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 23:37:56 +00:00
|
|
|
Text.prototype.__proto__ = Element.prototype;
|
|
|
|
|
|
|
|
Text.prototype.type = 'text';
|
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-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-06-14 20:58:54 +00:00
|
|
|
return new Line(options);
|
|
|
|
}
|
|
|
|
|
2013-07-18 22:56:05 +00:00
|
|
|
options = 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-29 13:54:02 +00:00
|
|
|
Box.call(this, options);
|
|
|
|
|
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-07-29 13:54:02 +00:00
|
|
|
this.border = {
|
2013-06-06 09:03:25 +00:00
|
|
|
type: 'bg',
|
2013-07-29 13:54:02 +00:00
|
|
|
__proto__: this
|
2013-07-17 03:28:56 +00:00
|
|
|
};
|
|
|
|
|
2013-07-29 13:54:02 +00:00
|
|
|
this.style.border = this.style;
|
2013-06-06 09:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-07-21 10:31:24 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-06-14 20:58:54 +00:00
|
|
|
return new ScrollableBox(options);
|
|
|
|
}
|
2013-06-25 23:17:21 +00:00
|
|
|
|
2013-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
Box.call(this, options);
|
2013-06-25 23:17:21 +00:00
|
|
|
|
2013-07-25 01:41:11 +00:00
|
|
|
if (options.scrollable === false) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
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-19 06:57:17 +00:00
|
|
|
this.style.scrollbar = this.style.scrollbar || this.scrollbar.style;
|
|
|
|
if (!this.style.scrollbar) {
|
|
|
|
this.style.scrollbar = {};
|
|
|
|
this.style.scrollbar.fg = this.scrollbar.fg;
|
|
|
|
this.style.scrollbar.bg = this.scrollbar.bg;
|
|
|
|
this.style.scrollbar.bold = this.scrollbar.bold;
|
|
|
|
this.style.scrollbar.underline = this.scrollbar.underline;
|
|
|
|
this.style.scrollbar.inverse = this.scrollbar.inverse;
|
|
|
|
this.style.scrollbar.invisible = this.scrollbar.invisible;
|
|
|
|
}
|
2015-02-12 01:21:41 +00:00
|
|
|
//this.scrollbar.style = this.style.scrollbar;
|
2013-07-27 11:58:50 +00:00
|
|
|
if (this.track || this.scrollbar.track) {
|
|
|
|
this.track = this.scrollbar.track || this.track;
|
|
|
|
this.style.track = this.style.scrollbar.track || this.style.track;
|
|
|
|
this.track.ch = this.track.ch || ' ';
|
|
|
|
this.style.track = this.style.track || this.track.style;
|
|
|
|
if (!this.style.track) {
|
|
|
|
this.style.track = {};
|
|
|
|
this.style.track.fg = this.track.fg;
|
|
|
|
this.style.track.bg = this.track.bg;
|
|
|
|
this.style.track.bold = this.track.bold;
|
|
|
|
this.style.track.underline = this.track.underline;
|
|
|
|
this.style.track.inverse = this.track.inverse;
|
|
|
|
this.style.track.invisible = this.track.invisible;
|
|
|
|
}
|
|
|
|
this.track.style = this.style.track;
|
|
|
|
}
|
2013-06-25 23:17:21 +00:00
|
|
|
}
|
2013-07-21 10:31:24 +00:00
|
|
|
|
|
|
|
if (options.mouse) {
|
2013-08-01 00:52:25 +00:00
|
|
|
this.on('wheeldown', function(el, data) {
|
2013-07-21 10:31:24 +00:00
|
|
|
self.scroll(self.height / 2 | 0 || 1);
|
|
|
|
self.screen.render();
|
|
|
|
});
|
2013-08-01 00:52:25 +00:00
|
|
|
this.on('wheelup', function(el, data) {
|
2013-07-21 10:31:24 +00:00
|
|
|
self.scroll(-(self.height / 2 | 0) || -1);
|
|
|
|
self.screen.render();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.keys && !options.ignoreKeys) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'g' && !key.shift) {
|
2015-01-25 05:33:30 +00:00
|
|
|
self.scrollTo(0);
|
2013-07-21 10:31:24 +00:00
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'g' && key.shift) {
|
2015-01-25 05:33:30 +00:00
|
|
|
self.scrollTo(self._scrollBottom());
|
2013-07-21 10:31:24 +00:00
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.on('parsed content', function() {
|
|
|
|
self._recalculateIndex();
|
|
|
|
});
|
|
|
|
|
|
|
|
self._recalculateIndex();
|
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-07-19 06:57:17 +00:00
|
|
|
ScrollableBox.prototype._scrollBottom = function() {
|
2013-07-21 10:31:24 +00:00
|
|
|
if (!this.scrollable) return 0;
|
|
|
|
|
2013-07-19 06:57:17 +00:00
|
|
|
// We could just calculate the children, but we can
|
|
|
|
// optimize for lists by just returning the items.length.
|
2013-08-02 10:57:58 +00:00
|
|
|
if (this._isList) {
|
2013-07-23 18:31:05 +00:00
|
|
|
return this.items ? this.items.length : 0;
|
2013-07-19 06:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.lpos && this.lpos._scrollBottom) {
|
|
|
|
return this.lpos._scrollBottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
var bottom = this.children.reduce(function(current, el) {
|
2014-01-12 12:12:13 +00:00
|
|
|
// el.height alone does not calculate the shrunken height, we need to use
|
|
|
|
// getCoords. A shrunken box inside a scrollable element will not grow any
|
|
|
|
// larger than the scrollable element's context regardless of how much
|
|
|
|
// content is in the shrunken box, unless we do this (call getCoords
|
|
|
|
// without the scrollable calculation):
|
2014-01-12 12:19:54 +00:00
|
|
|
// See: $ node test/widget-shrink-fail-2.js
|
2014-01-12 11:08:16 +00:00
|
|
|
if (!el.detached) {
|
2014-01-12 12:12:13 +00:00
|
|
|
var lpos = el._getCoords(false, true);
|
|
|
|
if (lpos) {
|
|
|
|
return Math.max(current, el.rtop + (lpos.yl - lpos.yi));
|
2014-01-12 11:08:16 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-19 06:57:17 +00:00
|
|
|
return Math.max(current, el.rtop + el.height);
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
if (this.lpos) this.lpos._scrollBottom = bottom;
|
|
|
|
|
|
|
|
return bottom;
|
|
|
|
};
|
|
|
|
|
2013-08-05 01:25:10 +00:00
|
|
|
ScrollableBox.prototype.setScroll =
|
2015-01-16 18:16:00 +00:00
|
|
|
ScrollableBox.prototype.scrollTo = function(offset, always) {
|
2015-01-13 23:04:24 +00:00
|
|
|
// XXX
|
|
|
|
// At first, this appeared to account for the first new calculation of childBase:
|
2015-01-15 00:24:55 +00:00
|
|
|
this.scroll(0);
|
2015-01-16 18:16:00 +00:00
|
|
|
return this.scroll(offset - (this.childBase + this.childOffset), always);
|
2013-07-19 06:57:17 +00:00
|
|
|
};
|
|
|
|
|
2013-08-05 01:25:10 +00:00
|
|
|
ScrollableBox.prototype.getScroll = function() {
|
|
|
|
return this.childBase + this.childOffset;
|
|
|
|
};
|
|
|
|
|
2013-07-23 12:36:27 +00:00
|
|
|
ScrollableBox.prototype.scroll = function(offset, always) {
|
2013-07-21 10:31:24 +00:00
|
|
|
if (!this.scrollable) return;
|
|
|
|
|
2013-08-13 18:01:31 +00:00
|
|
|
if (this.detached) return;
|
|
|
|
|
2013-07-21 10:31:24 +00:00
|
|
|
// Handle scrolling.
|
2013-07-19 07:28:31 +00:00
|
|
|
var visible = this.height - this.iheight
|
2013-07-14 16:04:48 +00:00
|
|
|
, base = this.childBase
|
|
|
|
, d
|
|
|
|
, p
|
|
|
|
, t
|
2013-07-23 20:26:57 +00:00
|
|
|
, b
|
|
|
|
, max
|
2013-07-25 02:21:20 +00:00
|
|
|
, emax;
|
2013-07-12 01:48:30 +00:00
|
|
|
|
2013-07-23 12:36:27 +00:00
|
|
|
if (this.alwaysScroll || always) {
|
2013-06-06 09:03:25 +00:00
|
|
|
// 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-21 10:31:24 +00:00
|
|
|
// Find max "bottom" value for
|
|
|
|
// content and descendant elements.
|
|
|
|
// Scroll the content if necessary.
|
2013-07-23 20:26:57 +00:00
|
|
|
if (this.childBase === base) {
|
2013-07-21 10:31:24 +00:00
|
|
|
return this.emit('scroll');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
this.parseContent();
|
|
|
|
|
|
|
|
max = this._clines.length - (this.height - this.iheight);
|
|
|
|
if (max < 0) max = 0;
|
|
|
|
emax = this._scrollBottom() - (this.height - this.iheight);
|
2013-07-23 15:38:54 +00:00
|
|
|
if (emax < 0) emax = 0;
|
2013-07-21 10:31:24 +00:00
|
|
|
|
|
|
|
this.childBase = Math.min(this.childBase, Math.max(emax, max));
|
|
|
|
|
|
|
|
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.
|
2013-07-17 09:38:11 +00:00
|
|
|
p = this.lpos;
|
2013-07-23 20:26:57 +00:00
|
|
|
// Only really need _getCoords() if we want
|
|
|
|
// to allow nestable scrolling elements...
|
|
|
|
// or if we **really** want shrinkable
|
|
|
|
// scrolling elements.
|
|
|
|
// p = this._getCoords();
|
2013-08-13 18:02:02 +00:00
|
|
|
if (p && this.childBase !== base && this.screen.cleanSides(this)) {
|
2013-07-19 07:28:31 +00:00
|
|
|
t = p.yi + this.itop;
|
|
|
|
b = p.yl - this.ibottom - 1;
|
2013-07-14 16:04:48 +00:00
|
|
|
d = this.childBase - base;
|
|
|
|
|
|
|
|
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-21 10:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.emit('scroll');
|
|
|
|
};
|
|
|
|
|
|
|
|
ScrollableBox.prototype._recalculateIndex = function() {
|
2013-07-25 02:21:20 +00:00
|
|
|
var max, emax;
|
2013-07-21 10:31:24 +00:00
|
|
|
|
2013-07-23 15:38:54 +00:00
|
|
|
if (this.detached || !this.scrollable) {
|
2013-07-21 10:31:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
max = this._clines.length - (this.height - this.iheight);
|
|
|
|
if (max < 0) max = 0;
|
|
|
|
emax = this._scrollBottom() - (this.height - this.iheight);
|
2013-07-23 15:38:54 +00:00
|
|
|
if (emax < 0) emax = 0;
|
2013-07-21 10:31:24 +00:00
|
|
|
|
|
|
|
this.childBase = Math.min(this.childBase, Math.max(emax, max));
|
|
|
|
|
|
|
|
if (this.childBase < 0) {
|
|
|
|
this.childBase = 0;
|
|
|
|
} else if (this.childBase > this.baseLimit) {
|
|
|
|
this.childBase = this.baseLimit;
|
|
|
|
}
|
2013-06-06 09:03:25 +00:00
|
|
|
};
|
|
|
|
|
2013-06-25 22:31:13 +00:00
|
|
|
ScrollableBox.prototype.resetScroll = function() {
|
2013-07-25 01:41:11 +00:00
|
|
|
if (!this.scrollable) return;
|
2013-06-25 22:31:13 +00:00
|
|
|
this.childOffset = 0;
|
|
|
|
this.childBase = 0;
|
2013-07-21 10:31:24 +00:00
|
|
|
return this.emit('scroll');
|
2013-06-25 22:31:13 +00:00
|
|
|
};
|
|
|
|
|
2014-01-13 16:25:31 +00:00
|
|
|
ScrollableBox.prototype.getScrollHeight = function() {
|
2013-08-20 18:34:18 +00:00
|
|
|
return Math.max(this._clines.length, this._scrollBottom());
|
|
|
|
};
|
|
|
|
|
2013-08-05 01:25:10 +00:00
|
|
|
ScrollableBox.prototype.getScrollPerc = function(s) {
|
|
|
|
var pos = this.lpos || this._getCoords();
|
|
|
|
if (!pos) return s ? -1 : 0;
|
|
|
|
|
|
|
|
var height = (pos.yl - pos.yi) - this.iheight
|
2013-08-20 18:34:18 +00:00
|
|
|
, i = this.getScrollHeight()
|
2013-08-05 01:25:10 +00:00
|
|
|
, p;
|
|
|
|
|
|
|
|
if (height < i) {
|
|
|
|
if (this.alwaysScroll) {
|
|
|
|
p = this.childBase / (i - height);
|
|
|
|
} else {
|
|
|
|
p = (this.childBase + this.childOffset) / (i - 1);
|
|
|
|
}
|
|
|
|
return p * 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s ? -1 : 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
ScrollableBox.prototype.setScrollPerc = function(i) {
|
|
|
|
var m = Math.max(this._clines.length, this._scrollBottom());
|
|
|
|
return this.scrollTo((i / 100) * m | 0);
|
|
|
|
};
|
|
|
|
|
2013-07-29 13:54:02 +00:00
|
|
|
/**
|
|
|
|
* ScrollableText
|
|
|
|
*/
|
|
|
|
|
|
|
|
function ScrollableText(options) {
|
|
|
|
if (!(this instanceof Node)) {
|
|
|
|
return new ScrollableText(options);
|
|
|
|
}
|
|
|
|
options = options || {};
|
|
|
|
options.scrollable = true;
|
|
|
|
options.alwaysScroll = true;
|
|
|
|
Box.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
ScrollableText.prototype.__proto__ = Box.prototype;
|
|
|
|
|
|
|
|
ScrollableText.prototype.type = 'scrollable-text';
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* List
|
|
|
|
*/
|
|
|
|
|
|
|
|
function List(options) {
|
2013-06-06 09:03:25 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-06-14 20:58:54 +00:00
|
|
|
return new List(options);
|
|
|
|
}
|
|
|
|
|
2013-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
|
|
|
|
2013-07-21 10:31:24 +00:00
|
|
|
options.ignoreKeys = true;
|
2013-07-23 18:31:05 +00:00
|
|
|
// Possibly put this here: this.items = [];
|
2013-07-25 06:20:47 +00:00
|
|
|
options.scrollable = true;
|
|
|
|
Box.call(this, options);
|
2013-06-01 10:47:18 +00:00
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
this.value = '';
|
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-08-02 10:57:58 +00:00
|
|
|
this._isList = true;
|
2013-06-01 02:09:07 +00:00
|
|
|
|
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-07-17 17:01:54 +00:00
|
|
|
if (this.options.itemHoverBg) {
|
|
|
|
this.options.itemHoverEffects = { bg: this.options.itemHoverBg };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.options.itemHoverEffects) {
|
|
|
|
this.style.item.hover = this.options.itemHoverEffects;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.options.itemFocusEffects) {
|
|
|
|
this.style.item.focus = this.options.itemFocusEffects;
|
|
|
|
}
|
|
|
|
|
2015-02-10 04:11:02 +00:00
|
|
|
this.interactive = options.interactive !== false;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-08-27 16:10:09 +00:00
|
|
|
this.select(0);
|
2013-06-06 15:27:19 +00:00
|
|
|
|
2013-07-21 10:31:24 +00:00
|
|
|
if (options.mouse) {
|
2013-08-01 00:52:25 +00:00
|
|
|
this.screen._listenMouse(this);
|
|
|
|
this.on('element wheeldown', function(el, data) {
|
2013-06-06 15:27:19 +00:00
|
|
|
self.select(self.selected + 2);
|
|
|
|
self.screen.render();
|
|
|
|
});
|
2013-08-01 00:52:25 +00:00
|
|
|
this.on('element wheelup', function(el, 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-18 19:52:56 +00:00
|
|
|
if (key.name === 'enter'
|
|
|
|
|| (options.vi && key.name === 'l' && !key.shift)) {
|
2014-06-02 04:43:02 +00:00
|
|
|
self.enterSelected();
|
2013-06-18 15:52:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (key.name === 'escape' || (options.vi && key.name === 'q')) {
|
2014-06-02 04:43:02 +00:00
|
|
|
self.cancelSelected();
|
2013-06-18 15:52:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'u' && key.ctrl) {
|
2013-07-19 14:17:21 +00:00
|
|
|
self.move(-((self.height - self.iheight) / 2) | 0);
|
2013-06-18 15:52:48 +00:00
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'd' && key.ctrl) {
|
2013-07-19 14:17:21 +00:00
|
|
|
self.move((self.height - self.iheight) / 2 | 0);
|
2013-06-18 15:52:48 +00:00
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'b' && key.ctrl) {
|
2013-07-19 14:17:21 +00:00
|
|
|
self.move(-(self.height - self.iheight));
|
2013-06-18 15:52:48 +00:00
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.vi && key.name === 'f' && key.ctrl) {
|
2013-07-19 14:17:21 +00:00
|
|
|
self.move(self.height - self.iheight);
|
2013-06-18 15:52:48 +00:00
|
|
|
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) {
|
2013-07-18 19:52:56 +00:00
|
|
|
// TODO: Maybe use Math.min(this.items.length,
|
|
|
|
// ... for calculating visible items elsewhere.
|
|
|
|
var visible = Math.min(
|
2013-07-19 14:17:21 +00:00
|
|
|
self.height - self.iheight,
|
2013-07-18 19:52:56 +00:00
|
|
|
self.items.length) / 2 | 0;
|
|
|
|
self.move(self.childBase + visible - self.selected);
|
2013-06-18 15:52:48 +00:00
|
|
|
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
|
2013-07-19 14:17:21 +00:00
|
|
|
+ Math.min(self.height - self.iheight, self.items.length)
|
2013-06-18 15:52:48 +00:00
|
|
|
- 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;
|
|
|
|
}
|
2014-05-06 21:40:19 +00:00
|
|
|
|
|
|
|
if (options.vi && key.ch === '/') {
|
|
|
|
if (typeof self.options.search !== 'function') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-13 23:04:24 +00:00
|
|
|
return self.options.search(function(searchString) {
|
2014-05-07 11:55:53 +00:00
|
|
|
self.select(self.fuzzyFind(searchString));
|
2014-05-06 21:40:19 +00:00
|
|
|
self.screen.render();
|
|
|
|
})
|
|
|
|
}
|
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-07-19 07:28:31 +00:00
|
|
|
var visible = self.height - self.iheight;
|
2015-01-25 05:33:30 +00:00
|
|
|
// if (self.selected < visible - 1) {
|
2013-06-14 08:28:59 +00:00
|
|
|
if (visible >= self.selected + 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-07-27 10:34:59 +00:00
|
|
|
|
|
|
|
this.on('adopt', function(el) {
|
|
|
|
if (!~self.items.indexOf(el)) {
|
|
|
|
el.fixed = true;
|
|
|
|
}
|
|
|
|
});
|
2013-08-27 15:22:19 +00:00
|
|
|
|
|
|
|
// Ensure children are removed from the
|
|
|
|
// item list if they are items.
|
|
|
|
this.on('remove', function(el) {
|
|
|
|
self.removeItem(el);
|
|
|
|
});
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
2013-07-25 06:20:47 +00:00
|
|
|
List.prototype.__proto__ = Box.prototype;
|
2013-06-01 02:09:07 +00:00
|
|
|
|
2013-06-25 11:34:10 +00:00
|
|
|
List.prototype.type = 'list';
|
|
|
|
|
2013-08-27 15:22:19 +00:00
|
|
|
List.prototype.add =
|
|
|
|
List.prototype.addItem =
|
|
|
|
List.prototype.appendItem = function(item) {
|
2013-06-06 09:03:25 +00:00
|
|
|
var self = this;
|
|
|
|
|
2014-01-16 21:02:04 +00:00
|
|
|
this.ritems.push(item);
|
|
|
|
|
2013-08-01 06:52:28 +00:00
|
|
|
// Note: Could potentially use Button here.
|
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',
|
2015-01-16 18:55:24 +00:00
|
|
|
top: this.items.length,
|
|
|
|
left: 0,
|
|
|
|
right: (this.scrollbar ? 1 : 0),
|
2013-07-12 08:34:32 +00:00
|
|
|
tags: this.parseTags,
|
2013-06-29 17:07:51 +00:00
|
|
|
height: 1,
|
2013-07-17 17:01:54 +00:00
|
|
|
hoverEffects: this.mouse ? this.style.item.hover : null,
|
2013-08-01 06:52:28 +00:00
|
|
|
focusEffects: this.mouse ? this.style.item.focus : null,
|
|
|
|
autoFocus: false
|
2013-07-16 05:52:13 +00:00
|
|
|
};
|
|
|
|
|
2015-01-16 18:55:24 +00:00
|
|
|
if (!this.screen.autoPadding) {
|
|
|
|
options.top = this.itop + this.items.length;
|
|
|
|
options.left = this.ileft;
|
|
|
|
options.right = this.iright + (this.scrollbar ? 1 : 0);
|
|
|
|
}
|
2013-07-30 13:50:49 +00:00
|
|
|
|
2013-07-16 05:52:13 +00:00
|
|
|
['bg', 'fg', 'bold', 'underline',
|
|
|
|
'blink', 'inverse', 'invisible'].forEach(function(name) {
|
|
|
|
options[name] = function() {
|
2015-02-10 04:11:02 +00:00
|
|
|
var attr = self.items[self.selected] === item && self.interactive
|
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.items.push(item);
|
2013-07-27 10:34:59 +00:00
|
|
|
this.append(item);
|
2013-06-06 09:03:25 +00:00
|
|
|
|
2013-08-27 16:10:09 +00:00
|
|
|
if (this.items.length === 1) {
|
|
|
|
this.select(0);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2014-06-11 08:03:32 +00:00
|
|
|
List.prototype.fuzzyFind = function(search) {
|
2014-05-07 11:55:53 +00:00
|
|
|
var index = this.getItemIndex(this.selected);
|
|
|
|
|
|
|
|
for (var i = 0; i < this.ritems.length; i++){
|
2014-06-11 08:03:32 +00:00
|
|
|
if (this.ritems[i].indexOf(search) === 0) {
|
2014-05-07 11:55:53 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2014-06-11 08:03:32 +00:00
|
|
|
|
2014-05-07 11:55:53 +00:00
|
|
|
return index;
|
2014-06-11 08:03:32 +00:00
|
|
|
};
|
2014-05-07 11:55:53 +00:00
|
|
|
|
2014-04-26 06:05:25 +00:00
|
|
|
List.prototype.getItemIndex = function(child) {
|
|
|
|
if (typeof child === 'number') {
|
|
|
|
return child;
|
|
|
|
} else if (typeof child === 'string') {
|
|
|
|
return this.ritems.indexOf(child);
|
|
|
|
} else {
|
|
|
|
return this.items.indexOf(child);
|
|
|
|
}
|
|
|
|
};
|
2013-08-27 15:22:19 +00:00
|
|
|
|
2014-04-26 06:05:25 +00:00
|
|
|
List.prototype.getItem = function(child) {
|
|
|
|
return this.items[this.getItemIndex(child)];
|
|
|
|
};
|
|
|
|
|
|
|
|
List.prototype.removeItem = function(child) {
|
|
|
|
var i = this.getItemIndex(child);
|
2013-08-27 15:22:19 +00:00
|
|
|
if (~i && this.items[i]) {
|
|
|
|
child = this.items.splice(i, 1)[0];
|
|
|
|
this.ritems.splice(i, 1);
|
|
|
|
this.remove(child);
|
2013-08-27 16:10:09 +00:00
|
|
|
if (i === this.selected) {
|
|
|
|
this.select(i - 1);
|
|
|
|
}
|
2013-06-01 10:47:18 +00:00
|
|
|
}
|
2013-06-01 02:09:07 +00:00
|
|
|
};
|
|
|
|
|
2014-04-26 05:55:33 +00:00
|
|
|
List.prototype.clearItems = function() {
|
|
|
|
return this.setItems([]);
|
|
|
|
};
|
|
|
|
|
2013-06-18 15:52:48 +00:00
|
|
|
List.prototype.setItems = function(items) {
|
2014-01-25 22:31:15 +00:00
|
|
|
var items = items.slice()
|
2013-06-25 11:34:10 +00:00
|
|
|
, original = this.items.slice()
|
2014-01-19 07:20:37 +00:00
|
|
|
, selected = this.selected
|
2014-01-25 22:31:15 +00:00
|
|
|
, sel = this.ritems[this.selected]
|
|
|
|
, i = 0;
|
2013-06-25 11:34:10 +00:00
|
|
|
|
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
|
|
|
|
2014-01-16 21:02:04 +00:00
|
|
|
this.ritems = items;
|
|
|
|
|
2013-06-25 11:34:10 +00:00
|
|
|
// Try to find our old item if it still exists.
|
|
|
|
sel = items.indexOf(sel);
|
2014-01-19 07:20:37 +00:00
|
|
|
if (~sel) {
|
|
|
|
this.select(sel);
|
|
|
|
} else if (items.length === original.length) {
|
|
|
|
this.select(selected);
|
|
|
|
} else {
|
|
|
|
this.select(Math.min(selected, items.length - 1));
|
|
|
|
}
|
2013-06-18 15:52:48 +00:00
|
|
|
};
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
List.prototype.select = function(index) {
|
2015-02-10 04:11:02 +00:00
|
|
|
if (!this.interactive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-27 16:10:09 +00:00
|
|
|
if (!this.items.length) {
|
|
|
|
this.selected = 0;
|
|
|
|
this.value = '';
|
|
|
|
this.scrollTo(0);
|
|
|
|
return;
|
|
|
|
}
|
2013-06-01 10:47:18 +00:00
|
|
|
|
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
|
|
|
|
2015-01-25 05:33:30 +00:00
|
|
|
if (index < 0) {
|
|
|
|
index = 0;
|
|
|
|
} else if (index >= this.items.length) {
|
|
|
|
index = this.items.length - 1;
|
|
|
|
}
|
2013-06-01 10:47:18 +00:00
|
|
|
|
|
|
|
if (this.selected === index && this._listInitialized) return;
|
|
|
|
this._listInitialized = true;
|
|
|
|
|
2013-06-06 09:03:25 +00:00
|
|
|
this.selected = index;
|
2013-07-17 05:53:34 +00:00
|
|
|
this.value = this.ritems[this.selected];
|
2013-07-19 06:57:17 +00:00
|
|
|
this.scrollTo(this.selected);
|
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);
|
|
|
|
};
|
|
|
|
|
2014-06-02 12:52:40 +00:00
|
|
|
List.prototype.pick = function(label, callback) {
|
|
|
|
if (!callback) {
|
|
|
|
callback = label;
|
|
|
|
label = null;
|
|
|
|
}
|
|
|
|
|
2015-02-10 04:11:02 +00:00
|
|
|
if (!this.interactive) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
2014-01-12 14:58:35 +00:00
|
|
|
var self = this;
|
|
|
|
var focused = this.screen.focused;
|
|
|
|
if (focused && focused._done) focused._done('stop');
|
|
|
|
this.screen.saveFocus();
|
2014-01-13 16:25:31 +00:00
|
|
|
|
2015-01-25 05:33:30 +00:00
|
|
|
// XXX Keep above:
|
|
|
|
// var parent = this.parent;
|
|
|
|
// this.detach();
|
|
|
|
// parent.append(this);
|
2014-01-13 16:25:31 +00:00
|
|
|
|
2014-01-12 14:58:35 +00:00
|
|
|
this.focus();
|
|
|
|
this.show();
|
2014-06-02 02:29:16 +00:00
|
|
|
this.select(0);
|
2014-06-02 12:52:40 +00:00
|
|
|
if (label) this.setLabel(label);
|
2014-01-12 14:58:35 +00:00
|
|
|
this.screen.render();
|
|
|
|
this.once('action', function(el, selected) {
|
2014-06-02 12:52:40 +00:00
|
|
|
if (label) self.removeLabel();
|
2014-01-12 14:58:35 +00:00
|
|
|
self.screen.restoreFocus();
|
|
|
|
self.hide();
|
|
|
|
self.screen.render();
|
|
|
|
if (!el) return callback();
|
|
|
|
return callback(null, self.ritems[selected]);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-06-02 04:43:02 +00:00
|
|
|
List.prototype.enterSelected = function(i) {
|
|
|
|
if (i != null) this.select(i);
|
|
|
|
this.emit('action', this.items[this.selected], this.selected);
|
|
|
|
this.emit('select', this.items[this.selected], this.selected);
|
|
|
|
};
|
|
|
|
|
|
|
|
List.prototype.cancelSelected = function(i) {
|
|
|
|
if (i != null) this.select(i);
|
|
|
|
this.emit('action');
|
|
|
|
this.emit('cancel');
|
|
|
|
};
|
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
/**
|
|
|
|
* Form
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Form(options) {
|
|
|
|
var self = this;
|
|
|
|
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-07-17 05:53:34 +00:00
|
|
|
return new Form(options);
|
|
|
|
}
|
|
|
|
|
2013-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
|
|
|
|
2013-07-21 10:31:24 +00:00
|
|
|
options.ignoreKeys = true;
|
2013-07-24 20:39:22 +00:00
|
|
|
Box.call(this, options);
|
2013-07-17 05:53:34 +00:00
|
|
|
|
|
|
|
if (options.keys) {
|
|
|
|
this.screen._listenKeys(this);
|
2013-08-01 00:52:25 +00:00
|
|
|
this.on('element keypress', function(el, ch, key) {
|
2013-07-18 19:52:56 +00:00
|
|
|
if ((key.name === 'tab' && !key.shift)
|
2014-01-13 18:38:50 +00:00
|
|
|
|| (el.type === 'textbox' && options.autoNext && key.name === 'enter')
|
2013-07-18 19:52:56 +00:00
|
|
|
|| key.name === 'down'
|
|
|
|
|| (options.vi && key.name === 'j')) {
|
2013-07-19 03:49:06 +00:00
|
|
|
if (el.type === 'textbox' || el.type === 'textarea') {
|
|
|
|
if (key.name === 'j') return;
|
|
|
|
if (key.name === 'tab') {
|
|
|
|
// Workaround, since we can't stop the tab from being added.
|
|
|
|
el.emit('keypress', null, { name: 'backspace' });
|
|
|
|
}
|
2013-07-19 14:17:21 +00:00
|
|
|
el.emit('keypress', '\x1b', { name: 'escape' });
|
2013-07-19 03:49:06 +00:00
|
|
|
}
|
2013-07-17 05:53:34 +00:00
|
|
|
self.focusNext();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-18 19:52:56 +00:00
|
|
|
if ((key.name === 'tab' && key.shift)
|
|
|
|
|| key.name === 'up'
|
|
|
|
|| (options.vi && key.name === 'k')) {
|
2013-07-19 03:49:06 +00:00
|
|
|
if (el.type === 'textbox' || el.type === 'textarea') {
|
|
|
|
if (key.name === 'k') return;
|
2013-07-19 14:17:21 +00:00
|
|
|
el.emit('keypress', '\x1b', { name: 'escape' });
|
2013-07-19 03:49:06 +00:00
|
|
|
}
|
2013-07-17 05:53:34 +00:00
|
|
|
self.focusPrevious();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key.name === 'escape') {
|
|
|
|
self.focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-25 06:20:47 +00:00
|
|
|
Form.prototype.__proto__ = Box.prototype;
|
2013-07-17 05:53:34 +00:00
|
|
|
|
|
|
|
Form.prototype.type = 'form';
|
|
|
|
|
|
|
|
Form.prototype._refresh = function() {
|
2014-03-30 08:56:38 +00:00
|
|
|
// XXX Possibly remove this if statement and refresh on every focus.
|
|
|
|
// Also potentially only include *visible* focusable elements.
|
|
|
|
// This would remove the need to check for _selected.visible in previous()
|
|
|
|
// and next().
|
|
|
|
if (!this._children) {
|
|
|
|
var out = [];
|
|
|
|
|
|
|
|
this.children.forEach(function fn(el) {
|
|
|
|
if (el.keyable) out.push(el);
|
|
|
|
el.children.forEach(fn);
|
|
|
|
});
|
2013-07-17 05:53:34 +00:00
|
|
|
|
2014-03-30 08:56:38 +00:00
|
|
|
this._children = out;
|
|
|
|
}
|
|
|
|
};
|
2013-07-17 05:53:34 +00:00
|
|
|
|
2014-03-30 08:56:38 +00:00
|
|
|
Form.prototype._visible = function() {
|
|
|
|
return !!this._children.filter(function(el) {
|
|
|
|
return el.visible;
|
|
|
|
}).length;
|
2014-03-30 08:51:54 +00:00
|
|
|
};
|
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
Form.prototype.next = function() {
|
|
|
|
this._refresh();
|
|
|
|
|
2014-03-30 08:56:38 +00:00
|
|
|
if (!this._visible()) return;
|
2014-03-30 08:51:54 +00:00
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
if (!this._selected) {
|
2014-01-13 18:38:50 +00:00
|
|
|
this._selected = this._children[0];
|
2014-03-30 08:56:38 +00:00
|
|
|
if (!this._selected.visible) return this.next();
|
2014-01-13 18:38:50 +00:00
|
|
|
if (this.screen.focused !== this._selected) return this._selected;
|
2013-07-17 05:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var i = this._children.indexOf(this._selected);
|
|
|
|
if (!~i || !this._children[i + 1]) {
|
2014-01-17 00:08:59 +00:00
|
|
|
this._selected = this._children[0];
|
2014-03-30 08:56:38 +00:00
|
|
|
if (!this._selected.visible) return this.next();
|
2014-01-17 00:08:59 +00:00
|
|
|
return this._selected;
|
2013-07-17 05:53:34 +00:00
|
|
|
}
|
|
|
|
|
2014-01-17 00:08:59 +00:00
|
|
|
this._selected = this._children[i + 1];
|
2014-03-30 08:56:38 +00:00
|
|
|
if (!this._selected.visible) return this.next();
|
2014-01-17 00:08:59 +00:00
|
|
|
return this._selected;
|
2013-07-17 05:53:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Form.prototype.previous = function() {
|
|
|
|
this._refresh();
|
|
|
|
|
2014-03-30 08:56:38 +00:00
|
|
|
if (!this._visible()) return;
|
2014-03-30 08:51:54 +00:00
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
if (!this._selected) {
|
2014-01-13 18:38:50 +00:00
|
|
|
this._selected = this._children[this._children.length - 1];
|
2014-03-30 08:56:38 +00:00
|
|
|
if (!this._selected.visible) return this.previous();
|
2014-01-13 18:38:50 +00:00
|
|
|
if (this.screen.focused !== this._selected) return this._selected;
|
2013-07-17 05:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var i = this._children.indexOf(this._selected);
|
|
|
|
if (!~i || !this._children[i - 1]) {
|
2014-01-17 00:08:59 +00:00
|
|
|
this._selected = this._children[this._children.length - 1];
|
2014-03-30 08:56:38 +00:00
|
|
|
if (!this._selected.visible) return this.previous();
|
2014-01-17 00:08:59 +00:00
|
|
|
return this._selected;
|
2013-07-17 05:53:34 +00:00
|
|
|
}
|
|
|
|
|
2014-01-17 00:08:59 +00:00
|
|
|
this._selected = this._children[i - 1];
|
2014-03-30 08:56:38 +00:00
|
|
|
if (!this._selected.visible) return this.previous();
|
2014-01-17 00:08:59 +00:00
|
|
|
return this._selected;
|
2013-07-17 05:53:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Form.prototype.focusNext = function() {
|
2014-03-30 08:51:54 +00:00
|
|
|
var next = this.next();
|
|
|
|
if (next) next.focus();
|
2013-07-17 05:53:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Form.prototype.focusPrevious = function() {
|
2014-03-30 08:51:54 +00:00
|
|
|
var previous = this.previous();
|
|
|
|
if (previous) previous.focus();
|
2013-07-17 05:53:34 +00:00
|
|
|
};
|
|
|
|
|
2014-01-17 05:54:19 +00:00
|
|
|
Form.prototype.resetSelected = function() {
|
|
|
|
this._selected = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
Form.prototype.focusFirst = function() {
|
|
|
|
this.resetSelected();
|
2014-03-30 08:51:54 +00:00
|
|
|
this.focusNext();
|
2014-01-17 05:54:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Form.prototype.focusLast = function() {
|
|
|
|
this.resetSelected();
|
2014-03-30 08:51:54 +00:00
|
|
|
this.focusPrevious();
|
2014-01-17 05:54:19 +00:00
|
|
|
};
|
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
Form.prototype.submit = function() {
|
|
|
|
var self = this
|
|
|
|
, out = {};
|
|
|
|
|
2013-07-18 19:33:00 +00:00
|
|
|
this.children.forEach(function fn(el) {
|
2013-07-17 05:53:34 +00:00
|
|
|
if (el.value != null) {
|
|
|
|
var name = el.name || el.type;
|
|
|
|
if (Array.isArray(out[name])) {
|
|
|
|
out[name].push(el.value);
|
|
|
|
} else if (out[name]) {
|
|
|
|
out[name] = [out[name], el.value];
|
|
|
|
} else {
|
|
|
|
out[name] = el.value;
|
|
|
|
}
|
|
|
|
}
|
2013-07-18 19:33:00 +00:00
|
|
|
el.children.forEach(fn);
|
|
|
|
});
|
2013-07-17 05:53:34 +00:00
|
|
|
|
|
|
|
this.emit('submit', out);
|
|
|
|
|
|
|
|
return this.submission = out;
|
|
|
|
};
|
|
|
|
|
|
|
|
Form.prototype.cancel = function() {
|
|
|
|
this.emit('cancel');
|
|
|
|
};
|
|
|
|
|
|
|
|
Form.prototype.reset = function() {
|
2013-07-18 19:33:00 +00:00
|
|
|
this.children.forEach(function fn(el) {
|
2013-07-17 05:53:34 +00:00
|
|
|
switch (el.type) {
|
|
|
|
case 'screen':
|
|
|
|
break;
|
|
|
|
case 'box':
|
|
|
|
break;
|
|
|
|
case 'text':
|
|
|
|
break;
|
|
|
|
case 'line':
|
|
|
|
break;
|
|
|
|
case 'scrollable-box':
|
|
|
|
break;
|
|
|
|
case 'list':
|
|
|
|
el.select(0);
|
|
|
|
return;
|
|
|
|
case 'form':
|
|
|
|
break;
|
|
|
|
case 'input':
|
|
|
|
break;
|
|
|
|
case 'textbox':
|
|
|
|
el.clearInput();
|
|
|
|
return;
|
|
|
|
case 'textarea':
|
|
|
|
el.clearInput();
|
|
|
|
return;
|
|
|
|
case 'button':
|
2013-08-13 15:54:17 +00:00
|
|
|
delete el.value;
|
2013-07-17 05:53:34 +00:00
|
|
|
break;
|
|
|
|
case 'progress-bar':
|
|
|
|
el.setProgress(0);
|
|
|
|
break;
|
|
|
|
case 'file-manager':
|
|
|
|
el.refresh(el.options.cwd);
|
|
|
|
return;
|
|
|
|
case 'checkbox':
|
|
|
|
el.uncheck();
|
|
|
|
return;
|
|
|
|
case 'radio-set':
|
|
|
|
break;
|
|
|
|
case 'radio-button':
|
|
|
|
el.uncheck();
|
|
|
|
return;
|
|
|
|
case 'prompt':
|
|
|
|
break;
|
|
|
|
case 'question':
|
|
|
|
break;
|
|
|
|
case 'message':
|
|
|
|
break;
|
|
|
|
case 'info':
|
|
|
|
break;
|
|
|
|
case 'loading':
|
|
|
|
break;
|
|
|
|
case 'list-bar':
|
|
|
|
//el.select(0);
|
|
|
|
break;
|
|
|
|
case 'dir-manager':
|
|
|
|
el.refresh(el.options.cwd);
|
|
|
|
return;
|
2015-01-28 04:39:00 +00:00
|
|
|
case 'terminal':
|
|
|
|
el.write('');
|
2013-07-17 05:53:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-07-18 19:33:00 +00:00
|
|
|
el.children.forEach(fn);
|
|
|
|
});
|
2013-07-17 05:53:34 +00:00
|
|
|
|
|
|
|
this.emit('reset');
|
|
|
|
};
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Input
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Input(options) {
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-06-14 20:58:54 +00:00
|
|
|
return new Input(options);
|
|
|
|
}
|
2013-07-18 22:56:05 +00:00
|
|
|
options = 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
|
|
|
/**
|
2013-07-25 01:41:11 +00:00
|
|
|
* Textarea
|
2013-06-01 02:09:07 +00:00
|
|
|
*/
|
|
|
|
|
2013-07-25 01:41:11 +00:00
|
|
|
function Textarea(options) {
|
2013-07-29 20:49:25 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-07-25 01:41:11 +00:00
|
|
|
return new Textarea(options);
|
2013-06-14 20:58:54 +00:00
|
|
|
}
|
2013-06-25 22:31:13 +00:00
|
|
|
|
2013-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
|
|
|
|
2013-07-25 01:41:11 +00:00
|
|
|
options.scrollable = options.scrollable !== false;
|
|
|
|
|
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-25 11:34:10 +00:00
|
|
|
|
2013-07-29 13:54:02 +00:00
|
|
|
this.__updateCursor = this._updateCursor.bind(this);
|
2013-07-19 14:17:21 +00:00
|
|
|
this.on('resize', this.__updateCursor);
|
|
|
|
this.on('move', this.__updateCursor);
|
2013-07-29 20:49:25 +00:00
|
|
|
|
|
|
|
if (options.inputOnFocus) {
|
|
|
|
this.on('focus', this.readInput.bind(this, null));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.inputOnFocus && options.keys) {
|
|
|
|
this.on('keypress', function(ch, key) {
|
|
|
|
if (self._reading) return;
|
|
|
|
if (key.name === 'enter' || (options.vi && key.name === 'i')) {
|
|
|
|
return self.readInput();
|
|
|
|
}
|
|
|
|
if (key.name === 'e') {
|
|
|
|
return self.readEditor();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.mouse) {
|
|
|
|
this.on('click', function(data) {
|
|
|
|
if (self._reading) return;
|
|
|
|
if (data.button !== 'right') return;
|
|
|
|
self.readEditor();
|
|
|
|
});
|
|
|
|
}
|
2013-06-01 02:09:07 +00:00
|
|
|
}
|
|
|
|
|
2013-07-25 01:41:11 +00:00
|
|
|
Textarea.prototype.__proto__ = Input.prototype;
|
2013-06-01 02:09:07 +00:00
|
|
|
|
2013-07-25 01:41:11 +00:00
|
|
|
Textarea.prototype.type = 'textarea';
|
2013-06-25 11:34:10 +00:00
|
|
|
|
2014-01-11 22:52:33 +00:00
|
|
|
Textarea.prototype._updateCursor = function(get) {
|
2013-07-25 01:41:11 +00:00
|
|
|
if (this.screen.focused !== this) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-11 22:52:33 +00:00
|
|
|
var lpos = get ? this.lpos : this._getCoords();
|
2013-07-25 01:41:11 +00:00
|
|
|
if (!lpos) return;
|
|
|
|
|
|
|
|
var last = this._clines[this._clines.length-1]
|
|
|
|
, program = this.screen.program
|
|
|
|
, line
|
|
|
|
, cx
|
|
|
|
, cy;
|
|
|
|
|
|
|
|
// Stop a situation where the textarea begins scrolling
|
|
|
|
// and the last cline appears to always be empty from the
|
|
|
|
// _typeScroll `+ '\n'` thing.
|
|
|
|
// Maybe not necessary anymore?
|
|
|
|
if (last === '' && this.value[this.value.length-1] !== '\n') {
|
|
|
|
last = this._clines[this._clines.length-2] || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
line = Math.min(
|
|
|
|
this._clines.length - 1 - (this.childBase || 0),
|
|
|
|
(lpos.yl - lpos.yi) - this.iheight - 1);
|
|
|
|
|
2014-01-05 07:01:21 +00:00
|
|
|
// When calling clearValue() on a full textarea with a border, the first
|
|
|
|
// argument in the above Math.min call ends up being -2. Make sure we stay
|
|
|
|
// positive.
|
|
|
|
line = Math.max(0, line);
|
|
|
|
|
2013-07-25 01:41:11 +00:00
|
|
|
cy = lpos.yi + this.itop + line;
|
|
|
|
cx = lpos.xi + this.ileft + last.length;
|
|
|
|
|
2013-07-29 20:49:25 +00:00
|
|
|
// XXX Not sure, but this may still sometimes
|
|
|
|
// cause problems when leaving editor.
|
2013-07-25 01:41:11 +00:00
|
|
|
if (cy === program.y && cx === program.x) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cy === program.y) {
|
|
|
|
if (cx > program.x) {
|
|
|
|
program.cuf(cx - program.x);
|
|
|
|
} else if (cx < program.x) {
|
|
|
|
program.cub(program.x - cx);
|
|
|
|
}
|
|
|
|
} else if (cx === program.x) {
|
|
|
|
if (cy > program.y) {
|
|
|
|
program.cud(cy - program.y);
|
|
|
|
} else if (cy < program.y) {
|
|
|
|
program.cuu(program.y - cy);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
program.cup(cy, cx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Textarea.prototype.input =
|
2013-07-29 13:54:02 +00:00
|
|
|
Textarea.prototype.setInput =
|
|
|
|
Textarea.prototype.readInput = 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-29 20:49:25 +00:00
|
|
|
if (this._reading) return;
|
|
|
|
this._reading = true;
|
2013-07-29 15:22:08 +00:00
|
|
|
|
2013-07-29 20:49:25 +00:00
|
|
|
this._callback = callback;
|
2013-07-24 23:20:30 +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-29 13:54:02 +00:00
|
|
|
this._updateCursor();
|
2013-06-09 18:14:42 +00:00
|
|
|
this.screen.program.showCursor();
|
2013-07-29 20:49:25 +00:00
|
|
|
//this.screen.program.sgr('normal');
|
|
|
|
|
|
|
|
this._done = function fn(err, value) {
|
|
|
|
if (!self._reading) return;
|
2013-06-09 18:14:42 +00:00
|
|
|
|
2013-07-24 22:16:55 +00:00
|
|
|
if (fn.done) return;
|
|
|
|
fn.done = true;
|
|
|
|
|
2013-07-29 20:49:25 +00:00
|
|
|
self._reading = false;
|
|
|
|
|
2013-07-29 15:22:08 +00:00
|
|
|
delete self._callback;
|
2013-07-29 20:49:25 +00:00
|
|
|
delete self._done;
|
2013-07-29 15:22:08 +00:00
|
|
|
|
2013-07-24 22:16:55 +00:00
|
|
|
self.removeListener('keypress', self.__listener);
|
|
|
|
delete self.__listener;
|
2013-07-29 15:22:08 +00:00
|
|
|
|
2013-07-29 20:49:25 +00:00
|
|
|
self.removeListener('blur', self.__done);
|
|
|
|
delete self.__done;
|
2013-07-24 22:16:55 +00:00
|
|
|
|
2013-06-09 18:14:42 +00:00
|
|
|
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-07-29 20:49:25 +00:00
|
|
|
if (self.options.inputOnFocus) {
|
|
|
|
self.screen.rewindFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ugly
|
|
|
|
if (err === 'stop') return;
|
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
if (err) {
|
|
|
|
self.emit('error', err);
|
|
|
|
} else if (value != null) {
|
|
|
|
self.emit('submit', value);
|
|
|
|
} else {
|
|
|
|
self.emit('cancel', value);
|
|
|
|
}
|
|
|
|
self.emit('action', value);
|
|
|
|
|
2013-07-29 20:49:25 +00:00
|
|
|
if (!callback) return;
|
2013-07-29 09:23:48 +00:00
|
|
|
|
2013-07-29 20:49:25 +00:00
|
|
|
return err
|
|
|
|
? callback(err)
|
|
|
|
: callback(null, value);
|
2013-06-09 18:14:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.__listener = this._listener.bind(this);
|
|
|
|
this.on('keypress', this.__listener);
|
2013-07-29 15:22:08 +00:00
|
|
|
|
2013-07-29 20:49:25 +00:00
|
|
|
this.__done = this._done.bind(this, null, null);
|
|
|
|
this.on('blur', this.__done);
|
2013-06-09 18:14:42 +00:00
|
|
|
};
|
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
Textarea.prototype._listener = function(ch, key) {
|
2013-07-29 20:49:25 +00:00
|
|
|
var done = this._done
|
2013-07-04 05:10:01 +00:00
|
|
|
, value = this.value;
|
2013-06-09 18:14:42 +00:00
|
|
|
|
2014-01-04 22:02:10 +00:00
|
|
|
if (key.name === 'return') return;
|
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-29 20:49:25 +00:00
|
|
|
if (this.options.keys && key.ctrl && key.name === 'e') {
|
|
|
|
return this.readEditor();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Optimize typing by writing directly
|
|
|
|
// to the screen and screen buffer here.
|
2013-07-04 05:10:01 +00:00
|
|
|
if (key.name === 'escape') {
|
2013-07-29 20:49:25 +00:00
|
|
|
done(null, null);
|
2013-07-04 05:10:01 +00:00
|
|
|
} else if (key.name === 'backspace') {
|
|
|
|
if (this.value.length) {
|
|
|
|
this.value = this.value.slice(0, -1);
|
|
|
|
}
|
2013-07-24 22:16:55 +00:00
|
|
|
} else if (ch) {
|
2013-07-29 20:49:25 +00:00
|
|
|
if (!/^[\x00-\x08\x0b-\x0c\x0e-\x1f\x7f]$/.test(ch)) {
|
|
|
|
this.value += ch;
|
|
|
|
}
|
2013-07-04 05:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.value !== value) {
|
|
|
|
this.screen.render();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-04 06:50:50 +00:00
|
|
|
Textarea.prototype._typeScroll = function() {
|
|
|
|
// XXX Workaround
|
2013-07-23 15:38:54 +00:00
|
|
|
var height = this.height - this.iheight;
|
|
|
|
if (this._clines.length - this.childBase > height) {
|
2013-07-04 06:50:50 +00:00
|
|
|
this.scroll(this._clines.length);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-29 13:54:02 +00:00
|
|
|
Textarea.prototype.getValue = function() {
|
|
|
|
return this.value;
|
|
|
|
};
|
|
|
|
|
2013-07-24 23:05:56 +00:00
|
|
|
Textarea.prototype.setValue = function(value) {
|
|
|
|
if (value == null) {
|
|
|
|
value = this.value;
|
|
|
|
}
|
|
|
|
if (this._value !== value) {
|
|
|
|
this.value = value;
|
|
|
|
this._value = value;
|
|
|
|
this.setContent(this.value);
|
|
|
|
this._typeScroll();
|
2013-07-29 13:54:02 +00:00
|
|
|
this._updateCursor();
|
2013-07-24 23:05:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-29 13:54:02 +00:00
|
|
|
Textarea.prototype.clearInput =
|
|
|
|
Textarea.prototype.clearValue = function() {
|
2013-07-24 23:05:56 +00:00
|
|
|
return this.setValue('');
|
2013-07-04 06:50:50 +00:00
|
|
|
};
|
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
Textarea.prototype.submit = function() {
|
2013-07-29 20:49:25 +00:00
|
|
|
if (!this.__listener) return;
|
|
|
|
return this.__listener('\x1b', { name: 'escape' });
|
2013-07-04 05:10:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Textarea.prototype.cancel = function() {
|
2013-07-29 20:49:25 +00:00
|
|
|
if (!this.__listener) return;
|
|
|
|
return this.__listener('\x1b', { name: 'escape' });
|
2013-07-04 05:10:01 +00:00
|
|
|
};
|
|
|
|
|
2013-07-24 23:05:56 +00:00
|
|
|
Textarea.prototype.render = function() {
|
|
|
|
this.setValue();
|
|
|
|
return this._render();
|
|
|
|
};
|
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
Textarea.prototype.editor =
|
2013-07-29 13:54:02 +00:00
|
|
|
Textarea.prototype.setEditor =
|
|
|
|
Textarea.prototype.readEditor = function(callback) {
|
2013-07-04 05:10:01 +00:00
|
|
|
var self = this;
|
2013-07-29 20:49:25 +00:00
|
|
|
|
|
|
|
if (this._reading) {
|
|
|
|
var _cb = this._callback
|
|
|
|
, cb = callback;
|
|
|
|
|
|
|
|
this._done('stop');
|
|
|
|
|
|
|
|
callback = function(err, value) {
|
|
|
|
if (_cb) _cb(err, value);
|
|
|
|
if (cb) cb(err, value);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-09-14 05:01:24 +00:00
|
|
|
if (!callback) {
|
|
|
|
callback = function() {};
|
|
|
|
}
|
|
|
|
|
2013-07-04 05:10:01 +00:00
|
|
|
return this.screen.readEditor({ value: this.value }, function(err, value) {
|
2013-09-14 05:01:24 +00:00
|
|
|
if (err) {
|
|
|
|
if (err.message === 'Unsuccessful.') {
|
|
|
|
self.screen.render();
|
|
|
|
return self.readInput(callback);
|
|
|
|
}
|
|
|
|
self.screen.render();
|
|
|
|
self.readInput(callback);
|
|
|
|
return callback(err);
|
|
|
|
}
|
2013-07-25 01:41:11 +00:00
|
|
|
self.setValue(value);
|
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-07-25 01:41:11 +00:00
|
|
|
/**
|
|
|
|
* Textbox
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Textbox(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!(this instanceof Node)) {
|
|
|
|
return new Textbox(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
options.scrollable = false;
|
|
|
|
|
|
|
|
Textarea.call(this, options);
|
|
|
|
|
|
|
|
this.secret = options.secret;
|
|
|
|
this.censor = options.censor;
|
|
|
|
}
|
|
|
|
|
|
|
|
Textbox.prototype.__proto__ = Textarea.prototype;
|
|
|
|
|
|
|
|
Textbox.prototype.type = 'textbox';
|
|
|
|
|
|
|
|
Textbox.prototype.__olistener = Textbox.prototype._listener;
|
|
|
|
Textbox.prototype._listener = function(ch, key) {
|
|
|
|
if (key.name === 'enter') {
|
2013-07-29 20:49:25 +00:00
|
|
|
this._done(null, this.value);
|
2013-07-25 01:41:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return this.__olistener(ch, key);
|
|
|
|
};
|
|
|
|
|
|
|
|
Textbox.prototype.setValue = function(value) {
|
|
|
|
var visible, val;
|
|
|
|
if (value == null) {
|
|
|
|
value = this.value;
|
|
|
|
}
|
|
|
|
if (this._value !== value) {
|
|
|
|
value = value.replace(/\n/g, '');
|
|
|
|
this.value = value;
|
|
|
|
this._value = value;
|
|
|
|
if (this.secret) {
|
|
|
|
this.setContent('');
|
|
|
|
} else if (this.censor) {
|
|
|
|
this.setContent(Array(this.value.length + 1).join('*'));
|
|
|
|
} else {
|
|
|
|
visible = -(this.width - this.iwidth - 1);
|
|
|
|
val = this.value.replace(/\t/g, this.screen.tabc);
|
|
|
|
this.setContent(val.slice(visible));
|
|
|
|
}
|
2013-07-29 13:54:02 +00:00
|
|
|
this._updateCursor();
|
2013-07-25 01:41:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-12 11:08:16 +00:00
|
|
|
Textbox.prototype.submit = function() {
|
2013-07-29 20:49:25 +00:00
|
|
|
if (!this.__listener) return;
|
|
|
|
return this.__listener('\r', { name: 'enter' });
|
|
|
|
};
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Button
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Button(options) {
|
2013-06-16 09:21:57 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-06-14 20:58:54 +00:00
|
|
|
return new Button(options);
|
|
|
|
}
|
2013-06-16 09:21:57 +00:00
|
|
|
|
2013-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
|
|
|
|
2013-07-17 06:37:57 +00:00
|
|
|
if (options.autoFocus == null) {
|
|
|
|
options.autoFocus = false;
|
|
|
|
}
|
|
|
|
|
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') {
|
2014-06-05 06:12:47 +00:00
|
|
|
return self.press();
|
2013-06-16 09:21:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-06-29 22:46:47 +00:00
|
|
|
if (this.options.mouse) {
|
|
|
|
this.on('click', function() {
|
2014-06-05 06:12:47 +00:00
|
|
|
return self.press();
|
2013-06-29 22:46:47 +00:00
|
|
|
});
|
|
|
|
}
|
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() {
|
2013-08-13 15:54:17 +00:00
|
|
|
this.value = true;
|
2014-06-05 06:12:47 +00:00
|
|
|
var result = this.emit('press');
|
2013-08-13 15:54:17 +00:00
|
|
|
delete this.value;
|
2014-06-05 06:12:47 +00:00
|
|
|
return result;
|
2013-06-16 09:21:57 +00:00
|
|
|
};
|
|
|
|
|
2013-06-01 07:06:04 +00:00
|
|
|
/**
|
|
|
|
* ProgressBar
|
|
|
|
*/
|
|
|
|
|
|
|
|
function ProgressBar(options) {
|
2013-09-11 11:22:08 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-06-14 20:58:54 +00:00
|
|
|
return new ProgressBar(options);
|
|
|
|
}
|
2013-07-18 22:56:05 +00:00
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
|
2013-06-01 07:06:04 +00:00
|
|
|
Input.call(this, options);
|
2013-07-17 05:53:34 +00:00
|
|
|
|
2013-06-01 07:06:04 +00:00
|
|
|
this.filled = options.filled || 0;
|
|
|
|
if (typeof this.filled === 'string') {
|
|
|
|
this.filled = +this.filled.slice(0, -1);
|
|
|
|
}
|
2013-07-17 05:53:34 +00:00
|
|
|
this.value = this.filled;
|
|
|
|
|
2013-06-01 07:06:04 +00:00
|
|
|
this.ch = options.ch || ' ';
|
2013-07-17 05:53:34 +00:00
|
|
|
|
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-07-17 05:53:34 +00:00
|
|
|
|
2013-06-12 09:18:47 +00:00
|
|
|
this.orientation = options.orientation || 'horizontal';
|
2013-07-17 05:53:34 +00:00
|
|
|
|
|
|
|
if (options.keys) {
|
|
|
|
this.on('keypress', function(ch, key) {
|
|
|
|
var back, forward;
|
|
|
|
if (self.orientation === 'horizontal') {
|
|
|
|
back = ['left', 'h'];
|
|
|
|
forward = ['right', 'l'];
|
|
|
|
} else if (self.orientation === 'vertical') {
|
|
|
|
back = ['down', 'j'];
|
|
|
|
forward = ['up', 'k'];
|
|
|
|
}
|
|
|
|
if (key.name === back[0] || (options.vi && key.name === back[1])) {
|
|
|
|
self.progress(-5);
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (key.name === forward[0] || (options.vi && key.name === forward[1])) {
|
|
|
|
self.progress(5);
|
|
|
|
self.screen.render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.mouse) {
|
|
|
|
this.on('click', function(data) {
|
|
|
|
var x, y, m, p;
|
2013-07-29 13:54:02 +00:00
|
|
|
if (!self.lpos) return;
|
2013-07-17 05:53:34 +00:00
|
|
|
if (self.orientation === 'horizontal') {
|
2013-07-29 13:54:02 +00:00
|
|
|
x = data.x - self.lpos.xi;
|
|
|
|
m = (self.lpos.xl - self.lpos.xi) - self.iwidth;
|
2013-07-17 05:53:34 +00:00
|
|
|
p = x / m * 100 | 0;
|
|
|
|
} else if (self.orientation === 'vertical') {
|
2013-07-29 13:54:02 +00:00
|
|
|
y = data.y - self.lpos.yi;
|
|
|
|
m = (self.lpos.yl - self.lpos.yi) - self.iheight;
|
2013-07-17 05:53:34 +00:00
|
|
|
p = y / m * 100 | 0;
|
|
|
|
}
|
|
|
|
self.setProgress(p);
|
|
|
|
});
|
|
|
|
}
|
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-07-18 08:54:41 +00:00
|
|
|
ProgressBar.prototype.render = function() {
|
|
|
|
var ret = this._render();
|
2013-07-19 14:17:21 +00:00
|
|
|
if (!ret) return;
|
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
|
|
|
|
2015-02-12 01:20:22 +00:00
|
|
|
dattr = this.sattr(this.style.bar, 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
|
|
|
|
2014-01-12 11:08:16 +00:00
|
|
|
if (this.content) {
|
|
|
|
var line = this.screen.lines[yi];
|
|
|
|
for (var i = 0; i < this.content.length; i++) {
|
|
|
|
line[xi + i][1] = this.content[i];
|
|
|
|
}
|
|
|
|
line.dirty = true;
|
|
|
|
}
|
|
|
|
|
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-07-17 05:53:34 +00:00
|
|
|
this.value = this.filled;
|
|
|
|
};
|
|
|
|
|
|
|
|
ProgressBar.prototype.setProgress = function(filled) {
|
|
|
|
this.filled = 0;
|
|
|
|
this.progress(filled);
|
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-17 05:53:34 +00:00
|
|
|
this.value = this.filled;
|
2013-06-09 19:32:29 +00:00
|
|
|
};
|
|
|
|
|
2013-07-12 12:48:55 +00:00
|
|
|
/**
|
|
|
|
* FileManager
|
|
|
|
*/
|
|
|
|
|
|
|
|
function FileManager(options) {
|
2013-07-18 22:56:05 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-07-12 12:48:55 +00:00
|
|
|
return new FileManager(options);
|
|
|
|
}
|
|
|
|
|
2013-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
2013-07-12 12:48:55 +00:00
|
|
|
options.parseTags = true;
|
2013-08-27 15:22:19 +00:00
|
|
|
// options.label = ' {blue-fg}%path{/blue-fg} ';
|
2013-07-12 12:48:55 +00:00
|
|
|
|
|
|
|
List.call(this, options);
|
|
|
|
|
|
|
|
this.cwd = options.cwd || process.cwd();
|
2013-07-17 05:53:34 +00:00
|
|
|
this.file = this.cwd;
|
|
|
|
this.value = this.cwd;
|
2013-07-12 12:48:55 +00:00
|
|
|
|
2013-08-27 15:22:19 +00:00
|
|
|
if (options.label && ~options.label.indexOf('%path')) {
|
|
|
|
this._label.setContent(options.label.replace('%path', this.cwd));
|
|
|
|
}
|
|
|
|
|
2013-07-12 12:48:55 +00:00
|
|
|
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);
|
|
|
|
}
|
2013-07-17 05:53:34 +00:00
|
|
|
self.file = file;
|
|
|
|
self.value = file;
|
2013-07-12 12:48:55 +00:00
|
|
|
if (stat.isDirectory()) {
|
|
|
|
self.emit('cd', file, self.cwd);
|
|
|
|
self.cwd = file;
|
2013-08-27 15:22:19 +00:00
|
|
|
if (options.label && ~options.label.indexOf('%path')) {
|
|
|
|
self._label.setContent(options.label.replace('%path', file));
|
|
|
|
}
|
2013-07-12 12:48:55 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (cwd) this.cwd = cwd;
|
|
|
|
else cwd = this.cwd;
|
2013-07-12 12:48:55 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-07-14 01:44:30 +00:00
|
|
|
return new Checkbox(options);
|
|
|
|
}
|
|
|
|
|
2013-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
|
|
|
|
2013-07-14 01:44:30 +00:00
|
|
|
Input.call(this, options);
|
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
this.text = options.content || options.text || '';
|
|
|
|
this.checked = this.value = options.checked || false;
|
2013-07-14 01:44:30 +00:00
|
|
|
|
|
|
|
this.on('keypress', function(ch, key) {
|
|
|
|
if (key.name === 'enter' || key.name === 'space') {
|
2013-07-17 05:53:34 +00:00
|
|
|
self.toggle();
|
|
|
|
self.screen.render();
|
2013-07-14 01:44:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
if (options.mouse) {
|
2013-07-14 01:44:30 +00:00
|
|
|
this.on('click', function() {
|
2013-07-24 22:16:55 +00:00
|
|
|
self.toggle();
|
2013-07-17 05:53:34 +00:00
|
|
|
self.screen.render();
|
2013-07-14 01:44:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
this.on('focus', function(old) {
|
2013-07-29 15:22:08 +00:00
|
|
|
var lpos = self.lpos;
|
2013-07-21 10:31:24 +00:00
|
|
|
if (!lpos) return;
|
2013-07-29 15:22:08 +00:00
|
|
|
self.screen.program.lsaveCursor('checkbox');
|
2013-07-21 10:31:24 +00:00
|
|
|
self.screen.program.cup(lpos.yi, lpos.xi + 1);
|
2013-07-17 05:53:34 +00:00
|
|
|
self.screen.program.showCursor();
|
2013-07-14 01:44:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.on('blur', function() {
|
2013-07-29 15:22:08 +00:00
|
|
|
self.screen.program.lrestoreCursor('checkbox', true);
|
2013-07-14 01:44:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Checkbox.prototype.__proto__ = Input.prototype;
|
|
|
|
|
|
|
|
Checkbox.prototype.type = 'checkbox';
|
|
|
|
|
2013-07-18 08:54:41 +00:00
|
|
|
Checkbox.prototype.render = function() {
|
2013-08-15 12:14:56 +00:00
|
|
|
this.clearPos(true);
|
|
|
|
this.setContent('[' + (this.checked ? 'x' : ' ') + '] ' + this.text, true);
|
2013-07-18 08:54:41 +00:00
|
|
|
return this._render();
|
2013-07-14 01:44:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Checkbox.prototype.check = function() {
|
|
|
|
if (this.checked) return;
|
2013-07-17 05:53:34 +00:00
|
|
|
this.checked = this.value = true;
|
2013-07-14 01:44:30 +00:00
|
|
|
this.emit('check');
|
|
|
|
};
|
|
|
|
|
|
|
|
Checkbox.prototype.uncheck = function() {
|
|
|
|
if (!this.checked) return;
|
2013-07-17 05:53:34 +00:00
|
|
|
this.checked = this.value = false;
|
2013-07-14 01:44:30 +00:00
|
|
|
this.emit('uncheck');
|
|
|
|
};
|
|
|
|
|
|
|
|
Checkbox.prototype.toggle = function() {
|
|
|
|
return this.checked
|
|
|
|
? this.uncheck()
|
|
|
|
: this.check();
|
|
|
|
};
|
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
/**
|
|
|
|
* RadioSet
|
|
|
|
*/
|
|
|
|
|
|
|
|
function RadioSet(options) {
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-07-17 05:53:34 +00:00
|
|
|
return new RadioSet(options);
|
|
|
|
}
|
2013-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
|
|
|
// Possibly inherit parent's style.
|
|
|
|
// options.style = this.parent.style;
|
2013-07-17 05:53:34 +00:00
|
|
|
Box.call(this, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
RadioSet.prototype.__proto__ = Box.prototype;
|
|
|
|
|
|
|
|
RadioSet.prototype.type = 'radio-set';
|
|
|
|
|
2013-07-14 01:44:30 +00:00
|
|
|
/**
|
|
|
|
* RadioButton
|
|
|
|
*/
|
|
|
|
|
|
|
|
function RadioButton(options) {
|
|
|
|
var self = this;
|
|
|
|
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-07-14 01:44:30 +00:00
|
|
|
return new RadioButton(options);
|
|
|
|
}
|
|
|
|
|
2013-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
|
|
|
|
2013-07-14 01:44:30 +00:00
|
|
|
Checkbox.call(this, options);
|
|
|
|
|
|
|
|
this.on('check', function() {
|
2013-07-21 10:31:24 +00:00
|
|
|
var el = self;
|
|
|
|
while (el = el.parent) {
|
2013-07-24 22:16:55 +00:00
|
|
|
if (el.type === 'radio-set'
|
|
|
|
|| el.type === 'form') break;
|
2013-07-21 10:31:24 +00:00
|
|
|
}
|
2013-07-24 22:16:55 +00:00
|
|
|
el = el || self.parent;
|
|
|
|
el.forDescendants(function(el) {
|
|
|
|
if (el.type !== 'radio-button' || el === self) {
|
|
|
|
return;
|
|
|
|
}
|
2013-07-14 01:44:30 +00:00
|
|
|
el.uncheck();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
RadioButton.prototype.__proto__ = Checkbox.prototype;
|
|
|
|
|
|
|
|
RadioButton.prototype.type = 'radio-button';
|
|
|
|
|
2013-07-30 00:22:50 +00:00
|
|
|
RadioButton.prototype.render = function() {
|
2013-08-15 12:14:56 +00:00
|
|
|
this.clearPos(true);
|
|
|
|
this.setContent('(' + (this.checked ? '*' : ' ') + ') ' + this.text, true);
|
2013-07-30 00:22:50 +00:00
|
|
|
return this._render();
|
|
|
|
};
|
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
RadioButton.prototype.toggle = RadioButton.prototype.check;
|
|
|
|
|
2013-07-14 01:44:30 +00:00
|
|
|
/**
|
|
|
|
* Prompt
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Prompt(options) {
|
|
|
|
var self = this;
|
|
|
|
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-07-14 01:44:30 +00:00
|
|
|
return new Prompt(options);
|
|
|
|
}
|
|
|
|
|
2013-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
|
|
|
|
2014-01-11 22:52:33 +00:00
|
|
|
options.hidden = true;
|
|
|
|
|
2013-07-14 01:44:30 +00:00
|
|
|
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';
|
|
|
|
|
2015-01-25 02:34:57 +00:00
|
|
|
Prompt.prototype.input =
|
|
|
|
Prompt.prototype.setInput =
|
|
|
|
Prompt.prototype.readInput = function(text, value, callback) {
|
2013-07-14 01:44:30 +00:00
|
|
|
var self = this;
|
|
|
|
var okay, cancel;
|
|
|
|
|
|
|
|
if (!callback) {
|
|
|
|
callback = value;
|
|
|
|
value = '';
|
|
|
|
}
|
|
|
|
|
2015-01-25 05:33:30 +00:00
|
|
|
// Keep above:
|
|
|
|
// var parent = this.parent;
|
|
|
|
// this.detach();
|
|
|
|
// parent.append(this);
|
2014-01-13 16:25:31 +00:00
|
|
|
|
2013-07-14 01:44:30 +00:00
|
|
|
this.show();
|
|
|
|
this.setContent(' ' + text);
|
|
|
|
|
2014-01-12 11:08:16 +00:00
|
|
|
this._.input.value = value;
|
2013-07-14 01:44:30 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-07-14 01:44:30 +00:00
|
|
|
return new Question(options);
|
|
|
|
}
|
|
|
|
|
2013-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
2014-01-11 22:52:33 +00:00
|
|
|
options.hidden = true;
|
2013-07-18 22:56:05 +00:00
|
|
|
|
2013-07-14 01:44:30 +00:00
|
|
|
Box.call(this, options);
|
|
|
|
|
|
|
|
this._.okay = new Button({
|
2014-01-12 16:40:18 +00:00
|
|
|
screen: this.screen,
|
2013-07-14 01:44:30 +00:00
|
|
|
parent: this,
|
2014-01-12 16:40:18 +00:00
|
|
|
top: 2,
|
2013-07-14 01:44:30 +00:00
|
|
|
height: 1,
|
|
|
|
left: 2,
|
|
|
|
width: 6,
|
|
|
|
content: 'Okay',
|
|
|
|
align: 'center',
|
|
|
|
bg: 'black',
|
|
|
|
hoverBg: 'blue',
|
|
|
|
autoFocus: false,
|
|
|
|
mouse: true
|
|
|
|
});
|
|
|
|
|
|
|
|
this._.cancel = new Button({
|
2014-01-12 16:40:18 +00:00
|
|
|
screen: this.screen,
|
2013-07-14 01:44:30 +00:00
|
|
|
parent: this,
|
2014-01-12 16:40:18 +00:00
|
|
|
top: 2,
|
2013-07-14 01:44:30 +00:00
|
|
|
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;
|
|
|
|
|
2015-01-25 05:33:30 +00:00
|
|
|
// Keep above:
|
|
|
|
// var parent = this.parent;
|
|
|
|
// this.detach();
|
|
|
|
// parent.append(this);
|
2014-01-13 16:25:31 +00:00
|
|
|
|
2013-07-14 01:44:30 +00:00
|
|
|
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;
|
|
|
|
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-07-14 01:44:30 +00:00
|
|
|
return new Message(options);
|
|
|
|
}
|
|
|
|
|
2013-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
2013-07-14 01:44:30 +00:00
|
|
|
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;
|
2015-01-25 05:33:30 +00:00
|
|
|
|
2013-07-14 01:44:30 +00:00
|
|
|
if (typeof time === 'function') {
|
|
|
|
callback = time;
|
|
|
|
time = null;
|
|
|
|
}
|
2015-01-25 05:33:30 +00:00
|
|
|
|
2014-01-11 22:52:33 +00:00
|
|
|
if (time == null) time = 3;
|
|
|
|
|
2015-01-25 05:33:30 +00:00
|
|
|
// Keep above:
|
|
|
|
// var parent = this.parent;
|
|
|
|
// this.detach();
|
|
|
|
// parent.append(this);
|
2014-01-12 11:08:16 +00:00
|
|
|
|
|
|
|
if (this.scrollable) {
|
|
|
|
this.screen.saveFocus();
|
|
|
|
this.focus();
|
2015-01-16 18:16:00 +00:00
|
|
|
this.scrollTo(0);
|
2014-01-12 11:08:16 +00:00
|
|
|
}
|
|
|
|
|
2013-07-14 01:44:30 +00:00
|
|
|
this.show();
|
|
|
|
this.setContent(text);
|
|
|
|
this.screen.render();
|
|
|
|
|
2014-01-11 22:52:33 +00:00
|
|
|
if (time === Infinity || time === -1 || time === 0) {
|
|
|
|
var end = function() {
|
|
|
|
if (end.done) return;
|
|
|
|
end.done = true;
|
2014-01-12 11:08:16 +00:00
|
|
|
if (self.scrollable) {
|
2014-01-12 14:58:35 +00:00
|
|
|
try {
|
|
|
|
self.screen.restoreFocus();
|
|
|
|
} catch (e) {
|
|
|
|
;
|
|
|
|
}
|
2014-01-12 11:08:16 +00:00
|
|
|
}
|
2014-01-11 22:52:33 +00:00
|
|
|
self.hide();
|
|
|
|
self.screen.render();
|
|
|
|
if (callback) callback();
|
|
|
|
};
|
2013-07-14 01:44:30 +00:00
|
|
|
|
2014-01-11 22:52:33 +00:00
|
|
|
setTimeout(function() {
|
|
|
|
self.screen.on('keypress', function fn(ch, key) {
|
|
|
|
if (key.name === 'mouse') return;
|
2014-01-12 11:08:16 +00:00
|
|
|
if (self.scrollable) {
|
|
|
|
if ((key.name === 'up' || (self.options.vi && key.name === 'k'))
|
|
|
|
|| (key.name === 'down' || (self.options.vi && key.name === 'j'))
|
|
|
|
|| (self.options.vi && key.name === 'u' && key.ctrl)
|
|
|
|
|| (self.options.vi && key.name === 'd' && key.ctrl)
|
|
|
|
|| (self.options.vi && key.name === 'b' && key.ctrl)
|
|
|
|
|| (self.options.vi && key.name === 'f' && key.ctrl)
|
|
|
|
|| (self.options.vi && key.name === 'g' && !key.shift)
|
|
|
|
|| (self.options.vi && key.name === 'g' && key.shift)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-06-02 01:38:36 +00:00
|
|
|
if (self.options.ignoreKeys && ~self.options.ignoreKeys.indexOf(key.name)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-01-11 22:52:33 +00:00
|
|
|
self.screen.removeListener('keypress', fn);
|
|
|
|
end();
|
|
|
|
});
|
|
|
|
if (!self.options.mouse) return;
|
|
|
|
self.screen.on('mouse', function fn(data) {
|
|
|
|
if (data.action === 'mousemove') return;
|
|
|
|
self.screen.removeListener('mouse', fn);
|
|
|
|
end();
|
|
|
|
});
|
|
|
|
}, 10);
|
2013-07-14 01:44:30 +00:00
|
|
|
|
2014-01-11 22:52:33 +00:00
|
|
|
return;
|
2013-07-14 01:44:30 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 22:52:33 +00:00
|
|
|
setTimeout(function() {
|
2013-07-14 01:44:30 +00:00
|
|
|
self.hide();
|
|
|
|
self.screen.render();
|
|
|
|
if (callback) callback();
|
2014-01-11 22:52:33 +00:00
|
|
|
}, time * 1000);
|
|
|
|
};
|
2013-07-14 01:44:30 +00:00
|
|
|
|
2014-01-11 22:52:33 +00:00
|
|
|
Message.prototype.error = function(text, time, callback) {
|
|
|
|
return this.display('{red-fg}Error: ' + text + '{/red-fg}', time, callback);
|
2013-07-14 01:44:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loading
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Loading(options) {
|
|
|
|
var self = this;
|
|
|
|
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-07-14 01:44:30 +00:00
|
|
|
return new Loading(options);
|
|
|
|
}
|
|
|
|
|
2013-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
|
|
|
|
2013-07-14 01:44:30 +00:00
|
|
|
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;
|
|
|
|
|
2015-01-25 05:33:30 +00:00
|
|
|
// XXX Keep above:
|
|
|
|
// var parent = this.parent;
|
|
|
|
// this.detach();
|
|
|
|
// parent.append(this);
|
2014-01-13 16:25:31 +00:00
|
|
|
|
2013-07-14 01:44:30 +00:00
|
|
|
this.show();
|
|
|
|
this.setContent(text);
|
|
|
|
|
|
|
|
if (this._.timer) {
|
2014-06-11 02:41:22 +00:00
|
|
|
this.stop();
|
2013-07-14 01:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listbar / HorizontalList
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Listbar(options) {
|
|
|
|
var self = this;
|
|
|
|
|
2013-07-24 20:39:22 +00:00
|
|
|
if (!(this instanceof Node)) {
|
2013-07-14 01:44:30 +00:00
|
|
|
return new Listbar(options);
|
|
|
|
}
|
|
|
|
|
2013-07-18 22:56:05 +00:00
|
|
|
options = options || {};
|
|
|
|
|
2013-07-14 01:44:30 +00:00
|
|
|
this.items = [];
|
2014-01-12 00:23:38 +00:00
|
|
|
this.ritems = [];
|
|
|
|
this.commands = [];
|
2014-01-12 00:54:26 +00:00
|
|
|
|
2013-07-17 08:20:37 +00:00
|
|
|
this.leftBase = 0;
|
|
|
|
this.leftOffset = 0;
|
2013-07-14 01:44:30 +00:00
|
|
|
|
2014-01-12 00:54:26 +00:00
|
|
|
this.mouse = options.mouse || false;
|
|
|
|
|
2013-07-14 01:44:30 +00:00
|
|
|
Box.call(this, options);
|
2013-07-17 08:20:37 +00:00
|
|
|
|
2015-01-30 04:06:19 +00:00
|
|
|
if (!this.style.selected) {
|
|
|
|
this.style.selected = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.style.item) {
|
|
|
|
this.style.item = {};
|
|
|
|
}
|
|
|
|
|
2013-07-17 08:20:37 +00:00
|
|
|
if (options.commands || options.items) {
|
|
|
|
this.setItems(options.commands || options.items);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.keys) {
|
2014-01-12 00:23:38 +00:00
|
|
|
this.on('keypress', function(ch, key) {
|
2014-01-12 01:28:14 +00:00
|
|
|
if (key.name === 'left'
|
|
|
|
|| (options.vi && key.name === 'h')
|
|
|
|
|| (key.shift && key.name === 'tab')) {
|
2014-01-12 00:54:26 +00:00
|
|
|
self.moveLeft();
|
2013-07-17 08:20:37 +00:00
|
|
|
self.screen.render();
|
2014-01-12 01:28:14 +00:00
|
|
|
// Stop propagation if we're in a form.
|
|
|
|
if (key.name === 'tab') return false;
|
2013-07-17 08:20:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-01-12 01:28:14 +00:00
|
|
|
if (key.name === 'right'
|
|
|
|
|| (options.vi && key.name === 'l')
|
|
|
|
|| key.name === 'tab') {
|
2014-01-12 00:54:26 +00:00
|
|
|
self.moveRight();
|
2013-07-17 08:20:37 +00:00
|
|
|
self.screen.render();
|
2014-01-12 01:28:14 +00:00
|
|
|
// Stop propagation if we're in a form.
|
|
|
|
if (key.name === 'tab') return false;
|
2013-07-17 08:20:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-01-11 22:52:33 +00:00
|
|
|
if (key.name === 'enter'
|
|
|
|
|| (options.vi && key.name === 'k' && !key.shift)) {
|
|
|
|
self.emit('action', self.items[self.selected], self.selected);
|
|
|
|
self.emit('select', self.items[self.selected], self.selected);
|
2014-01-12 00:23:38 +00:00
|
|
|
var item = self.items[self.selected];
|
2014-01-12 00:54:26 +00:00
|
|
|
if (item._.cmd.callback) {
|
|
|
|
item._.cmd.callback();
|
|
|
|
}
|
2014-01-12 00:23:38 +00:00
|
|
|
self.screen.render();
|
2014-01-11 22:52:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (key.name === 'escape' || (options.vi && key.name === 'q')) {
|
|
|
|
self.emit('action');
|
|
|
|
self.emit('cancel');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-01-12 00:54:26 +00:00
|
|
|
if (options.autoCommandKeys) {
|
2014-01-11 22:52:33 +00:00
|
|
|
this.screen.on('keypress', function(ch, key) {
|
|
|
|
if (/^[0-9]$/.test(ch)) {
|
|
|
|
var i = +ch - 1;
|
|
|
|
if (!~i) i = 9;
|
2014-06-02 04:43:02 +00:00
|
|
|
return self.selectTab(i);
|
2014-01-11 22:52:33 +00:00
|
|
|
}
|
2013-07-17 08:20:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.on('focus', function() {
|
2014-01-12 00:54:26 +00:00
|
|
|
self.select(self.selected);
|
2013-07-17 08:20:37 +00:00
|
|
|
});
|
2013-07-14 01:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Listbar.prototype.__proto__ = Box.prototype;
|
|
|
|
|
2013-07-17 05:53:34 +00:00
|
|
|
Listbar.prototype.type = 'listbar';
|
2013-07-14 01:44:30 +00:00
|
|
|
|
2014-01-11 22:52:33 +00:00
|
|
|
Listbar.prototype.__defineGetter__('selected', function() {
|
|
|
|
return this.leftBase + this.leftOffset;
|
|
|
|
});
|
|
|
|
|
2014-01-12 00:23:38 +00:00
|
|
|
Listbar.prototype.setItems = function(commands) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!Array.isArray(commands)) {
|
|
|
|
commands = Object.keys(commands).reduce(function(obj, key, i) {
|
|
|
|
var cmd = commands[key]
|
|
|
|
, cb;
|
|
|
|
|
|
|
|
if (typeof cmd === 'function') {
|
|
|
|
cb = cmd;
|
|
|
|
cmd = { callback: cb };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.text == null) cmd.text = key;
|
|
|
|
if (cmd.prefix == null) cmd.prefix = ++i + '';
|
|
|
|
|
|
|
|
if (cmd.text == null && cmd.callback) {
|
|
|
|
cmd.text = cmd.callback.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.push(cmd);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.items.forEach(function(el) {
|
|
|
|
el.detach();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.items = [];
|
|
|
|
this.ritems = [];
|
|
|
|
this.commands = [];
|
|
|
|
|
|
|
|
commands.forEach(function(cmd) {
|
2014-01-12 00:54:26 +00:00
|
|
|
self.add(cmd);
|
2014-01-12 00:23:38 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-01-12 00:54:26 +00:00
|
|
|
Listbar.prototype.add =
|
|
|
|
Listbar.prototype.addItem =
|
2014-01-12 00:23:38 +00:00
|
|
|
Listbar.prototype.appendItem = function(item, callback) {
|
|
|
|
var self = this
|
|
|
|
, prev = this.items[this.items.length - 1]
|
|
|
|
, drawn = prev ? prev.left + prev.width : 0
|
|
|
|
, cmd
|
|
|
|
, title
|
|
|
|
, len;
|
|
|
|
|
2014-01-12 01:28:14 +00:00
|
|
|
if (!this.screen.autoPadding) {
|
|
|
|
drawn += this.ileft;
|
|
|
|
}
|
|
|
|
|
2014-01-12 00:23:38 +00:00
|
|
|
if (typeof item === 'object') {
|
|
|
|
cmd = item;
|
2014-01-12 11:08:16 +00:00
|
|
|
if (cmd.prefix == null) cmd.prefix = (this.items.length + 1) + '';
|
2014-01-12 00:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof item === 'string') {
|
2014-01-12 00:54:26 +00:00
|
|
|
cmd = {
|
|
|
|
prefix: (this.items.length + 1) + '',
|
|
|
|
text: item,
|
|
|
|
callback: callback
|
|
|
|
};
|
2014-01-12 00:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof item === 'function') {
|
2014-01-12 00:54:26 +00:00
|
|
|
cmd = {
|
|
|
|
prefix: (this.items.length + 1) + '',
|
|
|
|
text: item.name,
|
|
|
|
callback: item
|
|
|
|
};
|
2014-01-12 00:23:38 +00:00
|
|
|
}
|
|
|
|
|
2014-01-12 03:02:19 +00:00
|
|
|
if (cmd.keys && cmd.keys[0]) {
|
|
|
|
cmd.prefix = cmd.keys[0];
|
|
|
|
}
|
|
|
|
|
2014-01-12 11:08:16 +00:00
|
|
|
var t = generateTags(this.style.prefix || { fg: 'lightblack' });
|
|
|
|
|
|
|
|
title = (cmd.prefix != null ? t.open + cmd.prefix + t.close + ':' : '') + cmd.text;
|
2014-01-12 00:23:38 +00:00
|
|
|
|
|
|
|
len = ((cmd.prefix != null ? cmd.prefix + ':' : '') + cmd.text).length;
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
screen: this.screen,
|
|
|
|
top: 0,
|
|
|
|
left: drawn + 1,
|
|
|
|
height: 1,
|
|
|
|
content: title,
|
|
|
|
width: len + 2,
|
|
|
|
align: 'center',
|
|
|
|
autoFocus: false,
|
|
|
|
tags: true,
|
|
|
|
mouse: true,
|
2014-01-12 02:47:40 +00:00
|
|
|
style: merge({}, this.style.item),
|
|
|
|
noOverflow: true
|
2014-01-12 00:23:38 +00:00
|
|
|
};
|
|
|
|
|
2014-01-12 00:54:26 +00:00
|
|
|
if (!this.screen.autoPadding) {
|
2014-01-12 00:23:38 +00:00
|
|
|
options.top += this.itop;
|
|
|
|
options.left += this.ileft;
|
|
|
|
}
|
|
|
|
|
|
|
|
['bg', 'fg', 'bold', 'underline',
|
|
|
|
'blink', 'inverse', 'invisible'].forEach(function(name) {
|
|
|
|
options.style[name] = function() {
|
|
|
|
var attr = self.items[self.selected] === el
|
|
|
|
? self.style.selected[name]
|
|
|
|
: self.style.item[name];
|
|
|
|
if (typeof attr === 'function') attr = attr(el);
|
|
|
|
return attr;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2014-01-12 00:54:26 +00:00
|
|
|
var el = new Box(options);
|
2014-01-12 00:23:38 +00:00
|
|
|
|
|
|
|
this._[cmd.text] = el;
|
|
|
|
cmd.element = el;
|
|
|
|
el._.cmd = cmd;
|
|
|
|
|
|
|
|
this.ritems.push(cmd.text);
|
|
|
|
this.items.push(el);
|
|
|
|
this.commands.push(cmd);
|
|
|
|
this.append(el);
|
|
|
|
|
|
|
|
if (cmd.callback) {
|
|
|
|
if (cmd.keys) {
|
2014-01-12 03:02:19 +00:00
|
|
|
this.screen.key(cmd.keys, function(ch, key) {
|
|
|
|
self.emit('action', el, self.selected);
|
|
|
|
self.emit('select', el, self.selected);
|
|
|
|
if (el._.cmd.callback) {
|
|
|
|
el._.cmd.callback();
|
|
|
|
}
|
|
|
|
self.select(el);
|
|
|
|
self.screen.render();
|
|
|
|
});
|
2014-01-12 00:23:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.items.length === 1) {
|
|
|
|
this.select(0);
|
|
|
|
}
|
|
|
|
|
2014-01-12 00:54:26 +00:00
|
|
|
if (this.mouse) {
|
|
|
|
el.on('click', function(data) {
|
|
|
|
self.emit('action', el, self.selected);
|
|
|
|
self.emit('select', el, self.selected);
|
|
|
|
if (el._.cmd.callback) {
|
|
|
|
el._.cmd.callback();
|
|
|
|
}
|
|
|
|
self.select(el);
|
|
|
|
self.screen.render();
|
|
|
|
});
|
2014-01-12 00:23:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-18 08:54:41 +00:00
|
|
|
Listbar.prototype.render = function() {
|
2013-07-17 08:20:37 +00:00
|
|
|
var self = this
|
2014-01-12 02:56:59 +00:00
|
|
|
, drawn = 0;
|
2014-01-12 01:28:14 +00:00
|
|
|
|
|
|
|
if (!this.screen.autoPadding) {
|
|
|
|
drawn += this.ileft;
|
|
|
|
}
|
2013-07-17 08:20:37 +00:00
|
|
|
|
|
|
|
this.items.forEach(function(el, i) {
|
|
|
|
if (i < self.leftBase) {
|
|
|
|
el.hide();
|
|
|
|
} else {
|
2014-01-12 01:28:14 +00:00
|
|
|
el.rleft = drawn + 1;
|
2014-01-11 22:52:33 +00:00
|
|
|
drawn += el.width + 2;
|
2013-07-17 08:20:37 +00:00
|
|
|
el.show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return this._render();
|
|
|
|
};
|
|
|
|
|
|
|
|
Listbar.prototype.select = function(offset) {
|
2014-01-12 00:54:26 +00:00
|
|
|
if (typeof offset !== 'number') {
|
|
|
|
offset = this.items.indexOf(offset);
|
|
|
|
}
|
|
|
|
|
2013-07-23 20:26:57 +00:00
|
|
|
var lpos = this._getCoords();
|
|
|
|
if (!lpos) return;
|
|
|
|
|
2013-07-17 08:20:37 +00:00
|
|
|
var self = this
|
2014-01-11 22:52:33 +00:00
|
|
|
, width = (lpos.xl - lpos.xi) - this.iwidth
|
2013-07-17 08:20:37 +00:00
|
|
|
, drawn = 0
|
|
|
|
, visible = 0
|
|
|
|
, el;
|
|
|
|
|
2015-01-25 05:33:30 +00:00
|
|
|
if (offset < 0) {
|
|
|
|
offset = 0;
|
|
|
|
} else if (offset >= this.items.length) {
|
|
|
|
offset = this.items.length - 1;
|
|
|
|
}
|
2013-07-17 08:20:37 +00:00
|
|
|
|
|
|
|
el = this.items[offset];
|
|
|
|
if (!el) return;
|
|
|
|
|
|
|
|
this.items.forEach(function(el, i) {
|
|
|
|
if (i < self.leftBase) return;
|
2015-01-25 05:33:30 +00:00
|
|
|
|
2013-07-23 20:26:57 +00:00
|
|
|
var lpos = el._getCoords();
|
|
|
|
if (!lpos) return;
|
2014-01-12 02:56:59 +00:00
|
|
|
|
|
|
|
if (lpos.xl - lpos.xi <= 0) return;
|
|
|
|
|
|
|
|
drawn += (lpos.xl - lpos.xi) + 2;
|
|
|
|
|
2013-07-17 08:20:37 +00:00
|
|
|
if (drawn <= width) visible++;
|
|
|
|
});
|
|
|
|
|
2014-01-12 02:47:40 +00:00
|
|
|
var diff = offset - (this.leftBase + this.leftOffset);
|
|
|
|
if (offset > this.leftBase + this.leftOffset) {
|
|
|
|
if (offset > this.leftBase + visible - 1) {
|
|
|
|
this.leftOffset = 0;
|
|
|
|
this.leftBase = offset;
|
|
|
|
} else {
|
|
|
|
this.leftOffset += diff;
|
|
|
|
}
|
|
|
|
} else if (offset < this.leftBase + this.leftOffset) {
|
|
|
|
diff = -diff;
|
|
|
|
if (offset < this.leftBase) {
|
|
|
|
this.leftOffset = 0;
|
|
|
|
this.leftBase = offset;
|
|
|
|
} else {
|
|
|
|
this.leftOffset -= diff;
|
|
|
|
}
|
|
|
|
}
|
2013-07-17 08:20:37 +00:00
|
|
|
};
|
|
|
|
|
2014-01-12 00:54:26 +00:00
|
|
|
Listbar.prototype.removeItem = function(child) {
|
|
|
|
var i = typeof child !== 'number'
|
|
|
|
? this.items.indexOf(child)
|
|
|
|
: child;
|
|
|
|
|
|
|
|
if (~i && this.items[i]) {
|
|
|
|
child = this.items.splice(i, 1)[0];
|
|
|
|
this.ritems.splice(i, 1);
|
|
|
|
this.commands.splice(i, 1);
|
|
|
|
this.remove(child);
|
|
|
|
if (i === this.selected) {
|
|
|
|
this.select(i - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Listbar.prototype.move = function(offset) {
|
|
|
|
this.select(this.selected + offset);
|
|
|
|
};
|
|
|
|
|
|
|
|
Listbar.prototype.moveLeft = function(offset) {
|
|
|
|
this.move(-(offset || 1));
|
|
|
|
};
|
|
|
|
|
|
|
|
Listbar.prototype.moveRight = function(offset) {
|
|
|
|
this.move(offset || 1);
|
2013-07-14 01:44:30 +00:00
|
|
|
};
|
|
|
|
|
2014-06-02 04:43:02 +00:00
|
|
|
Listbar.prototype.selectTab = function(index) {
|
|
|
|
var item = this.items[index];
|
|
|
|
if (item) {
|
|
|
|
if (item._.cmd.callback) {
|
|
|
|
item._.cmd.callback();
|
|
|
|
}
|
|
|
|
this.select(index);
|
|
|
|
this.screen.render();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-25 02:34:57 +00:00
|
|
|
/**
|
|
|
|
* Terminal
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Terminal(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!(this instanceof Node)) {
|
|
|
|
return new Terminal(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
options = options || {};
|
2015-02-12 01:20:22 +00:00
|
|
|
options.scrollable = false;
|
2015-01-25 02:34:57 +00:00
|
|
|
|
|
|
|
Box.call(this, options);
|
|
|
|
|
2015-02-11 03:36:52 +00:00
|
|
|
this.handler = options.handler;
|
|
|
|
this.shell = options.shell || process.env.SHELL || 'sh';
|
|
|
|
this.args = options.args || [];
|
|
|
|
|
2015-02-12 01:20:22 +00:00
|
|
|
this.cursor = this.options.cursor;
|
|
|
|
this.cursorBlink = this.options.cursorBlink;
|
|
|
|
this.screenKeys = this.options.screenKeys;
|
|
|
|
|
2015-02-11 03:36:52 +00:00
|
|
|
this.style = this.style || {};
|
|
|
|
this.style.bg = this.style.bg || 'default';
|
|
|
|
this.style.fg = this.style.fg || 'default';
|
|
|
|
|
|
|
|
this.bootstrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
Terminal.prototype.__proto__ = Box.prototype;
|
|
|
|
|
|
|
|
Terminal.prototype.type = 'terminal';
|
|
|
|
|
|
|
|
Terminal.prototype.bootstrap = function() {
|
|
|
|
var self = this;
|
|
|
|
|
2015-01-25 02:34:57 +00:00
|
|
|
var element = {
|
|
|
|
// window
|
|
|
|
get document() { return element; },
|
|
|
|
navigator: { userAgent: 'node.js' },
|
|
|
|
|
|
|
|
// document
|
|
|
|
get defaultView() { return element; },
|
|
|
|
get documentElement() { return element; },
|
|
|
|
createElement: function() { return element; },
|
|
|
|
|
|
|
|
// element
|
|
|
|
get ownerDocument() { return element; },
|
|
|
|
addEventListener: function() {},
|
|
|
|
removeEventListener: function() {},
|
|
|
|
getElementsByTagName: function(name) { return [element]; },
|
|
|
|
getElementById: function() { return element; },
|
2015-02-11 03:36:52 +00:00
|
|
|
parentNode: null,
|
|
|
|
offsetParent: null,
|
2015-01-25 02:34:57 +00:00
|
|
|
appendChild: function() {},
|
|
|
|
removeChild: function() {},
|
|
|
|
setAttribute: function() {},
|
|
|
|
getAttribute: function() {},
|
|
|
|
style: {},
|
|
|
|
focus: function() {},
|
|
|
|
blur: function() {},
|
|
|
|
console: console
|
|
|
|
};
|
|
|
|
|
2015-02-11 03:36:52 +00:00
|
|
|
element.parentNode = element;
|
|
|
|
element.offsetParent = element;
|
|
|
|
|
2015-01-25 02:34:57 +00:00
|
|
|
this.term = require('term.js')({
|
2015-02-11 03:36:52 +00:00
|
|
|
termName: 'xterm',
|
2015-01-25 02:34:57 +00:00
|
|
|
cols: this.width - this.iwidth,
|
|
|
|
rows: this.height - this.iheight,
|
|
|
|
context: element,
|
|
|
|
document: element,
|
|
|
|
body: element,
|
2015-02-12 01:20:22 +00:00
|
|
|
parent: element,
|
|
|
|
cursorBlink: this.cursorBlink,
|
|
|
|
screenKeys: this.screenKeys
|
2015-01-25 02:34:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.term.refresh = function() {
|
|
|
|
self.screen.render();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.term.keyDown = function() {};
|
|
|
|
this.term.keyPress = function() {};
|
|
|
|
|
|
|
|
this.term.open(element);
|
|
|
|
|
|
|
|
// Emits key sequences in html-land.
|
|
|
|
// Technically not necessary here.
|
|
|
|
// In reality if we wanted to be neat, we would overwrite the keyDown and
|
|
|
|
// keyPress methods with our own node.js-keys->terminal-keys methods, but
|
|
|
|
// since all the keys are already coming in as escape sequences, we can just
|
|
|
|
// send the input directly to the handler/socket (see below).
|
|
|
|
// this.term.on('data', function(data) {
|
2015-02-11 03:36:52 +00:00
|
|
|
// self.handler(data);
|
2015-01-25 02:34:57 +00:00
|
|
|
// });
|
|
|
|
|
|
|
|
// Incoming keys and mouse inputs.
|
|
|
|
// NOTE: Cannot pass mouse events - coordinates will be off!
|
|
|
|
this.screen.program.input.on('data', function(data) {
|
2015-02-11 03:36:52 +00:00
|
|
|
if (self.screen.focused === self && !self._isMouse(data)) {
|
|
|
|
self.handler(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.screen.on('mouse', function(data) {
|
|
|
|
if (self.screen.focused !== self) return;
|
|
|
|
if (data.x < self.left + self.ileft) return;
|
|
|
|
if (data.y < self.top + self.itop) return;
|
|
|
|
if (data.x > self.left - self.ileft + self.width) return;
|
|
|
|
if (data.y > self.top - self.itop + self.height) return;
|
|
|
|
if (self.term.x10Mouse
|
|
|
|
|| self.term.vt200Mouse
|
|
|
|
|| self.term.normalMouse
|
|
|
|
|| self.term.mouseEvents
|
|
|
|
|| self.term.utfMouse
|
|
|
|
|| self.term.sgrMouse
|
|
|
|
|| self.term.urxvtMouse) {
|
|
|
|
;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var b = data.raw[0];
|
|
|
|
//var x = data.x - (self.left + self.ileft);
|
|
|
|
//var y = data.y - (self.top + self.itop);
|
|
|
|
var x = data.x - self.left;
|
|
|
|
var y = data.y - self.top;
|
|
|
|
if (self.term.urxvtMouse) {
|
|
|
|
var s = '\x1b['
|
|
|
|
+ String.fromCharCode(b)
|
|
|
|
+ ';'
|
|
|
|
+ String.fromCharCode(x)
|
|
|
|
+ ';'
|
|
|
|
+ String.fromCharCode(y);
|
|
|
|
+ 'M';
|
|
|
|
} else if (self.term.sgrMouse) {
|
|
|
|
if (!self.screen.program.sgrMouse) {
|
|
|
|
b -= 32;
|
|
|
|
}
|
|
|
|
var s = '\x1b[<'
|
|
|
|
+ String.fromCharCode(b)
|
|
|
|
+ ';'
|
|
|
|
+ String.fromCharCode(x)
|
|
|
|
+ ';'
|
|
|
|
+ String.fromCharCode(y)
|
|
|
|
+ (data.action === 'mousedown' ? 'M' : 'm');
|
|
|
|
} else {
|
|
|
|
var s = '\x1b[M'
|
|
|
|
+ String.fromCharCode(b)
|
|
|
|
+ String.fromCharCode(x + 32)
|
|
|
|
+ String.fromCharCode(y + 32);
|
|
|
|
}
|
|
|
|
self.handler(s);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on('focus', function() {
|
2015-02-12 01:20:22 +00:00
|
|
|
self.term.focus();
|
2015-02-11 03:36:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.on('blur', function() {
|
2015-02-12 01:20:22 +00:00
|
|
|
self.term.blur();
|
2015-01-25 02:34:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.term.on('title', function(title) {
|
|
|
|
self.title = title;
|
|
|
|
self.emit('title', title);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on('resize', function() {
|
2015-01-31 20:26:19 +00:00
|
|
|
nextTick(function() {
|
2015-01-25 02:34:57 +00:00
|
|
|
self.term.resize(self.width - self.iwidth, self.height - self.iheight);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.once('render', function() {
|
|
|
|
self.term.resize(self.width - self.iwidth, self.height - self.iheight);
|
|
|
|
});
|
|
|
|
|
2015-02-11 03:36:52 +00:00
|
|
|
this.pty = require('pty.js').fork(this.shell, this.args, {
|
|
|
|
name: 'xterm',
|
|
|
|
cols: this.width - this.iwidth,
|
|
|
|
rows: this.height - this.iheight,
|
|
|
|
cwd: process.env.HOME,
|
|
|
|
env: process.env
|
|
|
|
});
|
2015-01-25 02:34:57 +00:00
|
|
|
|
2015-02-11 03:36:52 +00:00
|
|
|
this.on('resize', function() {
|
|
|
|
nextTick(function() {
|
|
|
|
self.pty.resize(self.width - self.iwidth, self.height - self.iheight);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.handler = function(data) {
|
|
|
|
self.pty.write(data);
|
|
|
|
self.screen.render();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.pty.on('data', function(data) {
|
|
|
|
self.write(data);
|
|
|
|
self.screen.render();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.screen.on('keypress', function() {
|
|
|
|
self.screen.render();
|
|
|
|
});
|
|
|
|
};
|
2015-01-25 02:34:57 +00:00
|
|
|
|
|
|
|
Terminal.prototype.write = function(data) {
|
|
|
|
return this.term.write(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
Terminal.prototype.render = function() {
|
|
|
|
var ret = this._render();
|
|
|
|
if (!ret) return;
|
|
|
|
|
2015-02-12 01:20:22 +00:00
|
|
|
this.dattr = this.sattr(this.style, this.style.fg, this.style.bg);
|
2015-02-11 03:36:52 +00:00
|
|
|
|
2015-01-25 02:34:57 +00:00
|
|
|
var xi = ret.xi + this.ileft
|
|
|
|
, xl = ret.xl - this.iright
|
|
|
|
, yi = ret.yi + this.itop
|
2015-02-11 03:36:52 +00:00
|
|
|
, yl = ret.yl - this.ibottom
|
|
|
|
, cursor;
|
2015-01-25 02:34:57 +00:00
|
|
|
|
|
|
|
var scrollback = this.term.lines.length - (yl - yi);
|
|
|
|
|
|
|
|
for (var y = yi; y < yl; y++) {
|
|
|
|
var line = this.screen.lines[y];
|
2015-02-11 03:36:52 +00:00
|
|
|
if (!line || !this.term.lines[scrollback + y - yi]) break;
|
|
|
|
|
|
|
|
if (y === yi + this.term.y
|
2015-02-12 01:20:22 +00:00
|
|
|
&& this.term.cursorState
|
2015-02-11 03:36:52 +00:00
|
|
|
&& this.screen.focused === this
|
|
|
|
&& (this.term.ydisp === this.term.ybase || this.term.selectMode)
|
|
|
|
&& !this.term.cursorHidden) {
|
|
|
|
cursor = xi + this.term.x;
|
|
|
|
} else {
|
|
|
|
cursor = -1;
|
|
|
|
}
|
|
|
|
|
2015-01-25 02:34:57 +00:00
|
|
|
for (var x = xi; x < xl; x++) {
|
2015-02-11 03:36:52 +00:00
|
|
|
if (!line[x] || !this.term.lines[scrollback + y - yi][x - xi]) break;
|
2015-02-12 01:20:22 +00:00
|
|
|
|
2015-02-13 06:37:53 +00:00
|
|
|
line[x][0] = this.term.lines[scrollback + y - yi][x - xi][0];
|
|
|
|
|
2015-02-11 03:36:52 +00:00
|
|
|
if (x === cursor) {
|
2015-02-12 01:20:22 +00:00
|
|
|
if (this.cursor === 'line') {
|
|
|
|
line[x][0] = this.dattr;
|
|
|
|
line[x][1] = '\u2502';
|
2015-02-13 06:37:53 +00:00
|
|
|
continue;
|
2015-02-12 01:20:22 +00:00
|
|
|
} else if (this.cursor === 'underline') {
|
2015-02-13 06:37:53 +00:00
|
|
|
line[x][0] = this.dattr | (2 << 18);
|
2015-02-12 01:20:22 +00:00
|
|
|
} else if (this.cursor === 'block' || !this.cursor) {
|
|
|
|
line[x][0] = this.dattr | (8 << 18);
|
|
|
|
}
|
2015-02-11 03:36:52 +00:00
|
|
|
}
|
2015-02-12 01:20:22 +00:00
|
|
|
|
|
|
|
line[x][1] = this.term.lines[scrollback + y - yi][x - xi][1];
|
|
|
|
|
2015-02-11 03:36:52 +00:00
|
|
|
// default foreground = 257
|
2015-02-12 01:20:22 +00:00
|
|
|
if (((line[x][0] >> 9) & 0x1ff) === 257) {
|
|
|
|
if (!((line[x][0] >> 18) & 8)) {
|
|
|
|
line[x][0] &= ~(0x1ff << 9);
|
|
|
|
line[x][0] |= ((this.dattr >> 9) & 0x1ff) << 9;
|
|
|
|
}
|
2015-02-11 03:36:52 +00:00
|
|
|
}
|
2015-02-12 01:20:22 +00:00
|
|
|
|
2015-02-11 03:36:52 +00:00
|
|
|
// default background = 256
|
2015-02-12 01:20:22 +00:00
|
|
|
if ((line[x][0] & 0x1ff) === 256) {
|
|
|
|
if (!((line[x][0] >> 18) & 8)) {
|
|
|
|
line[x][0] &= ~0x1ff;
|
|
|
|
line[x][0] |= this.dattr & 0x1ff;
|
|
|
|
}
|
2015-02-11 03:36:52 +00:00
|
|
|
}
|
2015-01-25 02:34:57 +00:00
|
|
|
}
|
2015-02-11 03:36:52 +00:00
|
|
|
|
2015-01-25 02:34:57 +00:00
|
|
|
line.dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2015-02-11 03:36:52 +00:00
|
|
|
Terminal.prototype._isMouse = function(buf) {
|
|
|
|
var s = buf;
|
|
|
|
if (Buffer.isBuffer(s)) {
|
|
|
|
if (s[0] > 127 && s[1] === undefined) {
|
|
|
|
s[0] -= 128;
|
|
|
|
s = '\x1b' + s.toString('utf-8');
|
|
|
|
} else {
|
|
|
|
s = s.toString('utf-8');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (buf[0] === 0x1b && buf[1] === 0x5b && buf[2] === 0x4d)
|
2015-02-12 01:20:22 +00:00
|
|
|
|| /^\x1b\[M([\x00\u0020-\uffff]{3})/.test(s)
|
2015-02-11 03:36:52 +00:00
|
|
|
|| /^\x1b\[(\d+;\d+;\d+)M/.test(s)
|
|
|
|
|| /^\x1b\[<(\d+;\d+;\d+)([mM])/.test(s)
|
|
|
|
|| /^\x1b\[<(\d+;\d+;\d+;\d+)&w/.test(s)
|
|
|
|
|| /^\x1b\[24([0135])~\[(\d+),(\d+)\]\r/.test(s)
|
|
|
|
|| /^\x1b\[(O|I)/.test(s);
|
|
|
|
};
|
|
|
|
|
2015-02-12 01:20:22 +00:00
|
|
|
Terminal.prototype.setScroll =
|
|
|
|
Terminal.prototype.scrollTo = function(offset, always) {
|
|
|
|
this.term.ydisp = offset;
|
|
|
|
return this.emit('scroll');
|
|
|
|
};
|
|
|
|
|
|
|
|
Terminal.prototype.getScroll = function() {
|
|
|
|
return this.term.ydisp;
|
|
|
|
};
|
|
|
|
|
|
|
|
Terminal.prototype.scroll = function(offset, always) {
|
|
|
|
this.term.scrollDisp(offset);
|
|
|
|
return this.emit('scroll');
|
|
|
|
};
|
|
|
|
|
|
|
|
Terminal.prototype.resetScroll = function() {
|
|
|
|
this.term.ydisp = 0;
|
|
|
|
this.term.ybase = 0;
|
|
|
|
return this.emit('scroll');
|
|
|
|
};
|
|
|
|
|
|
|
|
Terminal.prototype.getScrollHeight = function() {
|
|
|
|
return this.term.rows - 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
Terminal.prototype.getScrollPerc = function(s) {
|
|
|
|
return (this.term.ydisp / this.term.ybase) * 100;
|
|
|
|
};
|
|
|
|
|
|
|
|
Terminal.prototype.setScrollPerc = function(i) {
|
|
|
|
return this.setScroll((i / 100) * this.term.ybase | 0);
|
|
|
|
};
|
|
|
|
|
2015-02-01 21:43:52 +00:00
|
|
|
/**
|
|
|
|
* Image
|
|
|
|
* Good example of w3mimgdisplay commands:
|
|
|
|
* https://github.com/hut/ranger/blob/master/ranger/ext/img_display.py
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Image(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!(this instanceof Node)) {
|
|
|
|
return new Image(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
Box.call(this, options);
|
|
|
|
|
2015-02-12 01:20:22 +00:00
|
|
|
if (options.w3m) {
|
|
|
|
Image.w3mdisplay = options.w3m;
|
|
|
|
}
|
|
|
|
|
2015-02-09 08:09:30 +00:00
|
|
|
if (Image.hasW3MDisplay == null) {
|
2015-02-09 07:42:44 +00:00
|
|
|
if (fs.existsSync(Image.w3mdisplay)) {
|
|
|
|
Image.hasW3MDisplay = true;
|
2015-02-12 01:20:22 +00:00
|
|
|
} else if (options.search !== false) {
|
|
|
|
var file = findFile('/', 'w3mimgdisplay');
|
|
|
|
if (file) {
|
|
|
|
Image.hasW3MDisplay = true;
|
|
|
|
Image.w3mdisplay = file;
|
|
|
|
} else {
|
|
|
|
Image.hasW3MDisplay = false;
|
|
|
|
}
|
2015-02-09 07:42:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 01:20:22 +00:00
|
|
|
this.on('hide', function() {
|
|
|
|
self._lastFile = self.file;
|
|
|
|
self.clearImage();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on('show', function() {
|
|
|
|
if (!self._lastFile) return;
|
|
|
|
self.setImage(self._lastFile);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on('detach', function() {
|
|
|
|
self._lastFile = self.file;
|
|
|
|
self.clearImage();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on('attach', function() {
|
|
|
|
if (!self._lastFile) return;
|
|
|
|
self.setImage(self._lastFile);
|
|
|
|
});
|
|
|
|
|
2015-02-09 09:43:16 +00:00
|
|
|
if (this.options.file || this.options.img) {
|
|
|
|
this.setImage(this.options.file || this.options.img);
|
2015-02-01 21:43:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Image.prototype.__proto__ = Box.prototype;
|
|
|
|
|
|
|
|
Image.prototype.type = 'image';
|
|
|
|
|
2015-02-09 07:42:44 +00:00
|
|
|
Image.w3mdisplay = '/usr/lib/w3m/w3mimgdisplay';
|
2015-02-01 21:43:52 +00:00
|
|
|
|
|
|
|
Image.prototype.render = function() {
|
|
|
|
var ret = this._render();
|
|
|
|
if (!ret) return;
|
2015-02-09 09:43:16 +00:00
|
|
|
if (!this._noImage) {
|
2015-02-10 03:49:33 +00:00
|
|
|
this.setImage(this.file);
|
2015-02-09 09:43:16 +00:00
|
|
|
}
|
2015-02-01 21:43:52 +00:00
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
Image.prototype.spawn = function(file, args, opt, callback) {
|
|
|
|
var screen = this.screen
|
|
|
|
, opt = opt || {}
|
|
|
|
, spawn = require('child_process').spawn
|
|
|
|
, ps;
|
|
|
|
|
|
|
|
ps = spawn(file, args, opt);
|
|
|
|
|
2015-02-09 07:42:44 +00:00
|
|
|
ps.on('error', function(err) {
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(err);
|
|
|
|
});
|
2015-02-01 21:43:52 +00:00
|
|
|
|
2015-02-09 07:42:44 +00:00
|
|
|
ps.on('exit', function(code) {
|
|
|
|
if (!callback) return;
|
|
|
|
if (code !== 0) return callback(new Error('Exit Code: ' + code));
|
|
|
|
return callback(null, code === 0);
|
|
|
|
});
|
2015-02-01 21:43:52 +00:00
|
|
|
|
|
|
|
return ps;
|
|
|
|
};
|
|
|
|
|
|
|
|
Image.prototype.setImage = function(img, callback) {
|
|
|
|
var self = this;
|
2015-02-09 07:42:44 +00:00
|
|
|
|
2015-02-09 09:43:16 +00:00
|
|
|
if (Image.hasW3MDisplay === false) {
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(new Error('W3M Image Display not available.'));
|
|
|
|
}
|
|
|
|
|
2015-02-09 07:42:44 +00:00
|
|
|
if (!img) {
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(new Error('No image.'));
|
|
|
|
}
|
|
|
|
|
2015-02-10 03:49:33 +00:00
|
|
|
this.file = img;
|
|
|
|
|
|
|
|
function renderImage(ratio, callback) {
|
|
|
|
// clearImage unsets these:
|
|
|
|
var _file = self.file;
|
|
|
|
var _lastSize = self._lastSize;
|
|
|
|
return self.clearImage(function(err) {
|
|
|
|
if (err) return callback(err);
|
2015-02-01 21:43:52 +00:00
|
|
|
|
2015-02-10 03:49:33 +00:00
|
|
|
self.file = _file;
|
|
|
|
self._lastSize = _lastSize;
|
2015-02-01 21:43:52 +00:00
|
|
|
|
2015-02-09 09:43:16 +00:00
|
|
|
var opt = {
|
|
|
|
stdio: 'pipe',
|
|
|
|
env: process.env,
|
|
|
|
cwd: process.env.HOME
|
|
|
|
};
|
2015-02-01 21:43:52 +00:00
|
|
|
|
2015-02-09 09:43:16 +00:00
|
|
|
var ps = self.spawn(Image.w3mdisplay, [], opt, function(err, success) {
|
2015-02-01 21:43:52 +00:00
|
|
|
if (!callback) return;
|
|
|
|
return err
|
|
|
|
? callback(err)
|
|
|
|
: callback(null, success);
|
|
|
|
});
|
|
|
|
|
2015-02-10 03:49:33 +00:00
|
|
|
var width = self.width * ratio.tw | 0
|
|
|
|
, height = self.height * ratio.th | 0
|
|
|
|
, left = self.left * ratio.tw | 0
|
|
|
|
, top = self.top * ratio.th | 0;
|
2015-02-01 21:43:52 +00:00
|
|
|
|
|
|
|
var input = '0;1;'
|
|
|
|
+ left + ';'
|
|
|
|
+ top + ';'
|
|
|
|
+ width + ';'
|
|
|
|
+ height + ';;;;;'
|
|
|
|
+ img
|
|
|
|
+ '\n4;\n3;\n';
|
|
|
|
|
|
|
|
self._props = {
|
|
|
|
left: left,
|
|
|
|
top: top,
|
|
|
|
width: width,
|
|
|
|
height: height
|
|
|
|
};
|
|
|
|
|
|
|
|
ps.stdin.write(input);
|
|
|
|
ps.stdin.end();
|
|
|
|
});
|
2015-02-09 09:43:16 +00:00
|
|
|
}
|
|
|
|
|
2015-02-10 03:49:33 +00:00
|
|
|
return this.getPixelRatio(function(err, ratio) {
|
2015-02-09 09:43:16 +00:00
|
|
|
if (err) {
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2015-02-10 03:49:33 +00:00
|
|
|
return renderImage(ratio, function(err, success) {
|
2015-02-09 09:43:16 +00:00
|
|
|
if (err) {
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self.shrink || self.options.autofit) {
|
|
|
|
delete self.shrink;
|
|
|
|
delete self.options.shrink;
|
|
|
|
self.options.autofit = true;
|
|
|
|
return self.imageSize(function(err, size) {
|
2015-02-11 03:36:52 +00:00
|
|
|
if (err) {
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(err);
|
|
|
|
}
|
2015-02-09 09:43:16 +00:00
|
|
|
|
|
|
|
if (self._lastSize
|
2015-02-10 03:49:33 +00:00
|
|
|
&& ratio.tw === self._lastSize.tw
|
|
|
|
&& ratio.th === self._lastSize.th
|
2015-02-09 09:43:16 +00:00
|
|
|
&& size.width === self._lastSize.width
|
|
|
|
&& size.height === self._lastSize.height
|
|
|
|
&& self.left === self._lastSize.left
|
|
|
|
&& self.top === self._lastSize.top) {
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(null, success);
|
|
|
|
}
|
|
|
|
|
|
|
|
self._lastSize = {
|
2015-02-10 03:49:33 +00:00
|
|
|
tw: ratio.tw,
|
|
|
|
th: ratio.th,
|
2015-02-09 09:43:16 +00:00
|
|
|
width: size.width,
|
|
|
|
height: size.height,
|
|
|
|
left: self.left,
|
|
|
|
top: self.top
|
|
|
|
};
|
|
|
|
|
2015-02-10 03:49:33 +00:00
|
|
|
self.position.width = size.width / ratio.tw | 0;
|
|
|
|
self.position.height = size.height / ratio.th | 0;
|
2015-02-09 09:43:16 +00:00
|
|
|
|
|
|
|
self._noImage = true;
|
|
|
|
self.screen.render();
|
|
|
|
self._noImage = false;
|
|
|
|
|
2015-02-10 03:49:33 +00:00
|
|
|
return renderImage(ratio, callback);
|
2015-02-09 09:43:16 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(null, success);
|
|
|
|
});
|
2015-02-01 21:43:52 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Image.prototype.clearImage = function(callback) {
|
|
|
|
var self = this;
|
|
|
|
|
2015-02-09 09:43:16 +00:00
|
|
|
if (Image.hasW3MDisplay === false) {
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(new Error('W3M Image Display not available.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-01 21:43:52 +00:00
|
|
|
if (!this._props) {
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
var opt = {
|
|
|
|
stdio: 'pipe',
|
|
|
|
env: process.env,
|
|
|
|
cwd: process.env.HOME
|
|
|
|
};
|
|
|
|
|
2015-02-09 09:43:16 +00:00
|
|
|
var ps = this.spawn(Image.w3mdisplay, [], opt, function(err, success) {
|
2015-02-01 21:43:52 +00:00
|
|
|
if (!callback) return;
|
|
|
|
return err
|
|
|
|
? callback(err)
|
|
|
|
: callback(null, success);
|
|
|
|
});
|
|
|
|
|
2015-02-09 09:43:16 +00:00
|
|
|
var width = this._props.width + 2
|
|
|
|
, height = this._props.height + 2
|
2015-02-01 21:43:52 +00:00
|
|
|
, left = this._props.left
|
|
|
|
, top = this._props.top;
|
|
|
|
|
|
|
|
var input = '6;'
|
|
|
|
+ left + ';'
|
|
|
|
+ top + ';'
|
|
|
|
+ width + ';'
|
|
|
|
+ height
|
|
|
|
+ '\n4;\n3;\n';
|
|
|
|
|
2015-02-09 09:43:16 +00:00
|
|
|
delete this.file;
|
2015-02-10 03:49:33 +00:00
|
|
|
delete this._props;
|
2015-02-09 09:43:16 +00:00
|
|
|
delete this._lastSize;
|
2015-02-01 21:43:52 +00:00
|
|
|
|
|
|
|
ps.stdin.write(input);
|
|
|
|
ps.stdin.end();
|
|
|
|
};
|
|
|
|
|
2015-02-09 07:42:44 +00:00
|
|
|
Image.prototype.imageSize = function(callback) {
|
2015-02-01 21:43:52 +00:00
|
|
|
var self = this;
|
2015-02-09 09:43:16 +00:00
|
|
|
var img = this.file;
|
|
|
|
|
|
|
|
if (Image.hasW3MDisplay === false) {
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(new Error('W3M Image Display not available.'));
|
|
|
|
}
|
2015-02-09 07:42:44 +00:00
|
|
|
|
|
|
|
if (!img) {
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(new Error('No image.'));
|
|
|
|
}
|
2015-02-01 21:43:52 +00:00
|
|
|
|
|
|
|
var opt = {
|
|
|
|
stdio: 'pipe',
|
|
|
|
env: process.env,
|
|
|
|
cwd: process.env.HOME
|
|
|
|
};
|
|
|
|
|
2015-02-09 09:43:16 +00:00
|
|
|
var ps = this.spawn(Image.w3mdisplay, [], opt);
|
2015-02-01 21:43:52 +00:00
|
|
|
|
|
|
|
var buf = '';
|
|
|
|
|
|
|
|
ps.stdout.setEncoding('utf8');
|
|
|
|
|
|
|
|
ps.stdout.on('data', function(data) {
|
|
|
|
buf += data;
|
|
|
|
});
|
|
|
|
|
2015-02-09 07:42:44 +00:00
|
|
|
ps.on('error', function(err) {
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(err);
|
|
|
|
});
|
2015-02-01 21:43:52 +00:00
|
|
|
|
|
|
|
ps.on('exit', function() {
|
|
|
|
if (!callback) return;
|
|
|
|
var size = buf.trim().split(/\s+/);
|
|
|
|
return callback(null, {
|
|
|
|
raw: buf.trim(),
|
|
|
|
width: +size[0],
|
|
|
|
height: +size[1]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
var input = '5;' + img + '\n';
|
|
|
|
|
|
|
|
ps.stdin.write(input);
|
|
|
|
ps.stdin.end();
|
|
|
|
};
|
|
|
|
|
|
|
|
Image.prototype.termSize = function(callback) {
|
|
|
|
var self = this;
|
2015-02-09 09:43:16 +00:00
|
|
|
|
|
|
|
if (Image.hasW3MDisplay === false) {
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(new Error('W3M Image Display not available.'));
|
|
|
|
}
|
2015-02-01 21:43:52 +00:00
|
|
|
|
|
|
|
var opt = {
|
|
|
|
stdio: 'pipe',
|
|
|
|
env: process.env,
|
|
|
|
cwd: process.env.HOME
|
|
|
|
};
|
|
|
|
|
2015-02-09 09:43:16 +00:00
|
|
|
var ps = this.spawn(Image.w3mdisplay, ['-test'], opt);
|
2015-02-01 21:43:52 +00:00
|
|
|
|
|
|
|
var buf = '';
|
|
|
|
|
|
|
|
ps.stdout.setEncoding('utf8');
|
|
|
|
|
|
|
|
ps.stdout.on('data', function(data) {
|
|
|
|
buf += data;
|
|
|
|
});
|
|
|
|
|
2015-02-09 07:42:44 +00:00
|
|
|
ps.on('error', function(err) {
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(err);
|
|
|
|
});
|
2015-02-01 21:43:52 +00:00
|
|
|
|
|
|
|
ps.on('exit', function() {
|
|
|
|
if (!callback) return;
|
2015-02-10 03:49:33 +00:00
|
|
|
|
|
|
|
if (!buf.trim()) {
|
2015-02-12 01:20:22 +00:00
|
|
|
// Bug: w3mimgdisplay will sometimes
|
|
|
|
// output nothing. Try again:
|
2015-02-10 03:49:33 +00:00
|
|
|
return self.termSize(callback);
|
|
|
|
}
|
|
|
|
|
2015-02-01 21:43:52 +00:00
|
|
|
var size = buf.trim().split(/\s+/);
|
2015-02-10 03:49:33 +00:00
|
|
|
|
2015-02-01 21:43:52 +00:00
|
|
|
return callback(null, {
|
|
|
|
raw: buf.trim(),
|
|
|
|
width: +size[0],
|
|
|
|
height: +size[1]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
ps.stdin.end();
|
|
|
|
};
|
|
|
|
|
2015-02-10 03:49:33 +00:00
|
|
|
Image.prototype.getPixelRatio = function(callback) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// XXX We could cache this, but sometimes it's better
|
|
|
|
// to recalculate to be pixel perfect.
|
|
|
|
// if (this._ratio) {
|
|
|
|
// return callback(null, this._ratio);
|
|
|
|
// }
|
|
|
|
|
|
|
|
return this.termSize(function(err, dimensions) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
|
|
|
|
self._ratio = {
|
|
|
|
tw: dimensions.width / self.screen.width,
|
|
|
|
th: dimensions.height / self.screen.height
|
|
|
|
};
|
|
|
|
|
|
|
|
return callback(null, self._ratio);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-02-09 07:42:44 +00:00
|
|
|
Image.prototype.displayImage = function(callback) {
|
2015-02-09 09:43:16 +00:00
|
|
|
return this.screen.displayImage(this.file, callback);
|
2015-02-09 07:42:44 +00:00
|
|
|
};
|
|
|
|
|
2013-06-06 14:24:03 +00:00
|
|
|
/**
|
|
|
|
* Helpers
|
|
|
|
*/
|
|
|
|
|
2014-01-12 11:08:16 +00:00
|
|
|
function generateTags(style, text) {
|
|
|
|
var open = ''
|
|
|
|
, close = '';
|
|
|
|
|
|
|
|
Object.keys(style).forEach(function(key) {
|
|
|
|
var val = style[key];
|
|
|
|
if (typeof val === 'string') {
|
|
|
|
val = val.replace(/^light(?!-)/, 'light-');
|
|
|
|
open = '{' + val + '-' + key + '}' + open;
|
|
|
|
close += '{/' + val + '-' + key + '}';
|
|
|
|
} else {
|
2014-01-18 01:54:59 +00:00
|
|
|
if (val) {
|
|
|
|
open = '{' + key + '}' + open;
|
|
|
|
close += '{/' + key + '}';
|
|
|
|
}
|
2014-01-12 11:08:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (text != null) {
|
|
|
|
return open + text + close;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
open: open,
|
|
|
|
close: close
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-01-11 22:52:33 +00:00
|
|
|
function merge(a, b) {
|
|
|
|
Object.keys(b).forEach(function(key) {
|
|
|
|
a[key] = b[key];
|
|
|
|
});
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
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-08-10 14:30:05 +00:00
|
|
|
var wideChars = new RegExp('(['
|
2013-07-28 15:19:04 +00:00
|
|
|
+ '\\uff01-\\uffbe'
|
|
|
|
+ '\\uffc2-\\uffc7'
|
|
|
|
+ '\\uffca-\\uffcf'
|
|
|
|
+ '\\uffd2-\\uffd7'
|
|
|
|
+ '\\uffda-\\uffdc'
|
|
|
|
+ '\\uffe0-\\uffe6'
|
|
|
|
+ '\\uffe8-\\uffee'
|
|
|
|
+ '])', 'g');
|
|
|
|
|
2015-02-12 01:20:22 +00:00
|
|
|
function findFile(start, target) {
|
|
|
|
return (function read(dir) {
|
|
|
|
var files, file, stat, out;
|
|
|
|
|
|
|
|
if (dir === '/dev' || dir === '/sys' || dir === '/proc') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
files = fs.readdirSync(dir);
|
|
|
|
} catch (e) {
|
|
|
|
files = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < files.length; i++) {
|
|
|
|
file = files[i];
|
|
|
|
|
|
|
|
if (file === target) {
|
|
|
|
return (dir === '/' ? '' : dir) + '/' + file;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
stat = fs.statSync((dir === '/' ? '' : dir) + '/' + file);
|
|
|
|
} catch (e) {
|
|
|
|
stat = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat && stat.isDirectory()) {
|
|
|
|
out = read((dir === '/' ? '' : dir) + '/' + file);
|
|
|
|
if (out) return out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
})(start);
|
|
|
|
}
|
|
|
|
|
2014-04-26 10:57:37 +00:00
|
|
|
/**
|
|
|
|
* Helpers
|
|
|
|
*/
|
|
|
|
|
|
|
|
var helpers = {};
|
|
|
|
|
|
|
|
// Escape text for tag-enabled elements.
|
|
|
|
helpers.escape = function(text) {
|
|
|
|
return text.replace(/[{}]/g, function(ch) {
|
|
|
|
return ch === '{' ? '{open}' : '{close}';
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-06-01 02:09:07 +00:00
|
|
|
/**
|
|
|
|
* Expose
|
|
|
|
*/
|
|
|
|
|
2013-08-04 09:50:13 +00:00
|
|
|
exports.Node = exports.node = Node;
|
2013-07-03 23:46:06 +00:00
|
|
|
exports.Screen = exports.screen = Screen;
|
2013-08-04 09:50:13 +00:00
|
|
|
exports.Element = exports.element = Element;
|
2013-07-03 23:46:06 +00:00
|
|
|
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;
|
2013-07-17 05:53:34 +00:00
|
|
|
exports.Form = exports.form = Form;
|
2013-07-03 23:46:06 +00:00
|
|
|
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;
|
2013-07-17 05:53:34 +00:00
|
|
|
exports.RadioSet = exports.radioset = RadioSet;
|
2013-07-14 01:44:30 +00:00
|
|
|
exports.RadioButton = exports.radiobutton = RadioButton;
|
2013-07-17 05:53:34 +00:00
|
|
|
|
2013-07-14 01:44:30 +00:00
|
|
|
exports.Prompt = exports.prompt = Prompt;
|
|
|
|
exports.Question = exports.question = Question;
|
|
|
|
exports.Message = exports.message = Message;
|
|
|
|
exports.Loading = exports.loading = Loading;
|
|
|
|
exports.Listbar = exports.listbar = Listbar;
|
2014-04-26 10:57:37 +00:00
|
|
|
|
2015-01-25 02:34:57 +00:00
|
|
|
exports.Terminal = exports.terminal = Terminal;
|
2015-02-01 21:43:52 +00:00
|
|
|
exports.Image = exports.image = Image;
|
2015-01-25 02:34:57 +00:00
|
|
|
|
2014-04-26 10:57:37 +00:00
|
|
|
exports.helpers = helpers;
|