@k8ts/instruments
Version:
A collection of utilities and core components for k8ts.
125 lines • 4.23 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.displayers = exports.Displayers = void 0;
const chalk_1 = __importDefault(require("chalk"));
const _embedder_1 = require("../_embedder");
const chalkNoColor = new chalk_1.default.Instance({
level: 0
});
class DisplayerDecorator {
_system = new _embedder_1.Embedder("displayers");
_lastMode = "simple";
_withLastMode(mode, fn) {
return (...args) => {
const oldMode = this._lastMode;
this._lastMode = mode;
const result = fn(...args);
this._lastMode = oldMode;
return result;
};
}
implement(ctor, input) {
this._system.set(ctor.prototype, input);
const decorator = this;
Object.defineProperties(ctor.prototype, {
[Symbol.toStringTag]: {
get() {
const a = decorator.get(this);
return a.default();
}
},
toString: {
value() {
return this[Symbol.toStringTag];
}
}
});
}
wrapRecursiveFallback(out) {
const oAny = out;
const self = this;
for (const k in out) {
const orig = oAny[k];
if (typeof orig === "function") {
oAny[k] = function (...args) {
const oldMode = self._lastMode;
if (k === "simple" || k === "pretty") {
self._lastMode = k;
}
try {
let result = orig.call(oAny, ...args);
if (!Array.isArray(result)) {
result = [result];
}
const terms = result.map((x) => {
const sndDisplayers = exports.Displayers.tryGet(x);
if (sndDisplayers) {
return sndDisplayers[k].call(sndDisplayers, x, ...args);
}
return x;
});
return terms.join(" ");
}
finally {
this._lastMode = oldMode;
}
};
}
}
return out;
}
tryGet(target) {
if (typeof target !== "object") {
return undefined;
}
const input = this._system.get(target);
if (!input) {
return undefined;
}
const o = {
default: (format) => {
const lastMode = this._lastMode;
const result = o[lastMode]?.call(o, format) ?? o.simple(format);
return result;
},
simple: format => {
const result = input.simple?.call(o, target, format);
if (!result) {
const pretty = input.pretty?.call(o, target, format);
if (typeof pretty === "string") {
return chalkNoColor(pretty);
}
return "?!!?";
}
return result;
},
prefix: input.prefix && (() => input.prefix.call(o, target)),
pretty: format => {
return (input.pretty?.call(o, target, format) ??
input.simple?.call(o, target, format) ??
"?!?");
}
};
return this.wrapRecursiveFallback(o);
}
get(target) {
this._system.get(target); // error if not there
return this.tryGet(target);
}
get decorator() {
return (input) => {
return (ctor) => {
this.implement(ctor, input);
};
};
}
}
class PrivateCtorExemplar {
constructor(...args) { }
}
exports.Displayers = new DisplayerDecorator();
exports.displayers = exports.Displayers.decorator;
//# sourceMappingURL=displayers.js.map