node-console-progress-bar-tqdm
Version:
Progress bar in console for Node.js in the style of TQDM Python library
100 lines • 3.22 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.parse24BitColor = exports.is24BitAnsiColor = exports.is8BitAnsiColor = exports.isBasicAnsiColor = exports.getTermColorReset = exports.getTermColor = exports.getTermReturnToLineStart = exports.getTermClearScreen = void 0;
const utils_1 = require("./utils");
const basicAnsiColorsTable = {
'black': '0',
'red': '1',
'green': '2',
'yellow': '3',
'blue': '4',
'magenta': '5',
'cyan': '6',
'white': '7',
};
function getTermClearScreen() {
return '\x1B[2J';
}
exports.getTermClearScreen = getTermClearScreen;
function getTermReturnToLineStart() {
// ANSI/VT100 codes: https://bash-hackers.gabe565.com/scripting/terminalcodes/
// \x1b – ESC, ^[: Start an escape sequence.
// \x1b[ – ESC + [.
// 0G – Move cursor to the 0th column of the current row.
// K – Clear string from the cursor position to the end of line.
return '\x1b[0G\x1b[K';
}
exports.getTermReturnToLineStart = getTermReturnToLineStart;
function getTermColor(name, colorType = "fg" /* ColorType.Fg */) {
let color = '\x1B[';
if (isBasicAnsiColor(name)) {
color += (colorType == "fg" /* ColorType.Fg */ ? '3' : '4') + basicAnsiColorsTable[name];
}
else if (is8BitAnsiColor(name)) {
color += (colorType == "fg" /* ColorType.Fg */ ? '38' : '48') + `;5;${name.slice(1)}`;
}
else if (is24BitAnsiColor(name)) {
const clr = parse24BitColor(name);
color += (colorType == "fg" /* ColorType.Fg */ ? '38' : '48') + `;2;${clr.r};${clr.g};${clr.b}`;
}
else {
if (colorType == "fg" /* ColorType.Fg */) {
color += '39';
}
else {
color += '49';
}
}
color += 'm';
return color;
}
exports.getTermColor = getTermColor;
function getTermColorReset() {
return '\x1B[0m';
}
exports.getTermColorReset = getTermColorReset;
function isBasicAnsiColor(x) {
return (0, utils_1.hasOwnProperty)(basicAnsiColorsTable, x);
}
exports.isBasicAnsiColor = isBasicAnsiColor;
function is8BitAnsiColor(x) {
if (typeof x != 'string') {
return false;
}
const matches = /^\$(\d{2,3})$/.exec(x);
if (!matches) {
return false;
}
const colorId = parseInt(matches[1]);
return colorId >= 16 && colorId <= 255;
}
exports.is8BitAnsiColor = is8BitAnsiColor;
function is24BitAnsiColor(x) {
if (typeof x != 'string') {
return false;
}
return /^#[\da-f]{3,6}$/i.test(x);
}
exports.is24BitAnsiColor = is24BitAnsiColor;
function parse24BitColor(color) {
let hexR = 'ff';
let hexG = 'ff';
let hexB = 'ff';
let matches;
if ((matches = /^#([\da-f])([\da-f])([\da-f])$/i.exec(color))) {
[, hexR, hexG, hexB] = matches;
hexR = hexR.repeat(2);
hexG = hexG.repeat(2);
hexB = hexB.repeat(2);
}
else if ((matches = /^#([\da-f]{2})([\da-f]{2})([\da-f]{2})$/i.exec(color))) {
[, hexR, hexG, hexB] = matches;
}
return {
r: parseInt(hexR, 16),
g: parseInt(hexG, 16),
b: parseInt(hexB, 16),
};
}
exports.parse24BitColor = parse24BitColor;
//# sourceMappingURL=term.js.map
;