@best/console-stream
Version:
Best stdout stream wrapper
52 lines • 1.4 kB
JavaScript
;
/*
* Copyright (c) 2019, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("@best/utils");
const os_1 = require("os");
function countEOL(buffer) {
let eol_count = 0;
for (let i = 0; i < buffer.length; i++) {
if (buffer[i] === os_1.EOL) {
eol_count += 1;
}
}
return eol_count;
}
class OutputStream {
stdout;
isInteractive;
_linesBuffer = 0;
constructor(stream, isInteractive) {
this.stdout = stream;
this.isInteractive = isInteractive || utils_1.isInteractive;
}
write(str) {
this._linesBuffer += countEOL(str);
this.stdout.write(str);
}
writeln(str) {
this.write(str + '\n');
}
clearLine() {
if (this.isInteractive) {
(0, utils_1.clearLine)(this.stdout);
}
}
clearAll() {
if (this.isInteractive) {
(0, utils_1.clearLine)(this.stdout);
this.stdout.write('\r\x1B[K\r\x1B[1A'.repeat(this._linesBuffer));
this.reset();
}
}
reset() {
this._linesBuffer = 0;
}
}
exports.default = OutputStream;
//# sourceMappingURL=output-stream.js.map