@trap_stevo/terminal-plus
Version:
Transform your CLI into a visually striking, feature-rich command layer. Blend animated gradients, stylish badges, dynamic progress bars, sleek tables, and precise formatting utilities — all turning terminal output into a powerful visual experience.
144 lines (143 loc) • 5.75 kB
JavaScript
;
function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); }
function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; }
function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
const UniversalFormatUtilityManager = require("./HUDManagers/UniversalFormatUtilityManager");
const ColorUtilityManager = require("./HUDManagers/ColorUtilityManager");
const ConsoleProgressBarManager = require("./HUDComponents/ConsoleProgressBarManager");
const TerminalSpinner = require("./HUDComponents/TerminalSpinner");
const TerminalBadge = require("./HUDComponents/TerminalBadge");
const ConsoleTable = require("./HUDComponents/ConsoleTable");
var _colorEngine = /*#__PURE__*/new WeakMap();
class TerminalPlus {
constructor(options = {}) {
_classPrivateFieldInitSpec(this, _colorEngine, null);
this.options = {
gradientSpeed: 100,
defaultGradient: ["#00ffff", "#1a73e8"],
...options
};
this.progressBarManager = null;
_classPrivateFieldSet(_colorEngine, this, ColorUtilityManager);
}
log(message, type = "info", typeOptions = {}) {
const chalk = _classPrivateFieldGet(_colorEngine, this).chalk;
const time = chalk.gray(`[${new Date().toLocaleTimeString()}]`);
const typeStyles = {
info: chalk.cyan.bold,
warn: chalk.keyword("orange").bold,
error: chalk.red.bold,
success: chalk.green.bold,
debug: chalk.magenta.bold,
...(typeOptions ?? {})
};
const prefix = typeStyles[type] ? typeStyles[type](type.toUpperCase()) : "";
console.log(`${time} ${prefix} ${message}`);
}
printGradient(text, colors = this.options.defaultGradient) {
_classPrivateFieldGet(_colorEngine, this).outputGradient(text, colors);
}
printMovingGradient(text, colors = this.options.defaultGradient, interval = this.options.gradientSpeed) {
const animation = _classPrivateFieldGet(_colorEngine, this).movingGradientRenderer(text, colors, interval);
animation.start();
return animation;
}
printColor(text, options = {}) {
return _classPrivateFieldGet(_colorEngine, this).applyColor(text, options);
}
printBadge(label, options = {}) {
return TerminalBadge.print(label, options, _classPrivateFieldGet(_colorEngine, this));
}
getBadge(label, options = {}) {
return TerminalBadge.get(label, options, _classPrivateFieldGet(_colorEngine, this));
}
drawDivider(type = "default", position = null, colors = ["#273140", "#143857"], content = null, width = 116) {
if (type === "sci-frame") {
const horizontal = "═".repeat(width);
const padding = " ".repeat(width);
const top = `╔${horizontal}╗`;
const mid = `║${padding}║`;
const bottom = `╚${horizontal}╝`;
if (position === "top") {
this.printGradient(top, colors);
} else if (position === "bottom") {
this.printGradient(bottom, colors);
} else {
const midLine = content ? `║${content.padEnd(width).slice(0, width)}║` : mid;
this.printGradient(midLine, colors);
}
} else {
const line = content || (type === "arrow" ? ">>--->".repeat(Math.ceil(width / 6)).slice(0, width) : "─".repeat(width));
this.printGradient(line, colors);
}
}
createTable(data, options = {}) {
const table = new ConsoleTable(options);
table.setData(data);
return table.render();
}
createProgressBar(tasks = [], options = {}) {
this.progressBarManager = new ConsoleProgressBarManager(tasks, options);
this.progressBarManager.displayPage();
this.progressBarManager.listenForInput();
return this.progressBarManager;
}
createSpinner(label = "Processing...", options = {}) {
const spinner = new TerminalSpinner(label, options, _classPrivateFieldGet(_colorEngine, this));
spinner.start();
return spinner;
}
attachFollower(base, content, options = {}) {
const {
spacing = 1,
inline = false,
delay = 10,
inlinePosition = "left"
} = options;
const renderContent = async () => {
const output = typeof content === "function" ? await content() : content;
if (!output) {
return;
}
if (inline) {
const pad = " ".repeat(spacing);
if (inlinePosition === "right") {
process.stdout.write(pad + output + "\n");
} else {
process.stdout.clearLine(0);
process.stdout.cursorTo(0);
process.stdout.write(output + pad + base + "\n");
}
} else {
for (let i = 0; i < spacing; i++) {
process.stdout.write("\n");
}
process.stdout.write(output + "\n");
}
};
const animation = base && typeof base === "object" && typeof base.moving === "function";
if (animation) {
const interval = setInterval(async () => {
if (base.moving()) {
clearInterval(interval);
await renderContent();
}
}, delay);
} else {
setTimeout(renderContent, delay);
}
}
setDefaultGradient(gradient) {
this.options.defaultGradient = gradient;
}
formatBytes(size, dynamicUnits = true) {
return UniversalFormatUtilityManager.getFormattedSize(size, dynamicUnits);
}
formatSeconds(seconds) {
return UniversalFormatUtilityManager.convertSeconds(seconds);
}
}
;
module.exports = TerminalPlus;