Improve class

This commit is contained in:
SrGooglo 2025-05-21 19:10:05 +00:00
parent cb15d4b2cb
commit e1d44d19e6

View File

@ -1,114 +1,53 @@
class Echo { class InternalConsole {
constructor(params = {}) { constructor(params = {}) {
this.bgColor = params.bgColor ?? "dimgray" this.namespace = String(params.namespace)
this.color = params.color ?? "azure" this.bgColor = params.bgColor ?? "dimgray"
} this.color = params.textColor ?? "azure"
this.tagStyle = `background-color: ${this.bgColor}; color: ${this.color}; font-weight: bold; padding: 3px 7px; border-radius: 8px;`
this.timers = new Map()
queue = [] const methods = ["log", "info", "warn", "error", "debug", "trace"]
ECHO_TOKEN = {}
RESET_INPUT = "%c "
RESET_CSS = ""
tagFormatting(value) { methods.forEach((method) => {
this.queue.push({ const originalMethod = console[method].bind(console)
value: value,
css: `
display: inline-block;
background-color: ${this.bgColor};
color: ${this.color};
font-weight: bold;
padding: 3px 7px;
border-radius: 8px;
`
})
return this.ECHO_TOKEN this[method] = (...args) => {
} const formatParts = [`%c[${this.namespace}]%c`]
const styles = [this.tagStyle, ""]
using = (consoleFunction) => { args.forEach((arg) => {
function consoleFunctionProxy() { if (typeof arg === "object" || typeof arg === "function") {
var inputs = [] formatParts.push("%o")
var modifiers = [] } else {
formatParts.push("%s")
}
styles.push(arg)
})
for (var i = 0; i < arguments.length; i++) { return originalMethod(formatParts.join(" "), ...styles)
if (arguments[i] === this.ECHO_TOKEN) { }
var item = this.queue.shift()
inputs.push(("%c" + item.value), this.RESET_INPUT) Object.setPrototypeOf(
modifiers.push(item.css, this.RESET_CSS) this[method],
} else { Object.getPrototypeOf(console[method]),
var arg = arguments[i] )
})
}
if (typeof (arg) === "object" || typeof (arg) === "function") { time(label = "default") {
inputs.push("%o", this.RESET_INPUT) this.timers.set(label, performance.now())
modifiers.push(arg, this.RESET_CSS) }
} else {
inputs.push(("%c" + arg), this.RESET_INPUT)
modifiers.push(this.RESET_CSS, this.RESET_CSS)
}
}
}
consoleFunction(inputs.join(""), ...modifiers) timeEnd(label = "default") {
const startTime = this.timers.get(label)
this.queue = [] if (startTime) {
} const duration = performance.now() - startTime
this.timers.delete(label)
return consoleFunctionProxy.bind(this) this.debug(`${label}: ${duration}ms`)
} }
}
out = (method, ...args) => {
return this.using(console[method])(...args)
}
} }
export default class InternalConsole { export default InternalConsole
constructor(params = {}) {
this.namespace = String(params.namespace)
this.params = params
this.echo = new Echo({
bgColor: this.params.bgColor,
color: this.params.textColor,
})
}
_output(method, ...args) {
this.echo.out(
method,
this.echo.tagFormatting(`[${this.namespace}]`),
...args
)
}
log = (...args) => {
this._output("log", ...args)
}
warn = (...args) => {
this._output("warn", ...args)
}
error = (...args) => {
this._output("error", ...args)
}
debug = (...args) => {
this._output("debug", ...args)
}
info = (...args) => {
this._output("info", ...args)
}
trace = (...args) => {
this._output("trace", ...args)
}
time = (...args) => {
this._output("time", ...args)
}
timeEnd = (...args) => {
this._output("timeEnd", ...args)
}
}