2015-12-11 17:49:42 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
|
2019-05-09 20:41:18 +00:00
|
|
|
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
|
|
|
2015-12-11 17:49:42 +00:00
|
|
|
var _ansi = require('ansi');
|
|
|
|
|
|
|
|
var _ansi2 = _interopRequireDefault(_ansi);
|
|
|
|
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
|
2019-05-09 20:41:18 +00:00
|
|
|
var _process = process,
|
|
|
|
stdout = _process.stdout,
|
|
|
|
stdin = _process.stdin;
|
|
|
|
|
2015-12-11 17:49:42 +00:00
|
|
|
|
|
|
|
var listeners = [];
|
|
|
|
|
2019-05-09 20:41:18 +00:00
|
|
|
var prefix = '\x1B';
|
2015-12-11 17:49:42 +00:00
|
|
|
var keys = {
|
|
|
|
right: prefix + '[C',
|
|
|
|
up: prefix + '[A',
|
|
|
|
left: prefix + '[D',
|
|
|
|
down: prefix + '[B',
|
2019-05-09 20:41:18 +00:00
|
|
|
exit: '\x03',
|
2015-12-11 17:49:42 +00:00
|
|
|
space: ' '
|
|
|
|
};
|
|
|
|
|
2019-05-09 20:41:18 +00:00
|
|
|
var Interface = function () {
|
2015-12-11 17:49:42 +00:00
|
|
|
function Interface() {
|
2016-03-15 13:20:15 +00:00
|
|
|
var _this = this;
|
|
|
|
|
2019-05-09 20:41:18 +00:00
|
|
|
var output = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : stdout;
|
|
|
|
var input = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : stdin;
|
2015-12-11 17:49:42 +00:00
|
|
|
|
|
|
|
_classCallCheck(this, Interface);
|
|
|
|
|
|
|
|
this.output = output;
|
|
|
|
this.input = input;
|
|
|
|
|
|
|
|
this.input.setRawMode(true);
|
|
|
|
this.input.setEncoding('utf8');
|
|
|
|
|
|
|
|
this.cursor = (0, _ansi2.default)(this.output).hide();
|
|
|
|
|
|
|
|
this.input.addListener('data', function (data) {
|
2015-12-12 06:14:22 +00:00
|
|
|
var always = listeners.filter(function (listener) {
|
|
|
|
return listener.key === '';
|
|
|
|
});
|
|
|
|
|
|
|
|
always.forEach(function (listener) {
|
|
|
|
return listener.fn();
|
|
|
|
});
|
|
|
|
|
2015-12-11 17:49:42 +00:00
|
|
|
var key = Object.keys(keys).find(function (value, i) {
|
|
|
|
return keys[value] === data;
|
|
|
|
});
|
|
|
|
|
2016-03-15 13:20:15 +00:00
|
|
|
if (key === 'exit') {
|
2019-05-09 20:41:18 +00:00
|
|
|
_this.output.write('\x1B[2J\x1B[0;0H');
|
2016-03-15 13:20:15 +00:00
|
|
|
process.exit();
|
|
|
|
}
|
2015-12-11 17:49:42 +00:00
|
|
|
|
|
|
|
var match = listeners.filter(function (listener) {
|
|
|
|
return listener.key === key || listener.key === data;
|
|
|
|
});
|
|
|
|
|
|
|
|
match.forEach(function (listener) {
|
|
|
|
return listener.fn();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_createClass(Interface, [{
|
|
|
|
key: 'clear',
|
|
|
|
value: function clear() {
|
2019-05-09 20:41:18 +00:00
|
|
|
this.output.write('\x1B[2J\x1B[0;0H');
|
2015-12-11 17:49:42 +00:00
|
|
|
}
|
|
|
|
}, {
|
|
|
|
key: 'write',
|
|
|
|
value: function write() {
|
|
|
|
var _cursor;
|
|
|
|
|
|
|
|
(_cursor = this.cursor).write.apply(_cursor, arguments);
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
key: 'onKey',
|
|
|
|
value: function onKey(key, fn) {
|
2015-12-12 06:14:22 +00:00
|
|
|
if (typeof key === 'function') {
|
|
|
|
fn = key;
|
|
|
|
key = '';
|
|
|
|
}
|
2015-12-11 17:49:42 +00:00
|
|
|
listeners.push({ key: key, fn: fn });
|
|
|
|
}
|
2015-12-18 12:55:26 +00:00
|
|
|
}, {
|
|
|
|
key: 'line',
|
|
|
|
value: function line(from, to) {
|
|
|
|
var delta = {
|
|
|
|
x: to.x - from.x,
|
|
|
|
y: to.y - from.y
|
|
|
|
};
|
|
|
|
|
|
|
|
var error = 0;
|
|
|
|
|
|
|
|
var deltaerr = Math.abs(delta.y / delta.x);
|
|
|
|
|
|
|
|
var y = from.y;
|
|
|
|
|
2019-05-09 20:41:18 +00:00
|
|
|
|
2015-12-18 12:55:26 +00:00
|
|
|
for (var x = from.x; x < to.x; x++) {
|
|
|
|
this.cursor.goto(x, y);
|
|
|
|
this.write('.');
|
|
|
|
error += deltaerr;
|
|
|
|
|
|
|
|
while (error >= 0.5) {
|
|
|
|
this.cursor.goto(x, y);
|
|
|
|
this.write('.');
|
|
|
|
y += Math.sign(delta.y);
|
|
|
|
|
|
|
|
error -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-12 06:14:22 +00:00
|
|
|
}, {
|
|
|
|
key: 'columns',
|
|
|
|
get: function get() {
|
|
|
|
return this.output.columns;
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
key: 'rows',
|
|
|
|
get: function get() {
|
|
|
|
return this.output.rows;
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
key: 'center',
|
|
|
|
get: function get() {
|
|
|
|
return {
|
|
|
|
x: this.output.columns / 2,
|
|
|
|
y: this.output.rows / 2
|
|
|
|
};
|
|
|
|
}
|
2015-12-11 17:49:42 +00:00
|
|
|
}]);
|
|
|
|
|
|
|
|
return Interface;
|
2019-05-09 20:41:18 +00:00
|
|
|
}();
|
2015-12-11 17:49:42 +00:00
|
|
|
|
|
|
|
exports.default = Interface;
|