owobot/util/chatparse_console.js

108 lines
3.5 KiB
JavaScript
Raw Normal View History

const settings = require('../settings.json');
2024-08-01 20:11:59 -04:00
const lang = require('./mc_lang.js');
const _consoleColors = require("./consolecolors.json");
let consoleColors;
let consoleColors24;
if(_consoleColors[settings.terminalMode]){
consoleColors=_consoleColors[settings.terminalMode].fourBit
consoleColors24=_consoleColors[settings.terminalMode].twentyFourBit
} else {
consoleColors=_consoleColors.none.fourBit
consoleColors24=_consoleColors.none.twentyFourBit
2024-07-30 05:56:23 -04:00
}
2024-08-01 20:11:59 -04:00
2024-07-30 05:56:23 -04:00
const hexColorParser = (color) => {
2024-08-03 04:14:42 -04:00
if(!consoleColors24.enabled || consoleColors24.bit!==24){ // Hex color parsing to the 8 bit and 4 bit modes has not been implemented yet
return "";
}
let out = '\x1B[0;'
2024-07-30 05:56:23 -04:00
const redChannel = Number('0x' + color.slice(1, 3))
const greenChannel = Number('0x' + color.slice(3, 5))
const blueChannel = Number('0x' + color.slice(5, 7))
if (!consoleColors24.lightMode && redChannel < 64 && greenChannel < 64 && blueChannel < 64) {
out += '48;2;220;220;220;'
} else if (consoleColors24.lightMode && ((redChannel > 192 && greenChannel > 192 && blueChannel > 192) || greenChannel > 160)) {
out += '48;2;0;0;0;'
2024-07-30 05:56:23 -04:00
}
return out + `38;2;${redChannel};${greenChannel};${blueChannel}m`
2024-07-30 05:56:23 -04:00
}
2024-08-01 20:11:59 -04:00
2024-07-30 05:56:23 -04:00
const processColor = (col, rcol) => {
2024-07-30 18:11:47 -04:00
let out;
2024-07-30 05:56:23 -04:00
if (col === 'reset') {
2024-07-30 18:11:47 -04:00
out = rcol
2024-07-30 05:56:23 -04:00
} else if (col.startsWith('#')) {
2024-07-30 18:11:47 -04:00
out = hexColorParser(col)
2024-07-30 05:56:23 -04:00
} else {
2024-07-30 18:11:47 -04:00
out = consoleColors[col]
2024-07-30 05:56:23 -04:00
}
return out
}
2024-08-01 20:11:59 -04:00
2024-07-30 18:11:47 -04:00
const parse = function (_data, l = 0, resetColor = consoleColors.reset) {
2024-07-30 05:56:23 -04:00
if (l >= 12) {
return ['', '', '']
}
let data
if (typeof _data === 'string') {
data = { text: _data, color: 'reset' }
} else if (typeof _data === 'number') {
data = { text: _data + '', color: 'reset' }
} else if (_data.constructor === Array) {
data = { extra: _data, color: 'reset' }
} else {
data = _data
}
let nkt = false
2024-08-01 20:11:59 -04:00
let out = ""
2024-07-30 05:56:23 -04:00
if (data['']) {
data.text = data['']
nkt = true
}
if (data.color) {
2024-08-01 20:11:59 -04:00
out += processColor(data.color, resetColor)
2024-07-30 05:56:23 -04:00
} else {
2024-08-01 20:11:59 -04:00
out += resetColor
2024-07-30 05:56:23 -04:00
}
if (data.text) {
let _text = data.text
if (typeof _text === 'number') {
_text = _text.toString()
}
if (nkt) {
2024-08-01 20:11:59 -04:00
out += resetColor
2024-07-30 05:56:23 -04:00
}
2024-08-01 20:11:59 -04:00
out += _text.replaceAll('\x1b', '').replaceAll('\x0e', '') // Remove escape codes and [SO] from console format
2024-07-30 05:56:23 -04:00
}
if (data.translate) {
let trans = data.translate.replace(/%%/g, '\ue123').replaceAll('\x1b', '').replaceAll('\x0e', '') // Remove escape codes from console format
if (lang[trans] !== undefined) {
trans = lang[trans].replace(/%%/g, '\ue123')
}
for (const i in data.with) {
const j2 = parse(data.with[i], l + 1, data.color ? processColor(data.color, resetColor) : resetColor)
2024-08-01 20:11:59 -04:00
trans = trans.replace(/%s/, j2.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans = trans.replaceAll(`%${+i + 1}$s`, j2.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
2024-07-30 05:56:23 -04:00
}
2024-08-01 20:11:59 -04:00
out += trans.replace(/\ue123/g, '%').replace(/\ue124/g, '%s').replace(/\ue125/g, '$s')
2024-07-30 05:56:23 -04:00
}
if (data.extra) {
for (const i in data.extra) {
const parsed = parse(data.extra[i], l, data.color ? processColor(data.color, resetColor) : resetColor)
2024-08-01 20:11:59 -04:00
out += parsed
2024-07-30 05:56:23 -04:00
}
}
2024-08-01 20:11:59 -04:00
out += resetColor
2024-07-30 05:56:23 -04:00
return out
}
const parse2 = function (_data, l, resetColor) {
try {
return parse(_data)
} catch (e) {
console.error(e)
2024-08-01 20:11:59 -04:00
return '\x1B[0m\x1B[38;2;255;85;85mAn error occured while parsing a message. See console for more information.\nJSON that caused the error: ' + JSON.stringify(_data)
2024-07-30 05:56:23 -04:00
}
}
module.exports = parse2