2014-10-17 04:03:32 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const events = require('events');
|
|
|
|
const util = require('util');
|
|
|
|
const ansi = require('./ansi_term.js');
|
|
|
|
const colorCodes = require('./color_codes.js');
|
|
|
|
const enigAssert = require('./enigma_assert.js');
|
|
|
|
const { renderSubstr } = require('./string_util.js');
|
2015-06-06 06:33:59 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const _ = require('lodash');
|
2014-10-17 04:03:32 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.View = View;
|
2014-10-20 03:06:39 +00:00
|
|
|
|
2016-08-27 03:30:25 +00:00
|
|
|
const VIEW_SPECIAL_KEY_MAP_DEFAULT = {
|
2022-06-05 20:04:25 +00:00
|
|
|
accept: ['return'],
|
|
|
|
exit: ['esc'],
|
|
|
|
backspace: ['backspace', 'del', 'ctrl + d'], // https://www.tecmint.com/linux-command-line-bash-shortcut-keys/
|
|
|
|
del: ['del'],
|
|
|
|
next: ['tab'],
|
|
|
|
up: ['up arrow'],
|
|
|
|
down: ['down arrow'],
|
|
|
|
end: ['end'],
|
|
|
|
home: ['home'],
|
|
|
|
left: ['left arrow'],
|
|
|
|
right: ['right arrow'],
|
|
|
|
clearLine: ['ctrl + y'],
|
2014-10-20 03:06:39 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.VIEW_SPECIAL_KEY_MAP_DEFAULT = VIEW_SPECIAL_KEY_MAP_DEFAULT;
|
2016-08-27 03:30:25 +00:00
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
function View(options) {
|
2018-06-22 05:15:04 +00:00
|
|
|
events.EventEmitter.call(this);
|
|
|
|
|
|
|
|
enigAssert(_.isObject(options));
|
|
|
|
enigAssert(_.isObject(options.client));
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.client = options.client;
|
|
|
|
this.cursor = options.cursor || 'show';
|
|
|
|
this.cursorStyle = options.cursorStyle || 'default';
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.acceptsFocus = options.acceptsFocus || false;
|
|
|
|
this.acceptsInput = options.acceptsInput || false;
|
|
|
|
this.autoAdjustHeight = _.get(options, 'dimens.height')
|
|
|
|
? false
|
|
|
|
: _.get(options, 'autoAdjustHeight', true);
|
|
|
|
this.position = { x: 0, y: 0 };
|
|
|
|
this.textStyle = options.textStyle || 'normal';
|
|
|
|
this.focusTextStyle = options.focusTextStyle || this.textStyle;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.id) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.setId(options.id);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.position) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.setPosition(options.position);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.dimens) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.setDimension(options.dimens);
|
|
|
|
} else {
|
|
|
|
this.dimens = {
|
2022-06-05 20:04:25 +00:00
|
|
|
width: options.width || 0,
|
|
|
|
height: 0,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Just use styleSGRx for these, e.g. styleSGR0, styleSGR1 = norm/focus
|
2022-06-05 20:04:25 +00:00
|
|
|
this.ansiSGR =
|
|
|
|
options.ansiSGR || ansi.getSGRFromGraphicRendition({ fg: 39, bg: 49 }, true);
|
|
|
|
this.ansiFocusSGR = options.ansiFocusSGR || this.ansiSGR;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.styleSGR1 = options.styleSGR1 || this.ansiSGR;
|
|
|
|
this.styleSGR2 = options.styleSGR2 || this.ansiFocusSGR;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.acceptsInput) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.specialKeyMap = options.specialKeyMap || VIEW_SPECIAL_KEY_MAP_DEFAULT;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isObject(options.specialKeyMapOverride)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.setSpecialKeyMapOverride(options.specialKeyMapOverride);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.isKeyMapped = function (keySet, keyName) {
|
|
|
|
return (
|
|
|
|
_.has(this.specialKeyMap, keySet) &&
|
|
|
|
this.specialKeyMap[keySet].indexOf(keyName) > -1
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getANSIColor = function (color) {
|
|
|
|
var sgr = [color.flags, color.fg];
|
|
|
|
if (color.bg !== color.flags) {
|
2018-06-22 05:15:04 +00:00
|
|
|
sgr.push(color.bg);
|
|
|
|
}
|
|
|
|
return ansi.sgr(sgr);
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.hideCusor = function () {
|
2018-06-24 02:16:44 +00:00
|
|
|
this.client.term.rawWrite(ansi.hideCursor());
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.restoreCursor = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
//this.client.term.write(ansi.setCursorStyle(this.cursorStyle));
|
2022-06-05 20:04:25 +00:00
|
|
|
this.client.term.rawWrite(
|
|
|
|
'show' === this.cursor ? ansi.showCursor() : ansi.hideCursor()
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
2018-06-24 04:23:22 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.initDefaultWidth = function (width = 15) {
|
|
|
|
this.dimens.width =
|
|
|
|
this.dimens.width ||
|
|
|
|
Math.min(width, this.client.term.termWidth - this.position.col);
|
2018-06-24 04:23:22 +00:00
|
|
|
};
|
2014-10-20 03:06:39 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 05:12:44 +00:00
|
|
|
util.inherits(View, events.EventEmitter);
|
2014-10-20 05:30:44 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
View.prototype.setId = function (id) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.id = id;
|
2014-10-23 05:41:00 +00:00
|
|
|
};
|
2014-10-20 05:30:44 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
View.prototype.getId = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.id;
|
2015-04-14 06:19:14 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
View.prototype.setPosition = function (pos) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Allow the following forms: [row, col], { row : r, col : c }, or (row, col)
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-12 20:12:03 +00:00
|
|
|
if (Array.isArray(pos)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.position.row = pos[0];
|
|
|
|
this.position.col = pos[1];
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (_.isNumber(pos.row) && _.isNumber(pos.col)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.position.row = pos.row;
|
|
|
|
this.position.col = pos.col;
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (2 === arguments.length) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.position.row = parseInt(arguments[0], 10);
|
|
|
|
this.position.col = parseInt(arguments[1], 10);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// sanatize
|
2022-06-05 20:04:25 +00:00
|
|
|
this.position.row = Math.max(this.position.row, 1);
|
|
|
|
this.position.col = Math.max(this.position.col, 1);
|
|
|
|
this.position.row = Math.min(this.position.row, this.client.term.termHeight);
|
|
|
|
this.position.col = Math.min(this.position.col, this.client.term.termWidth);
|
2014-10-20 03:06:39 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
View.prototype.setDimension = function (dimens) {
|
|
|
|
enigAssert(
|
|
|
|
_.isObject(dimens) && _.isNumber(dimens.height) && _.isNumber(dimens.width)
|
|
|
|
);
|
2018-06-24 02:16:44 +00:00
|
|
|
this.dimens = dimens;
|
|
|
|
this.autoAdjustHeight = false;
|
2015-05-06 04:19:21 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
View.prototype.setHeight = function (height) {
|
|
|
|
height = parseInt(height) || 1;
|
|
|
|
height = Math.min(height, this.client.term.termHeight);
|
2015-06-29 04:45:57 +00:00
|
|
|
|
2018-06-24 02:16:44 +00:00
|
|
|
this.dimens.height = height;
|
|
|
|
this.autoAdjustHeight = false;
|
2015-05-06 04:19:21 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
View.prototype.setWidth = function (width) {
|
|
|
|
width = parseInt(width) || 1;
|
|
|
|
width = Math.min(width, this.client.term.termWidth - this.position.col);
|
2015-06-29 04:45:57 +00:00
|
|
|
|
2018-06-24 02:16:44 +00:00
|
|
|
this.dimens.width = width;
|
2015-05-06 04:19:21 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
View.prototype.getSGR = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.ansiSGR;
|
2015-05-06 04:19:21 +00:00
|
|
|
};
|
2015-04-29 21:38:20 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
View.prototype.getStyleSGR = function (n) {
|
2018-06-22 05:15:04 +00:00
|
|
|
n = parseInt(n) || 0;
|
|
|
|
return this['styleSGR' + n];
|
2015-05-18 17:31:35 +00:00
|
|
|
};
|
2015-05-15 05:01:00 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
View.prototype.getFocusSGR = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.ansiFocusSGR;
|
2015-05-06 04:19:21 +00:00
|
|
|
};
|
2014-10-20 03:06:39 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
View.prototype.setSpecialKeyMapOverride = function (specialKeyMapOverride) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.specialKeyMap = Object.assign(this.specialKeyMap, specialKeyMapOverride);
|
2018-03-04 20:35:36 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
View.prototype.setPropertyValue = function (propName, value) {
|
|
|
|
switch (propName) {
|
|
|
|
case 'acceptsFocus':
|
2019-05-25 04:27:50 +00:00
|
|
|
if (_.isBoolean(value)) {
|
|
|
|
this.acceptsFocus = value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'height':
|
|
|
|
this.setHeight(value);
|
|
|
|
break;
|
|
|
|
case 'width':
|
|
|
|
this.setWidth(value);
|
|
|
|
break;
|
|
|
|
case 'focus':
|
|
|
|
this.setFocusProperty(value);
|
|
|
|
break;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'text':
|
|
|
|
if ('setText' in this) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.setText(value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'textStyle':
|
|
|
|
this.textStyle = value;
|
|
|
|
break;
|
|
|
|
case 'focusTextStyle':
|
|
|
|
this.focusTextStyle = value;
|
|
|
|
break;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'justify':
|
|
|
|
this.justify = value;
|
|
|
|
break;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'fillChar':
|
|
|
|
if ('fillChar' in this) {
|
|
|
|
if (_.isNumber(value)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.fillChar = String.fromCharCode(value);
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (_.isString(value)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.fillChar = renderSubstr(value, 0, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'submit':
|
|
|
|
if (_.isBoolean(value)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.submit = value;
|
2022-06-05 20:04:25 +00:00
|
|
|
} /* else {
|
2018-06-23 03:26:46 +00:00
|
|
|
this.submit = _.isArray(value) && value.length > 0;
|
|
|
|
}
|
|
|
|
*/
|
2018-06-22 05:15:04 +00:00
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'resizable':
|
|
|
|
if (_.isBoolean(value)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.resizable = value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'argName':
|
|
|
|
this.submitArgName = value;
|
|
|
|
break;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'omit':
|
|
|
|
if (_.isBoolean(value)) {
|
|
|
|
this.omitFromSubmission = value;
|
|
|
|
break;
|
2019-06-08 17:57:36 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'validate':
|
|
|
|
if (_.isFunction(value)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.validate = value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (/styleSGR[0-9]{1,2}/.test(propName)) {
|
|
|
|
if (_.isObject(value)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this[propName] = ansi.getSGRFromGraphicRendition(value, true);
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (_.isString(value)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this[propName] = colorCodes.pipeToAnsi(value);
|
|
|
|
}
|
|
|
|
}
|
2015-06-29 04:45:57 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
View.prototype.redraw = function () {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.client.term.write(ansi.goto(this.position.row, this.position.col));
|
2014-10-20 03:06:39 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
View.prototype.setFocusProperty = function (focused) {
|
2022-01-21 19:53:32 +00:00
|
|
|
// Either this should accept focus, or the focus should be false
|
|
|
|
enigAssert(this.acceptsFocus || !focused, 'View does not accept focus');
|
2018-06-22 05:15:04 +00:00
|
|
|
this.hasFocus = focused;
|
2022-01-21 19:53:32 +00:00
|
|
|
};
|
|
|
|
|
2022-06-12 20:12:03 +00:00
|
|
|
View.prototype.setFocus = function (focused) {
|
2022-05-01 18:41:20 +00:00
|
|
|
// Call separate method to differentiate between a value set as a
|
2022-01-21 19:53:32 +00:00
|
|
|
// property vs focus programmatically called.
|
|
|
|
this.setFocusProperty(focused);
|
2018-06-22 05:15:04 +00:00
|
|
|
this.restoreCursor();
|
2014-10-24 04:18:38 +00:00
|
|
|
};
|
2015-06-05 22:20:26 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
View.prototype.onKeyPress = function (ch, key) {
|
|
|
|
enigAssert(this.hasFocus, 'View does not have focus');
|
|
|
|
enigAssert(this.acceptsInput, 'View does not accept input');
|
2016-08-27 03:30:25 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!this.hasFocus || !this.acceptsInput) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-06-05 22:20:26 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (key) {
|
2018-06-22 05:15:04 +00:00
|
|
|
enigAssert(this.specialKeyMap, 'No special key map defined');
|
2015-06-05 22:20:26 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.isKeyMapped('accept', key.name)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.emit('action', 'accept', key);
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (this.isKeyMapped('next', key.name)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.emit('action', 'next', key);
|
|
|
|
}
|
|
|
|
}
|
2015-06-05 22:20:26 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (ch) {
|
2018-06-22 05:15:04 +00:00
|
|
|
enigAssert(1 === ch.length);
|
|
|
|
}
|
2016-08-11 04:48:13 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.emit('key press', ch, key);
|
2015-06-05 22:20:26 +00:00
|
|
|
};
|
2014-11-02 19:07:17 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
View.prototype.getData = function () {};
|