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