@consify/ansi
Version:
@consify/ansi — a zero-dependency toolkit for styling terminal text with colors, backgrounds, and effects. Perfect for developers who want simple, readable, and expressive CLI output.
381 lines (380 loc) • 10.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IceBurg = exports.IceBurgStyleElem = exports.StyleBr = exports.StyleElem = exports.BgElem = exports.ClElem = exports.Style = exports.CL = exports.BrCL = exports.BrBG = exports.BG = void 0;
const constants_js_1 = require("./shared/constants.js");
class BASE {
constructor(props = {}) {
this.props = props;
}
;
inspectProps() {
return Object.assign({}, this.props);
}
}
;
/**Goal: To provide more features, customization over output string to the end user */
class BasePen extends BASE {
constructor(props = {}) {
super(props);
}
;
escapeCode(code) {
return `\x1b[${code}m`;
}
;
resetCode(prop, code) {
//Generate appropriate resetCode for each prop
let resetCode;
if (prop in this.props) {
switch (prop) {
case 'ST':
resetCode = constants_js_1.RESET_ST_CODES[code];
break;
case 'FG':
resetCode = constants_js_1.RESET_FG_CODE;
break;
case 'BG':
resetCode = constants_js_1.RESET_BG_CODE;
break;
default:
break;
}
;
}
;
return resetCode;
}
get prefix() {
let pf = '';
for (let prop of constants_js_1.PARAM_KEYS) {
const code = this.props[prop];
if (typeof code === "number")
pf += this.escapeCode(code);
}
;
return pf;
}
;
get suffix() {
let sf = '';
for (let prop of constants_js_1.PARAM_KEYS) {
const code = this.props[prop];
if (typeof code === "number") {
const reset = this.resetCode(prop, code);
if (typeof reset === "number")
sf += this.escapeCode(reset);
}
;
}
;
return sf;
}
;
getCallbackParams() {
const params = {
prefix: this.prefix,
suffix: this.suffix,
date: new Date(),
};
return params;
}
;
compose(writableInp) {
return `${this.prefix}${writableInp}${this.suffix}`;
}
}
//Note: only a base/element or subClient can extend a Client
class BaseClient extends BASE {
constructor(props = {}) {
super(props);
}
;
write(textOrCallback) {
const pen = new BasePen(this.props);
if (typeof textOrCallback === 'function') {
return textOrCallback(pen.getCallbackParams());
}
;
return pen.compose(textOrCallback);
}
;
log(...args) {
const newArgs = args.map(arg => {
if (['string', 'number'].some(type => typeof arg === type)) {
return this.write(arg);
}
else {
return arg;
}
});
console.log(...newArgs);
}
}
//Color-client, features offered by text-coloring
class ClClient extends BaseClient {
constructor(props = {}) {
super(props);
}
;
}
;
//Color-element e.g. red, blue etc which utilize Color-client
class ClElem extends ClClient {
constructor(props = {}) {
super(props);
}
;
}
exports.ClElem = ClElem;
;
//An object holding all color entities e.g {red,blue,...}
class AbstractCL extends BASE {
calcFG(ci) {
return this.offset + ci;
}
constructor(props = {}) {
super(props);
for (let ci = 0; ci < constants_js_1.ANSI_COLORS.length; ci++) {
const color = constants_js_1.ANSI_COLORS[ci];
Object.defineProperty(this, color, {
value: new ClElem(Object.assign(Object.assign({}, this.props), { FG: this.calcFG(ci) }))
});
}
}
}
;
;
//Containing all regular text colors
class CL extends AbstractCL {
get offset() { return constants_js_1.CL_OFFSET; }
;
constructor(props = {}) {
super(props);
}
}
exports.CL = CL;
;
//Containing all bright text colors
class BrCL extends AbstractCL {
get offset() { return constants_js_1.BR_CL_OFFSET; }
;
constructor(props = {}) {
super(props);
}
}
exports.BrCL = BrCL;
;
//Background-client, not only write() but supports the chaining for each ClElem
class BgClient extends CL {
get br() { return this._br; }
;
constructor(props = {}) {
super(props);
this._br = new BrCL(props);
}
;
write(textOrCallback) {
const pen = new BasePen(this.props);
if (typeof textOrCallback === 'function') {
return textOrCallback(pen.getCallbackParams());
}
;
return pen.compose(textOrCallback);
}
;
log(...args) {
const newArgs = args.map(arg => {
if (['string', 'number'].some(type => typeof arg === type)) {
return this.write(arg);
}
else {
return arg;
}
});
console.log(...newArgs);
}
}
;
//background-color entity e.g red, black, yellow etc
class BgElem extends BgClient {
constructor(props = {}) {
super(props);
}
;
}
exports.BgElem = BgElem;
;
//An object holding all bg-color entities e.g {red,blue,...}
class AbstractBG extends BASE {
calcBG(ci) {
return this.offset + ci;
}
constructor(props = {}) {
super(props);
for (let ci = 0; ci < constants_js_1.ANSI_COLORS.length; ci++) {
const color = constants_js_1.ANSI_COLORS[ci];
Object.defineProperty(this, color, {
value: new BgElem(Object.assign(Object.assign({}, this.props), { BG: this.calcBG(ci) }))
});
}
}
}
;
;
//Containing all regular bg colors
class BG extends AbstractBG {
get offset() { return constants_js_1.BG_OFFSET; }
;
constructor(props = {}) {
super(props);
}
;
}
exports.BG = BG;
;
//Containing all bright bg colors
class BrBG extends AbstractBG {
get offset() { return constants_js_1.BR_BG_OFFSET; }
;
constructor(props = {}) {
super(props);
}
;
}
exports.BrBG = BrBG;
;
/**
/*Provides most basic utilities to client such as to continue text-coloring(non-bright), plus write method
* * <underline>.write(text)
* <underline>.blue.write(text)
*/
class StyleBaseClient extends CL {
constructor(props = {}) {
super(props);
}
;
write(textOrCallback) {
const pen = new BasePen(this.props);
if (typeof textOrCallback === 'function') {
return textOrCallback(pen.getCallbackParams());
}
;
return pen.compose(textOrCallback);
}
;
log(...args) {
const newArgs = args.map(arg => {
if (['string', 'number'].some(type => typeof arg === type)) {
return this.write(arg);
}
else {
return arg;
}
});
console.log(...newArgs);
}
}
;
/**
* Allows to continue define br color or br bg after defining the style.
* Can define bright text color or either bright background color
* @example
* * underline.<br>.blue.write(text)
* underline.<br>.bg.red.write(text)
* underline.<br>.bg.red.blue.write(text)
* underline.<br>.bg.red.br.blue.write(text)
*/
class StyleBr extends BrCL {
get bg() { return this._brBg; }
constructor(props = {}) {
super(props);
this._brBg = new BrBG(props);
}
}
exports.StyleBr = StyleBr;
;
/**
* StyleClient Allows almost any deep chain to define after <style>
* * <underline>.write(text)
* <underline>.blue.write(text)
* <underline>.bg.red.write(text)
* <underline>.bg.red.br.yellow(text)
* <underline>.br.blue.write(text)
* <underline>.br.bg.red.write(text) Or <underline>.brBg.red.write(text)
* <underline>.br.bg.red.blue(text) Or <underline>.brBg.red.blue.write(text)
* <underline>.br.bg.red.br.blue.write(text) Or <underline>.brBg.red.br.blue.write(text)
*/
class StyleClient extends StyleBaseClient {
get br() { return this.styleBr; }
;
get bg() { return this.styleBg; }
;
get brBg() { return this.styleBrBg; }
constructor(props = {}) {
super(props);
this.styleBr = new StyleBr(props);
this.styleBg = new BG(props);
this.styleBrBg = new BrBG(props);
}
}
;
// style-elements are like e.g. <underline>, <reverse>, <hidden> and more...
class StyleElem extends StyleClient {
constructor(props = {}) {
super(props);
}
;
}
exports.StyleElem = StyleElem;
;
class Style extends BASE {
calcST(si) { return si; }
constructor(props = {}) {
super(props);
for (let si = 0; si < constants_js_1.ANSI_STYLES.length; si++) {
const style = constants_js_1.ANSI_STYLES[si];
Object.defineProperty(this, style, {
value: new StyleElem(Object.assign(Object.assign({}, this.props), { ST: this.calcST(si) }))
});
}
}
;
}
exports.Style = Style;
;
;
/**
* IceBurg: contains every single feature offered in this whole package,
* Rather than injecting features directly from others, will inject by using additional IceBurg layers
*/
class IceBurgBASE extends StyleClient {
//To inject features like: ANSI.red, ANSI.bg..., ANSI.br..., ANSI.br.bg... etc
constructor(props = {}) {
super(props);
}
;
}
;
class IceBurgStyleElem extends StyleClient {
//To inject style features like: ANSI.underline..., ANSI.bold.br... etc
constructor(props = {}) {
super(props);
}
}
exports.IceBurgStyleElem = IceBurgStyleElem;
;
class IceBurg extends IceBurgBASE {
calcST(si) { return si; }
constructor(props = {}) {
super(props);
for (let si = 0; si < constants_js_1.ANSI_STYLES.length; si++) {
const style = constants_js_1.ANSI_STYLES[si];
Object.defineProperty(this, style, {
value: new IceBurgStyleElem(Object.assign(Object.assign({}, this.props), { ST: this.calcST(si) }))
});
}
}
}
exports.IceBurg = IceBurg;
;
;