UNPKG

@plugjs/plug

Version:
97 lines (96 loc) 2.77 kB
// logging/colors.ts import { sep } from "node:path"; import { getCurrentWorkingDirectory, resolveRelativeChildPath } from "../paths.mjs"; import { logOptions } from "./options.mjs"; var _colors = logOptions.colors; logOptions.on("changed", ({ colors }) => _colors = colors); var rst = "\x1B[0m"; var und = "\x1B[4m"; var gry = "\x1B[38;5;240m"; var red = "\x1B[38;5;203m"; var grn = "\x1B[38;5;76m"; var ylw = "\x1B[38;5;220m"; var blu = "\x1B[38;5;69m"; var mgt = "\x1B[38;5;213m"; var cyn = "\x1B[38;5;81m"; var wht = "\x1B[1;38;5;255m"; var tsk = "\x1B[38;5;141m"; function colorize(color, string) { if (!_colors) return `${string}`; const lines = `${string}`.split("\n"); return lines.map((line) => `${color}${line}${rst}`).join("\n"); } function $p(path) { const directory = getCurrentWorkingDirectory(); const relative = resolveRelativeChildPath(directory, path); const resolved = relative == null ? path : `.${sep}${relative}`; return _colors ? `${und}${gry}${resolved}${rst}` : `"${resolved}"`; } function $t(task, quoted = true) { return _colors ? `${tsk}${task}${rst}` : quoted ? `"${task}"` : task; } function $ms(millis, note) { let string; if (millis >= 6e4) { const minutes = Math.floor(millis / 6e4); const seconds = Math.floor(millis % 6e4 / 1e3); string = `${minutes}m ${seconds}s`; } else if (millis >= 1e4) { const seconds = Math.floor(millis / 1e3); const decimal = Math.floor(millis % 1e3 / 100); string = `${seconds}.${decimal}s`; } else if (millis >= 1e3) { const seconds = Math.floor(millis / 1e3); const decimal = Math.floor(millis % 1e3 / 10); string = `${seconds}.${decimal}s`; } else { string = `${millis}ms`; } return note ? _colors ? `${gry}[${note}${gry}|${string}]${rst}` : `[${note}|${string}]` : _colors ? `${gry}[${string}]${rst}` : `[${string}]`; } function $gry(string) { return _colors ? `${gry}${string}${rst}` : string; } function $red(string) { return colorize(red, string); } function $grn(string) { return colorize(grn, string); } function $ylw(string) { return colorize(ylw, string); } function $blu(string) { return colorize(blu, string); } function $mgt(string) { return colorize(mgt, string); } function $cyn(string) { return colorize(cyn, string); } function $wht(string) { return colorize(wht, string); } function $und(string) { return colorize(und, string); } function $plur(number, singular, plural, colorize2 = _colors) { return colorize2 ? number === 1 ? `${$ylw(number)} ${singular}` : `${$ylw(number)} ${plural}` : number === 1 ? `${number} ${singular}` : `${number} ${plural}`; } export { $blu, $cyn, $grn, $gry, $mgt, $ms, $p, $plur, $red, $t, $und, $wht, $ylw }; //# sourceMappingURL=colors.mjs.map