-
Notifications
You must be signed in to change notification settings - Fork 4
/
format.ts
46 lines (36 loc) · 1.22 KB
/
format.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { green } from "./deps.ts";
export function format(data: ArrayBufferView) {
const bytes = new Uint8Array(data.buffer);
let out = " +-------------------------------------------------+\n";
out += ` |${
green(" 0 1 2 3 4 5 6 7 8 9 a b c d e f ")
}|\n`;
out +=
"+--------+-------------------------------------------------+----------------+\n";
const lineCount = Math.ceil(bytes.length / 16);
for (let line = 0; line < lineCount; line++) {
const start = line * 16;
const addr = start.toString(16).padStart(8, "0");
const lineBytes = bytes.slice(start, start + 16);
out += `|${green(addr)}| `;
lineBytes.forEach(
(byte) => (out += byte.toString(16).padStart(2, "0") + " "),
);
if (lineBytes.length < 16) {
out += " ".repeat(16 - lineBytes.length);
}
out += "|";
lineBytes.forEach(function (byte) {
return (out += byte > 31 && byte < 127
? green(String.fromCharCode(byte))
: ".");
});
if (lineBytes.length < 16) {
out += " ".repeat(16 - lineBytes.length);
}
out += "|\n";
}
out +=
"+--------+-------------------------------------------------+----------------+";
return out;
}