add cli log with colors

This commit is contained in:
王劲鹏 2020-03-18 14:14:36 +08:00 committed by osborn
parent bcd1dd8844
commit 281d3d7425
2 changed files with 51 additions and 1 deletions

View File

@ -0,0 +1,37 @@
var styles = {
'bold' : ['\x1B[1m', '\x1B[22m'],
'italic' : ['\x1B[3m', '\x1B[23m'],
'underline' : ['\x1B[4m', '\x1B[24m'],
'inverse' : ['\x1B[7m', '\x1B[27m'],
'strikethrough' : ['\x1B[9m', '\x1B[29m'],
'white' : ['\x1B[37m', '\x1B[39m'],
'grey' : ['\x1B[90m', '\x1B[39m'],
'black' : ['\x1B[30m', '\x1B[39m'],
'blue' : ['\x1B[34m', '\x1B[39m'],
'cyan' : ['\x1B[36m', '\x1B[39m'],
'green' : ['\x1B[32m', '\x1B[39m'],
'magenta' : ['\x1B[35m', '\x1B[39m'],
'red' : ['\x1B[31m', '\x1B[39m'],
'yellow' : ['\x1B[33m', '\x1B[39m'],
'whiteBG' : ['\x1B[47m', '\x1B[49m'],
'greyBG' : ['\x1B[49;5;8m', '\x1B[49m'],
'blackBG' : ['\x1B[40m', '\x1B[49m'],
'blueBG' : ['\x1B[44m', '\x1B[49m'],
'cyanBG' : ['\x1B[46m', '\x1B[49m'],
'greenBG' : ['\x1B[42m', '\x1B[49m'],
'magentaBG' : ['\x1B[45m', '\x1B[49m'],
'redBG' : ['\x1B[41m', '\x1B[49m'],
'yellowBG' : ['\x1B[43m', '\x1B[49m']
}
function log (key, obj) {
if (typeof obj === 'string') {
console.log(styles[key][0] + '%s' + styles[key][1], obj)
} else if (typeof obj === 'object') {
console.log(styles[key][0] + '%o' + styles[key][1], obj)
} else {
console.log(styles[key][0] + '%s' + styles[key][1], obj)
}
}
module.exports = log

View File

@ -2,6 +2,7 @@ const ws = require('nodejs-websocket')
const { exec, spawn } = require('child_process')
const fs = require('fs')
const log = require('./console')
var server
var contextId = null
var clientConnection = null
@ -25,7 +26,6 @@ const createServer = () => {
}
connection.on('text', function (result) {
console.log('text', result)
let resultObject = JSON.parse(result)
switch(resultObject.cmd) {
case 'DEBUG':
@ -49,6 +49,19 @@ const createServer = () => {
})
}, 1500)
break
case 'EXCEPTION':
log('redBG', resultObject.data.source)
log('redBG', resultObject.data.exception)
break
case 'LOG':
if (resultObject.data.type == 'DEFAULT') {
log('black', resultObject.data.message)
} else if (resultObject.data.type == 'ERROR') {
log('red', resultObject.data.message)
} else if (resultObject.data.type == 'WARN') {
log('blue', resultObject.data.message)
}
break
}
})