-
Notifications
You must be signed in to change notification settings - Fork 104
/
text_view.js
195 lines (162 loc) · 6.04 KB
/
text_view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* jslint node: true */
'use strict';
// 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;
const stripAllLineFeeds = require('./string_util.js').stripAllLineFeeds;
// deps
const util = require('util');
const _ = require('lodash');
exports.TextView = TextView;
function TextView(options) {
if (options.dimens) {
options.dimens.height = 1; // force height of 1 for TextView's & sub classes
}
View.call(this, options);
if (options.maxLength) {
this.maxLength = options.maxLength;
} else {
this.maxLength = this.client.term.termWidth - this.position.col;
}
this.fillChar = renderSubstr(miscUtil.valueWithDefault(options.fillChar, ' '), 0, 1);
this.justify = options.justify || 'left';
this.resizable = miscUtil.valueWithDefault(options.resizable, true);
this.horizScroll = miscUtil.valueWithDefault(options.horizScroll, true);
if (_.isString(options.textOverflow)) {
this.textOverflow = options.textOverflow;
}
if (_.isString(options.textMaskChar) && 1 === options.textMaskChar.length) {
this.textMaskChar = options.textMaskChar;
}
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 {
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);
}
}
}
const renderedFillChar = pipeToAnsi(this.fillChar);
this.client.term.write(
padStr(
textToDraw,
this.dimens.width,
renderedFillChar, //this.fillChar,
this.justify,
this.hasFocus ? this.getFocusSGR() : this.getSGR(),
this.getStyleSGR(1) || this.getSGR(),
true // use render len
),
false // no converting CRLF needed
);
};
this.getEndOfTextColumn = function () {
var offset = Math.min(this.text.length, this.dimens.width);
return this.position.col + offset;
};
this.setText(options.text || '', false); // false=do not redraw now
}
util.inherits(TextView, View);
TextView.prototype.redraw = function () {
//
// 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 (_.isUndefined(this.text)) {
return;
}
}
this.hasDrawnOnce = true;
TextView.super_.prototype.redraw.call(this);
if (_.isString(this.text)) {
this.drawText(this.text);
}
};
TextView.prototype.setFocus = function (focused) {
TextView.super_.prototype.setFocus.call(this, focused);
this.redraw();
this.client.term.write(ansi.goto(this.position.row, this.getEndOfTextColumn()));
this.client.term.write(this.getFocusSGR());
};
TextView.prototype.getData = function () {
return this.text;
};
TextView.prototype.setText = function (text, redraw) {
redraw = _.isBoolean(redraw) ? redraw : true;
if (!_.isString(text)) {
// allow |text| to be numbers/etc.
text = text.toString();
}
this.text = pipeToAnsi(stripAllLineFeeds(text), this.client); // expand MCI/etc.
if (this.maxLength > 0) {
this.text = renderSubstr(this.text, 0, this.maxLength);
}
// :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
);
if (redraw) {
this.redraw();
}
};
TextView.prototype.clearText = function () {
this.setText('');
};
TextView.prototype.setPropertyValue = function (propName, value) {
switch (propName) {
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;
}
TextView.super_.prototype.setPropertyValue.call(this, propName, value);
};