Skip to content

Commit f7cf98d

Browse files
committed
add hexdump
1 parent ee112f9 commit f7cf98d

2 files changed

Lines changed: 49 additions & 11 deletions

File tree

exec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ export async function exec(cmd: string | string[], stdin = "") {
7979
return new TextDecoder().decode(stdout).trim();
8080
}
8181

82-
export async function* tail(cmd: string | string[], stdin = "") {
82+
export type TailOptions = {
83+
fromStderr: boolean;
84+
};
85+
86+
export async function* tail(cmd: string | string[], _opts?: TailOptions) {
8387
if (!Array.isArray(cmd)) {
8488
cmd = cmd.split(" ");
8589
}
@@ -90,26 +94,22 @@ export async function* tail(cmd: string | string[], stdin = "") {
9094
cmd,
9195
};
9296

93-
if (stdin) {
94-
opts.stdin = "piped";
95-
}
96-
9797
const p = Deno.run(opts);
98-
if (stdin) {
99-
const encoder = new TextEncoder();
100-
await p.stdin!.write(encoder.encode(stdin));
101-
p.stdin!.close();
98+
let s = p.stdout!;
99+
100+
if (_opts?.fromStderr) {
101+
s = p.stderr!;
102102
}
103103

104104
for (;;) {
105105
const buf = new Uint8Array(4096);
106-
const n = await p.stdout?.read(buf);
106+
const n = await s.read(buf);
107107

108108
if (n === null || n === undefined) {
109109
break;
110110
}
111111

112-
const text = new TextDecoder().decode(buf);
112+
const text = new TextDecoder().decode(buf.slice(0, n));
113113
yield text;
114114
}
115115

hex.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
export function isprint(x: number) {
2+
return x >= 0x20 && x <= 0x7e;
3+
}
4+
5+
export function dump(buf: Uint8Array | string) {
6+
if (typeof buf === "string") {
7+
buf = new TextEncoder().encode(buf);
8+
}
9+
10+
let out = "";
11+
12+
let i = 0;
13+
let j = 0;
14+
15+
for (i = 0; i < buf.length; i += 16) {
16+
out += i.toString(16).padStart(6, "0");
17+
out += ": ";
18+
19+
for (j = 0; j < 16; j++) {
20+
if (i + j < buf.length) {
21+
out += buf[i + j].toString(16).padStart(2, "0");
22+
out += " ";
23+
} else {
24+
out += " ";
25+
}
26+
}
27+
out += " ";
28+
29+
for (j = 0; j < 16; j++) {
30+
if (i + j < buf.length) {
31+
out += isprint(buf[i + j]) ? String.fromCharCode(buf[i + j]) : ".";
32+
}
33+
}
34+
out += "\n";
35+
}
36+
37+
return out;
38+
}

0 commit comments

Comments
 (0)