ui
This commit is contained in:
+37
@@ -0,0 +1,37 @@
|
||||
import chalk from 'chalk'
|
||||
|
||||
const _colorCache = {}
|
||||
|
||||
export function chalkColor (name) {
|
||||
let color = _colorCache[name]
|
||||
if (color) {
|
||||
return color
|
||||
}
|
||||
|
||||
if (name[0] === '#') {
|
||||
color = chalk.hex(name)
|
||||
} else {
|
||||
color = chalk[name] || chalk.keyword(name)
|
||||
}
|
||||
|
||||
_colorCache[name] = color
|
||||
return color
|
||||
}
|
||||
|
||||
const _bgColorCache = {}
|
||||
|
||||
export function chalkBgColor (name) {
|
||||
let color = _bgColorCache[name]
|
||||
if (color) {
|
||||
return color
|
||||
}
|
||||
|
||||
if (name[0] === '#') {
|
||||
color = chalk.bgHex(name)
|
||||
} else {
|
||||
color = chalk['bg' + name[0].toUpperCase() + name.slice(1)] || chalk.bgKeyword(name)
|
||||
}
|
||||
|
||||
_bgColorCache[name] = color
|
||||
return color
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import dayjs from 'dayjs'
|
||||
|
||||
export function formatDate (timeFormat, date) {
|
||||
return dayjs(date).format(timeFormat)
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import { sep } from 'path'
|
||||
|
||||
export function parseStack (stack) {
|
||||
const cwd = process.cwd() + sep
|
||||
|
||||
const lines = stack
|
||||
.split('\n')
|
||||
.splice(1)
|
||||
.map(l => l
|
||||
.trim()
|
||||
.replace('file://', '')
|
||||
.replace(cwd, '')
|
||||
)
|
||||
|
||||
return lines
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
export const TYPE_COLOR_MAP = {
|
||||
info: 'cyan'
|
||||
}
|
||||
|
||||
export const LEVEL_COLOR_MAP = {
|
||||
0: 'red',
|
||||
1: 'yellow',
|
||||
2: 'white',
|
||||
3: 'green'
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
import { vsprintf } from 'printj'
|
||||
|
||||
const FORMAT_ARGS = [
|
||||
['additional', 5],
|
||||
['message', 4],
|
||||
['type', 2],
|
||||
['date', 1],
|
||||
['tag', 3]
|
||||
] // .sort((a, b) => b[0].length - a[0].length)
|
||||
|
||||
const _compileCache = {}
|
||||
// process.on('beforeExit', () => { console.log(_compileCache) })
|
||||
|
||||
export function compileFormat (format) {
|
||||
if (_compileCache[format]) {
|
||||
return _compileCache[format]
|
||||
}
|
||||
|
||||
let _format = format
|
||||
for (const arg of FORMAT_ARGS) {
|
||||
_format = _format.replace(new RegExp('([%-])' + arg[0], 'g'), '$1' + arg[1])
|
||||
}
|
||||
|
||||
_compileCache[format] = _format
|
||||
return _format
|
||||
}
|
||||
|
||||
export function formatString (format, argv) {
|
||||
return vsprintf(compileFormat(format), argv)
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
export function assignGlobalReference (newInstance, referenceKey) {
|
||||
if (!newInstance.constructor || (global[referenceKey] && !global[referenceKey].constructor)) {
|
||||
throw new Error('Assigning to global reference is only supported for class instances')
|
||||
} else if (newInstance.constructor && !global[referenceKey]) {
|
||||
global[referenceKey] = newInstance
|
||||
} else if (!(
|
||||
newInstance instanceof global[referenceKey].constructor ||
|
||||
global[referenceKey] instanceof newInstance.constructor
|
||||
)) {
|
||||
throw new Error(`Not a ${global[referenceKey].constructor.name} instance`)
|
||||
}
|
||||
|
||||
const oldInstance = Object.create(global[referenceKey])
|
||||
|
||||
for (const prop in global[referenceKey]) {
|
||||
oldInstance[prop] = global[referenceKey][prop]
|
||||
delete global[referenceKey][prop]
|
||||
}
|
||||
|
||||
for (const prop of Object.getOwnPropertySymbols(global[referenceKey])) {
|
||||
oldInstance[prop] = global[referenceKey][prop]
|
||||
delete global[referenceKey][prop]
|
||||
}
|
||||
|
||||
for (const prop in newInstance) {
|
||||
global[referenceKey][prop] = newInstance[prop]
|
||||
}
|
||||
|
||||
for (const prop of Object.getOwnPropertySymbols(newInstance)) {
|
||||
global[referenceKey][prop] = newInstance[prop]
|
||||
}
|
||||
|
||||
return oldInstance
|
||||
}
|
||||
|
||||
export function assignGlobalConsola (newConsola) {
|
||||
return assignGlobalReference(newConsola, 'consola')
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
export function isPlainObject (obj) {
|
||||
return Object.prototype.toString.call(obj) === '[object Object]'
|
||||
}
|
||||
|
||||
// TODO: remove for consola@3
|
||||
export function isLogObj (arg) {
|
||||
// Should be plain object
|
||||
if (!isPlainObject(arg)) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Should contains either 'message' or 'args' field
|
||||
if (!arg.message && !arg.args) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Handle non-standard error objects
|
||||
if (arg.stack) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
import { writeSync } from 'fs'
|
||||
|
||||
export function writeStream (data, stream, mode = 'default') {
|
||||
const write = stream.__write || stream.write
|
||||
|
||||
switch (mode) {
|
||||
case 'async':
|
||||
return new Promise((resolve) => {
|
||||
if (write.call(stream, data) === true) {
|
||||
resolve()
|
||||
} else {
|
||||
stream.once('drain', () => { resolve() })
|
||||
}
|
||||
})
|
||||
case 'sync':
|
||||
return writeSync(stream.fd, data)
|
||||
default:
|
||||
return write.call(stream, data)
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
export function centerAlign (str, len, space = ' ') {
|
||||
const free = len - str.length
|
||||
if (free <= 0) {
|
||||
return str
|
||||
}
|
||||
const freeLeft = Math.floor(free / 2)
|
||||
let _str = ''
|
||||
for (let i = 0; i < len; i++) {
|
||||
_str += (i < freeLeft || i >= freeLeft + str.length) ? space : str[i - freeLeft]
|
||||
}
|
||||
return _str
|
||||
}
|
||||
|
||||
export function rightAlign (str, len, space = ' ') {
|
||||
const free = len - str.length
|
||||
if (free <= 0) {
|
||||
return str
|
||||
}
|
||||
let _str = ''
|
||||
for (let i = 0; i < len; i++) {
|
||||
_str += i < free ? space : str[i - free]
|
||||
}
|
||||
return _str
|
||||
}
|
||||
|
||||
export function leftAlign (str, len, space = ' ') {
|
||||
let _str = ''
|
||||
for (let i = 0; i < len; i++) {
|
||||
_str += i < str.length ? str[i] : space
|
||||
}
|
||||
return _str
|
||||
}
|
||||
|
||||
export function align (alignment, str, len, space = ' ') {
|
||||
switch (alignment) {
|
||||
case 'left': return leftAlign(str, len, space)
|
||||
case 'right': return rightAlign(str, len, space)
|
||||
case 'center': return centerAlign(str, len, space)
|
||||
default: return str
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user