2014-10-22 05:12:44 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-08-04 01:48:45 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const View = require('./view.js').View;
|
|
|
|
const miscUtil = require('./misc_util.js');
|
|
|
|
const ansi = require('./ansi_term.js');
|
|
|
|
const padStr = require('./string_util.js').pad;
|
|
|
|
const stylizeString = require('./string_util.js').stylizeString;
|
|
|
|
const renderSubstr = require('./string_util.js').renderSubstr;
|
|
|
|
const renderStringLength = require('./string_util.js').renderStringLength;
|
|
|
|
const pipeToAnsi = require('./color_codes.js').pipeToAnsi;
|
|
|
|
|
|
|
|
// deps
|
|
|
|
const util = require('util');
|
|
|
|
const _ = require('lodash');
|
2014-10-22 05:12:44 +00:00
|
|
|
|
|
|
|
exports.TextView = TextView;
|
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
function TextView(options) {
|
2015-05-11 22:39:28 +00:00
|
|
|
if(options.dimens) {
|
|
|
|
options.dimens.height = 1; // force height of 1 for TextView's & sub classes
|
|
|
|
}
|
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
View.call(this, options);
|
2014-10-22 05:12:44 +00:00
|
|
|
|
2015-04-09 04:54:13 +00:00
|
|
|
if(options.maxLength) {
|
|
|
|
this.maxLength = options.maxLength;
|
2015-04-27 02:46:16 +00:00
|
|
|
} else {
|
2015-06-29 04:31:12 +00:00
|
|
|
this.maxLength = this.client.term.termWidth - this.position.col;
|
2014-10-22 05:12:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 02:46:16 +00:00
|
|
|
this.fillChar = miscUtil.valueWithDefault(options.fillChar, ' ').substr(0, 1);
|
|
|
|
this.justify = options.justify || 'right';
|
|
|
|
this.resizable = miscUtil.valueWithDefault(options.resizable, true);
|
|
|
|
this.horizScroll = miscUtil.valueWithDefault(options.horizScroll, true);
|
2015-05-08 04:13:12 +00:00
|
|
|
|
|
|
|
if(_.isString(options.textOverflow)) {
|
|
|
|
this.textOverflow = options.textOverflow;
|
|
|
|
}
|
2014-11-02 19:07:17 +00:00
|
|
|
|
2015-04-27 02:46:16 +00:00
|
|
|
if(_.isString(options.textMaskChar) && 1 === options.textMaskChar.length) {
|
|
|
|
this.textMaskChar = options.textMaskChar;
|
2014-10-22 05:12:44 +00:00
|
|
|
}
|
2015-04-27 02:46:16 +00:00
|
|
|
|
2016-08-07 02:11:04 +00:00
|
|
|
/*
|
2015-04-06 06:18:08 +00:00
|
|
|
this.drawText = function(s) {
|
|
|
|
|
2015-04-27 02:46:16 +00:00
|
|
|
//
|
2015-04-27 03:57:23 +00:00
|
|
|
// |<- this.maxLength
|
2015-04-27 02:46:16 +00:00
|
|
|
// ABCDEFGHIJK
|
2015-04-27 03:57:23 +00:00
|
|
|
// |ABCDEFG| ^_ this.text.length
|
|
|
|
// ^-- this.dimens.width
|
2015-04-27 02:46:16 +00:00
|
|
|
//
|
2016-08-04 01:48:45 +00:00
|
|
|
let textToDraw = _.isString(this.textMaskChar) ?
|
2015-04-27 02:46:16 +00:00
|
|
|
new Array(s.length + 1).join(this.textMaskChar) :
|
2016-08-04 01:48:45 +00:00
|
|
|
stylizeString(s, this.hasFocus ? this.focusTextStyle : this.textStyle);
|
2016-07-16 19:06:03 +00:00
|
|
|
|
2015-04-27 02:46:16 +00:00
|
|
|
if(textToDraw.length > this.dimens.width) {
|
2015-05-08 04:13:12 +00:00
|
|
|
if(this.hasFocus) {
|
|
|
|
if(this.horizScroll) {
|
|
|
|
textToDraw = textToDraw.substr(textToDraw.length - this.dimens.width, textToDraw.length);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(textToDraw.length > this.dimens.width) {
|
|
|
|
if(this.textOverflow &&
|
|
|
|
this.dimens.width > this.textOverflow.length &&
|
|
|
|
textToDraw.length - this.textOverflow.length >= this.textOverflow.length)
|
|
|
|
{
|
|
|
|
textToDraw = textToDraw.substr(0, this.dimens.width - this.textOverflow.length) + this.textOverflow;
|
|
|
|
} else {
|
|
|
|
textToDraw = textToDraw.substr(0, this.dimens.width);
|
|
|
|
}
|
|
|
|
}
|
2015-04-27 02:46:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 01:48:45 +00:00
|
|
|
this.client.term.write(padStr(
|
|
|
|
textToDraw,
|
|
|
|
this.dimens.width + 1,
|
|
|
|
this.fillChar,
|
|
|
|
this.justify,
|
|
|
|
this.hasFocus ? this.getFocusSGR() : this.getSGR(),
|
|
|
|
this.getStyleSGR(1) || this.getSGR()
|
|
|
|
), false);
|
|
|
|
};
|
2016-08-07 02:11:04 +00:00
|
|
|
*/
|
|
|
|
|
2016-08-04 01:48:45 +00:00
|
|
|
this.drawText = function(s) {
|
|
|
|
|
|
|
|
//
|
|
|
|
// |<- this.maxLength
|
|
|
|
// ABCDEFGHIJK
|
|
|
|
// |ABCDEFG| ^_ this.text.length
|
|
|
|
// ^-- this.dimens.width
|
|
|
|
//
|
|
|
|
let renderLength = renderStringLength(s); // initial; may be adjusted below:
|
|
|
|
|
|
|
|
let textToDraw = _.isString(this.textMaskChar) ?
|
|
|
|
new Array(renderLength + 1).join(this.textMaskChar) :
|
|
|
|
stylizeString(s, this.hasFocus ? this.focusTextStyle : this.textStyle);
|
|
|
|
|
|
|
|
renderLength = renderStringLength(textToDraw);
|
|
|
|
|
|
|
|
if(renderLength > this.dimens.width) {
|
|
|
|
if(this.hasFocus) {
|
|
|
|
if(this.horizScroll) {
|
|
|
|
textToDraw = renderSubstr(textToDraw, renderLength - this.dimens.width, renderLength);
|
|
|
|
}
|
|
|
|
} else {
|
2016-08-07 02:11:04 +00:00
|
|
|
if(this.textOverflow &&
|
|
|
|
this.dimens.width > this.textOverflow.length &&
|
|
|
|
renderLength - this.textOverflow.length >= this.textOverflow.length)
|
|
|
|
{
|
|
|
|
textToDraw = renderSubstr(textToDraw, 0, this.dimens.width - this.textOverflow.length) + this.textOverflow;
|
|
|
|
} else {
|
|
|
|
textToDraw = renderSubstr(textToDraw, 0, this.dimens.width);
|
|
|
|
}
|
2016-08-04 01:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-11 05:35:17 +00:00
|
|
|
this.client.term.write(
|
|
|
|
padStr(
|
|
|
|
textToDraw,
|
|
|
|
this.dimens.width + 1,
|
|
|
|
this.fillChar,
|
|
|
|
this.justify,
|
|
|
|
this.hasFocus ? this.getFocusSGR() : this.getSGR(),
|
|
|
|
this.getStyleSGR(1) || this.getSGR()
|
|
|
|
),
|
|
|
|
false // no converting CRLF needed
|
|
|
|
);
|
2015-04-06 06:18:08 +00:00
|
|
|
};
|
|
|
|
|
2016-08-07 02:11:04 +00:00
|
|
|
|
2015-05-18 17:31:35 +00:00
|
|
|
this.getEndOfTextColumn = function() {
|
2015-05-16 20:39:14 +00:00
|
|
|
var offset = Math.min(this.text.length, this.dimens.width);
|
2015-05-18 17:31:35 +00:00
|
|
|
return this.position.col + offset;
|
2015-05-16 20:39:14 +00:00
|
|
|
};
|
|
|
|
|
2016-08-11 04:48:13 +00:00
|
|
|
this.setText(options.text || '', false); // false=do not redraw now
|
2014-10-22 05:12:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(TextView, View);
|
|
|
|
|
|
|
|
TextView.prototype.redraw = function() {
|
2016-08-07 02:11:04 +00:00
|
|
|
//
|
|
|
|
// A lot of views will get an initial redraw() with empty text (''). We can short
|
|
|
|
// circuit this by NOT doing any of the work if this is the initial drawText
|
|
|
|
// and there is no actual text (e.g. save SGR's and processing)
|
|
|
|
//
|
|
|
|
if(!this.hasDrawnOnce) {
|
|
|
|
if(!this.text) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.hasDrawnOnce = true;
|
|
|
|
|
2014-10-22 05:12:44 +00:00
|
|
|
TextView.super_.prototype.redraw.call(this);
|
|
|
|
|
2015-12-24 18:54:03 +00:00
|
|
|
if(_.isString(this.text)) {
|
|
|
|
this.drawText(this.text);
|
|
|
|
}
|
2014-10-22 05:12:44 +00:00
|
|
|
};
|
|
|
|
|
2014-10-23 05:41:00 +00:00
|
|
|
TextView.prototype.setFocus = function(focused) {
|
|
|
|
TextView.super_.prototype.setFocus.call(this, focused);
|
|
|
|
|
|
|
|
this.redraw();
|
2015-05-16 20:39:14 +00:00
|
|
|
|
2015-05-18 17:31:35 +00:00
|
|
|
this.client.term.write(ansi.goto(this.position.row, this.getEndOfTextColumn()));
|
2015-04-29 21:38:20 +00:00
|
|
|
this.client.term.write(this.getFocusSGR());
|
2014-10-23 05:41:00 +00:00
|
|
|
};
|
|
|
|
|
2015-04-17 04:29:53 +00:00
|
|
|
TextView.prototype.getData = function() {
|
2015-03-31 03:29:06 +00:00
|
|
|
return this.text;
|
|
|
|
};
|
|
|
|
|
2016-08-11 04:48:13 +00:00
|
|
|
TextView.prototype.setText = function(text, redraw) {
|
|
|
|
redraw = _.isBoolean(redraw) ? redraw : true;
|
|
|
|
|
2016-07-16 19:06:03 +00:00
|
|
|
if(!_.isString(text)) {
|
|
|
|
text = text.toString();
|
2016-08-04 01:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
text = pipeToAnsi(text, this.client); // expand MCI/etc.
|
2016-07-16 19:06:03 +00:00
|
|
|
|
2015-04-12 05:48:41 +00:00
|
|
|
var widthDelta = 0;
|
|
|
|
if(this.text && this.text !== text) {
|
2016-08-11 05:35:17 +00:00
|
|
|
widthDelta = Math.abs(renderStringLength(this.text) - renderStringLength(text));
|
2015-04-12 05:48:41 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 05:12:44 +00:00
|
|
|
this.text = text;
|
|
|
|
|
|
|
|
if(this.maxLength > 0) {
|
2016-08-04 01:48:45 +00:00
|
|
|
this.text = renderSubstr(this.text, 0, this.maxLength);
|
|
|
|
//this.text = this.text.substr(0, this.maxLength);
|
2014-10-22 05:12:44 +00:00
|
|
|
}
|
|
|
|
|
2016-08-04 01:48:45 +00:00
|
|
|
// :TODO: it would be nice to be able to stylize strings with MCI and {special} MCI syntax, e.g. "|BN {UN!toUpper}"
|
|
|
|
this.text = stylizeString(this.text, this.hasFocus ? this.focusTextStyle : this.textStyle);
|
2014-10-22 05:12:44 +00:00
|
|
|
|
2016-08-04 01:48:45 +00:00
|
|
|
if(this.autoScale.width) {
|
2016-08-11 05:35:17 +00:00
|
|
|
this.dimens.width = renderStringLength(this.text) + widthDelta;
|
2014-10-22 05:12:44 +00:00
|
|
|
}
|
2016-08-04 01:48:45 +00:00
|
|
|
|
2016-08-11 04:48:13 +00:00
|
|
|
if(redraw) {
|
|
|
|
this.redraw();
|
|
|
|
}
|
2016-08-04 01:48:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
TextView.prototype.setText = function(text) {
|
|
|
|
if(!_.isString(text)) {
|
|
|
|
text = text.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
var widthDelta = 0;
|
|
|
|
if(this.text && this.text !== text) {
|
|
|
|
widthDelta = Math.abs(this.text.length - text.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.text = text;
|
|
|
|
|
|
|
|
if(this.maxLength > 0) {
|
|
|
|
this.text = this.text.substr(0, this.maxLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.text = stylizeString(this.text, this.hasFocus ? this.focusTextStyle : this.textStyle);
|
|
|
|
|
|
|
|
//if(this.resizable) {
|
|
|
|
// this.dimens.width = this.text.length + widthDelta;
|
|
|
|
//}
|
|
|
|
|
2015-09-03 05:11:17 +00:00
|
|
|
if(this.autoScale.width) {
|
|
|
|
this.dimens.width = this.text.length + widthDelta;
|
|
|
|
}
|
2014-11-04 05:53:01 +00:00
|
|
|
|
|
|
|
this.redraw();
|
2014-10-22 05:12:44 +00:00
|
|
|
};
|
2016-08-04 01:48:45 +00:00
|
|
|
*/
|
|
|
|
|
2015-04-06 06:18:08 +00:00
|
|
|
TextView.prototype.clearText = function() {
|
|
|
|
this.setText('');
|
|
|
|
};
|
2015-06-30 05:14:17 +00:00
|
|
|
|
|
|
|
TextView.prototype.setPropertyValue = function(propName, value) {
|
|
|
|
switch(propName) {
|
2016-08-04 04:43:06 +00:00
|
|
|
case 'textMaskChar' : this.textMaskChar = value.substr(0, 1); break;
|
|
|
|
case 'textOverflow' : this.textOverflow = value; break;
|
|
|
|
case 'maxLength' : this.maxLength = parseInt(value, 10); break;
|
|
|
|
case 'password' :
|
|
|
|
if(true === value) {
|
|
|
|
this.textMaskChar = this.client.currentTheme.helpers.getPasswordChar();
|
|
|
|
}
|
|
|
|
break;
|
2015-06-30 05:14:17 +00:00
|
|
|
}
|
2016-08-04 01:48:45 +00:00
|
|
|
|
2015-06-30 05:14:17 +00:00
|
|
|
|
|
|
|
TextView.super_.prototype.setPropertyValue.call(this, propName, value);
|
2015-07-22 05:52:20 +00:00
|
|
|
};
|