mirror of
https://github.com/ragestudio/vessel.git
synced 2025-06-09 02:24:17 +00:00
Improve class
This commit is contained in:
parent
cb15d4b2cb
commit
e1d44d19e6
@ -1,114 +1,53 @@
|
||||
class Echo {
|
||||
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 {
|
||||
class InternalConsole {
|
||||
constructor(params = {}) {
|
||||
this.namespace = String(params.namespace)
|
||||
this.params = params
|
||||
this.echo = new Echo({
|
||||
bgColor: this.params.bgColor,
|
||||
color: this.params.textColor,
|
||||
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()
|
||||
|
||||
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) {
|
||||
this.echo.out(
|
||||
method,
|
||||
this.echo.tagFormatting(`[${this.namespace}]`),
|
||||
...args
|
||||
)
|
||||
time(label = "default") {
|
||||
this.timers.set(label, performance.now())
|
||||
}
|
||||
|
||||
log = (...args) => {
|
||||
this._output("log", ...args)
|
||||
}
|
||||
timeEnd(label = "default") {
|
||||
const startTime = this.timers.get(label)
|
||||
|
||||
warn = (...args) => {
|
||||
this._output("warn", ...args)
|
||||
}
|
||||
if (startTime) {
|
||||
const duration = performance.now() - startTime
|
||||
this.timers.delete(label)
|
||||
|
||||
error = (...args) => {
|
||||
this._output("error", ...args)
|
||||
this.debug(`${label}: ${duration}ms`)
|
||||
}
|
||||
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user