console-styles
Advanced tools
Comparing version 0.2.4 to 1.0.0
@@ -29,2 +29,6 @@ declare module "console-styles" { | ||
inverse: this; | ||
upper: this; | ||
lower: this; | ||
trim: this; | ||
timestamp: this; | ||
log(...text: string): void | ||
@@ -31,0 +35,0 @@ } |
75
index.js
@@ -31,30 +31,65 @@ const { format } = require("util"); | ||
let open = []; | ||
let close = []; | ||
const modifiers = { | ||
upper: text => text.toUpperCase(), | ||
lower: text => text.toLowerCase(), | ||
trim: text => text.trim(), | ||
timestamp: text => { | ||
const date = new Date(); | ||
const hours = date.getHours(); | ||
const minutes = date.getMinutes(); | ||
const seconds = date.getSeconds(); | ||
const timestamp = `[` + | ||
`${hours < 10 ? `0${hours}` : hours}:` + | ||
`${minutes < 10 ? `0${minutes}` : minutes}:` + | ||
`${seconds < 10 ? `0${seconds}` : seconds}` + | ||
`]`; | ||
return `${timestamp}${text}`; | ||
} | ||
} | ||
const base = new Proxy(function() {}, { | ||
get: function (target, prop, receiver) { | ||
const styler = new Proxy({}, { | ||
get: function (t, prop, rec) { | ||
const baseTarget = function() {} | ||
baseTarget.open = []; | ||
baseTarget.close = []; | ||
baseTarget.mods = []; | ||
if (prop in codes) { | ||
if (!open.includes(codes[prop][0])) open.push(codes[prop][0]); | ||
if (!close.includes(codes[prop][1])) close.push(codes[prop][1]); | ||
} else if (prop === "log") { | ||
baseTarget.open.push(codes[prop][0]); | ||
baseTarget.close.push(codes[prop][1]); | ||
} | ||
else if (prop in modifiers) baseTarget.mods.push(prop); | ||
else if (prop === "log") { | ||
return (...args) => { | ||
const output = this.apply(target, null, args); | ||
process.stdout.write(`${format(output)}\n`); | ||
}; | ||
} | ||
return receiver; | ||
}, | ||
apply: function (target, thisArg, args) { | ||
let styled = args.join(" "); | ||
for (const o of open) styled = `\u001b[${o}m${styled}`; | ||
for (const c of close) styled = `${styled}\u001b[${c}m`; | ||
open = []; close = []; | ||
return styled; | ||
process.stdout.write(`${format(args.join(" "))}\n`); | ||
} | ||
} else return rec; | ||
return new Proxy(baseTarget, { | ||
get: function (t, prop, rec) { | ||
if (prop in codes) { | ||
if (!t.open.includes(codes[prop][0])) t.open.push(codes[prop][0]); | ||
if (!t.close.includes(codes[prop][1])) t.close.push(codes[prop][1]); | ||
} | ||
else if (prop in modifiers && !t.mods.includes(prop)) t.mods.push(prop); | ||
else if (prop === "log") { | ||
return (...args) => { | ||
const output = this.apply(t, null, args); | ||
process.stdout.write(`${format(output)}\n`); | ||
}; | ||
} | ||
return rec; | ||
}, | ||
apply: function (t, thisArg, args) { | ||
let styled = args.join(" "); | ||
for (const m of t.mods) styled = modifiers[m](styled); | ||
for (const o of t.open) styled = `\u001b[${o}m${styled}`; | ||
for (const c of t.close) styled = `${styled}\u001b[${c}m`; | ||
return styled; | ||
} | ||
}); | ||
} | ||
}); | ||
module.exports = styler; | ||
{ | ||
"name": "console-styles", | ||
"version": "0.2.4", | ||
"version": "1.0.0", | ||
"typings": "index.d.ts", | ||
@@ -5,0 +5,0 @@ "description": "a styled console logger with no dependencies", |
@@ -25,2 +25,4 @@ # About | ||
console.log(styler.black.bgCyan("this is great Kappa", "hello")); | ||
console.log(styler.red("nested", styler.blue("colours"))); | ||
``` | ||
@@ -27,0 +29,0 @@ |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
6121
5
124
1
69