malwoden
Version:
   
106 lines • 3.73 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TextWidget = exports.wrapText = exports.truncateText = void 0;
var terminal_1 = require("../terminal");
var widget_1 = require("./widget");
function truncateText(config) {
if (config.addEllipsis) {
return config.text.substring(0, config.truncateAfter - 3) + "...";
}
else {
return config.text.substring(0, config.truncateAfter);
}
}
exports.truncateText = truncateText;
function wrapText(config) {
var lines = [];
var words = config.text.split(" ");
var current = "";
while (words.length) {
var next = words.shift();
var nextWithSpace = " " + next;
// ensure we always get at least one word
if (current === "") {
current += next;
continue;
}
else if (current.length + nextWithSpace.length > config.wrapAt) {
// if we need to line break
lines.push(current);
current = next;
}
else {
// otherwise add it on
current += nextWithSpace;
}
}
if (current.length) {
lines.push(current);
}
return lines;
}
exports.wrapText = wrapText;
/**
* Represents text to draw to the screen, potentially truncated or wrapped.
*/
var TextWidget = /** @class */ (function (_super) {
__extends(TextWidget, _super);
function TextWidget(config) {
var _this = _super.call(this, config) || this;
_this.state = __assign({ foreColor: terminal_1.Color.White, backColor: terminal_1.Color.Black }, config.initialState);
return _this;
}
TextWidget.prototype.getLines = function (text) {
if (this.state.wrapAt)
return wrapText({ text: text, wrapAt: this.state.wrapAt });
else
return [text];
};
TextWidget.prototype.getText = function () {
if (this.state.truncateAfter === undefined)
return this.state.text;
return truncateText({
text: this.state.text,
truncateAfter: this.state.truncateAfter,
addEllipsis: !!this.state.truncateAddEllipsis,
});
};
TextWidget.prototype.onDraw = function () {
if (!this.terminal)
return;
var origin = this.getAbsoluteOrigin();
var text = this.getText();
var lines = this.getLines(text);
for (var y = 0; y < lines.length; y++) {
var line = lines[y];
this.terminal.writeAt({ x: origin.x, y: origin.y + y }, line, this.state.foreColor, this.state.backColor);
}
};
return TextWidget;
}(widget_1.Widget));
exports.TextWidget = TextWidget;
//# sourceMappingURL=text-widget.js.map