node-console-progress-bar-tqdm
Version:
Progress bar in console for Node.js in the style of TQDM Python library
116 lines • 3.42 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TqdmWriteStream = exports.TqdmAsyncResultIterator = exports.TqdmSyncResultIterator = exports.TqdmNumericIterator = void 0;
const node_os_1 = require("node:os");
const node_tty_1 = __importDefault(require("node:tty"));
const term_1 = require("./term");
const utils_1 = require("./utils");
const defaultTerminalColumns = 80;
class TqdmNumericIterator {
num;
cnt = 0;
constructor(num) {
this.num = num;
}
next() {
const val = this.cnt++;
if (val >= this.num) {
return { value: undefined, done: true };
}
return { value: val, done: false };
}
}
exports.TqdmNumericIterator = TqdmNumericIterator;
class TqdmSyncResultIterator {
container;
constructor(container) {
this.container = container;
}
next() {
return this.container.nextSync();
}
}
exports.TqdmSyncResultIterator = TqdmSyncResultIterator;
class TqdmAsyncResultIterator {
container;
constructor(container) {
this.container = container;
}
next() {
return this.container.nextAsync();
}
}
exports.TqdmAsyncResultIterator = TqdmAsyncResultIterator;
class TqdmWriteStream {
stream;
resetLine;
streamIsTty;
constructor(stream, forceTerminal = false) {
this.stream = stream;
this.resetLine = this.generalResetLine;
this.streamIsTty = stream instanceof node_tty_1.default.WriteStream && (0, utils_1.hasFd)(stream) && node_tty_1.default.isatty(stream.fd);
if (this.streamIsTty) {
this.resetLine = this.ttyResetLine;
process.once('SIGWINCH', this.onTerminalResize);
}
else if (forceTerminal) {
this.resetLine = this.forceTerminalResetLine;
}
}
get isTty() {
return this.streamIsTty;
}
get columns() {
const stream = this.getStreamAsTty();
if (stream) {
return stream.columns;
}
return defaultTerminalColumns;
}
write(chunk) {
this.stream.write(chunk);
}
finalize() {
process.off('SIGWINCH', this.onTerminalResize);
this.stream.write(node_os_1.EOL);
}
clearScreen() {
const stream = this.getStreamAsTty();
if (stream) {
stream.write(node_os_1.EOL);
stream.write((0, term_1.getTermClearScreen)());
stream.cursorTo(0, 0);
}
}
onTerminalResize = () => {
this.clearScreen();
process.once('SIGWINCH', this.onTerminalResize);
};
getStreamAsTty() {
if (this.stream instanceof node_tty_1.default.WriteStream) {
return this.stream;
}
return null;
}
ttyResetLine = () => {
const stream = this.getStreamAsTty();
if (stream) {
stream.clearLine(0);
stream.cursorTo(0);
}
else {
this.forceTerminalResetLine();
}
};
forceTerminalResetLine = () => {
this.stream.write((0, term_1.getTermReturnToLineStart)());
};
generalResetLine = () => {
this.stream.write(node_os_1.EOL);
};
}
exports.TqdmWriteStream = TqdmWriteStream;
//# sourceMappingURL=supply.js.map