diff options
Diffstat (limited to 'Components/DisplayFile/hex.js')
-rw-r--r-- | Components/DisplayFile/hex.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/Components/DisplayFile/hex.js b/Components/DisplayFile/hex.js new file mode 100644 index 0000000..f062608 --- /dev/null +++ b/Components/DisplayFile/hex.js @@ -0,0 +1,44 @@ +const chalk = require(__dirname + '/../../MistyCore/node_modules/chalk'); + +function hexEight(dec) { + let hex = Math.round(dec).toString(16); + let zero = "00000000"; + return zero.substring(0, 8 - hex.length) + hex; +} + +function fixLength(string, length) { + let end = " ".repeat(length); + if (string.length > length) return string.substring(0, length); + + return string + end.substring(0, length - string.length); +} + +module.exports = (buffer, color) => { + let ret = ""; + let lines = buffer.toString("hex").match(/.{1,32}/g).map(i => i.match(/.{1,2}/g).join(" ")); + + let byte = 0; + for (let line of lines) { + if (color) { + ret += chalk.gray(hexEight(byte)) + " " + fixLength(line, 47).split(" ").map((i, _) => { + if ((_ + 1) % 2 === 1) { + return chalk.red(i); + } else { + return chalk.magenta(i); + } + }).join(" ") + " " + chalk.blue(Buffer.from(line.replaceAll(" ", ""), "hex").toString().replace(/[\x00-\x1F]/gm, chalk.gray("."))) + "\n"; + } else { + ret += hexEight(byte) + " " + fixLength(line, 47) + " " + Buffer.from(line.replaceAll(" ", ""), "hex").toString().replace(/[\x00-\x1F]/gm, ".") + "\n"; + } + + byte += line.replaceAll(" ", "").length / 2; + } + + if (color) { + ret += chalk.gray(hexEight(byte)); + } else { + ret += hexEight(byte); + } + + return ret; +}
\ No newline at end of file |