@rawcmd/core
Version:
Rawcmd core package.
59 lines • 1.79 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const console_1 = require("../streamlets/console");
class Spinwriter {
constructor(config) {
this._timer = null;
this.__config = Object.assign({ chars: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'], speed: 30, streamlet: new console_1.ConsoleStreamlet(), resolver: (message) => `${[this.getChar(), message].filter((v) => !!v).join(' ')} ` }, config);
}
isStarted() {
return !!this._timer;
}
getChar() {
return this.__config.chars[0];
}
getSize() {
return [
this.__config.streamlet.width,
this.__config.streamlet.height,
];
}
start() {
if (!this._timer) {
this._timer = setTimeout(this._tick.bind(this), 0);
}
return this;
}
stop() {
if (this._timer) {
this.__config.streamlet.clearLine();
clearTimeout(this._timer);
this._timer = null;
}
return this;
}
write(message) {
if (!this.isStarted()) {
return false;
}
message.trim().split(/\r?\n/g).forEach((line) => {
this._message = line;
this._render();
});
return true;
}
_tick() {
if (!this._timer) {
return;
}
this.__config.chars.push(this.__config.chars.shift());
this._render();
this._timer = setTimeout(this._tick.bind(this), this.__config.speed);
}
_render() {
this.__config.streamlet.clearLine();
this.__config.streamlet.write(this.__config.resolver.call(this, this._message));
}
}
exports.Spinwriter = Spinwriter;
//# sourceMappingURL=spinwriter.js.map