UNPKG

node-console-progress-bar-tqdm

Version:

Progress bar in console for Node.js in the style of TQDM Python library

89 lines 2.6 kB
import { hasOwnProperty } from './utils.js'; const basicAnsiColorsTable = { 'black': '0', 'red': '1', 'green': '2', 'yellow': '3', 'blue': '4', 'magenta': '5', 'cyan': '6', 'white': '7', }; export function getTermClearScreen() { return '\x1B[2J'; } export 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'; } export 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; } export function getTermColorReset() { return '\x1B[0m'; } export function isBasicAnsiColor(x) { return hasOwnProperty(basicAnsiColorsTable, x); } export 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; } export function is24BitAnsiColor(x) { if (typeof x != 'string') { return false; } return /^#[\da-f]{3,6}$/i.test(x); } export 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), }; } //# sourceMappingURL=term.js.map