UNPKG

iso-bench

Version:

Small benchmark library focused in avoiding optimization/deoptimization pollution between tests by isolating them.

73 lines (72 loc) 2.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TTYOutput = void 0; class TTYOutput { _stream; logs = []; top = 0; drawheight = 0; constructor(_stream) { this._stream = _stream; this._stream.on("resize", () => { }); } _drawRow(row, log) { const totalRows = this._stream.rows - 1; const bottom = this.top + this.drawheight; const currentLocation = this.drawheight; if (row < this.top) { this.top = row; this.redraw(); } else if (row >= bottom) { const diff = row - bottom; this.drawheight += diff + 1; } if (this.drawheight > totalRows) { const diff = this.drawheight - totalRows; this.drawheight -= diff; this.top += diff; this.redraw(); } else { const realRow = row - this.top; const diff = realRow - currentLocation; this._stream.moveCursor(0, diff); this._stream.cursorTo(0); this._stream.clearLine(0); this._stream.write(`${log}\n`); this._stream.moveCursor(0, this.drawheight - realRow - 1); } } log(log, row) { if (row == null) { row = this.logs.length; this.logs.push(""); } while (row >= this.logs.length) { this.log(""); } this.logs[row] = log; this._drawRow(row, log); return row; } redraw() { this._stream.cursorTo(0, 0); for (let i = 0; i < this.drawheight; i++) { this._stream.clearLine(0); this._stream.write(`${this.logs[this.top + i]}\n`); } } end() { if (this.logs.length > this.drawheight) { this._stream.moveCursor(0, -this.drawheight); for (const log of this.logs) { this._stream.cursorTo(0); this._stream.clearLine(0); this._stream.write(`${log}\n`); } } } } exports.TTYOutput = TTYOutput;