-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmonitor.ts
More file actions
63 lines (55 loc) · 2.12 KB
/
monitor.ts
File metadata and controls
63 lines (55 loc) · 2.12 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Command, Env, Opt } from "./lib/command.js";
import { stdout } from "process";
import { getDevice } from "./util.js";
import chalk from "chalk";
import readline from "readline";
const cmd = new Command("Monitor program output", {
action: async (options: Record<string, string | boolean>, args: Record<string, string>, env: Env) => {
const echo = !(options["no-echo"] as boolean);
const rl = readline.createInterface({ input: process.stdin });
readline.emitKeypressEvents(process.stdin, rl);
process.stdin.setRawMode(true);
const port = options["port"] as string;
const baudrate = options["baudrate"] as string;
const socket = options["socket"] as string;
const ble = options["ble"] as string;
const device = await getDevice(port, baudrate, socket, ble, env);
device.programOutput.onData((data) => {
stdout.write(data);
});
device.programError.onData((data) => {
stdout.write(chalk.red(data));
});
await new Promise((resolve) => {
process.stdin.on("keypress", (str, key) => {
if (key.ctrl && key.name === "c") {
device.programOutput.onData(undefined);
device.programError.onData(undefined);
process.stdin.setRawMode(false);
rl.close();
resolve(null);
}
else {
if (echo) {
if (key.sequence === "\r") {
stdout.write("\r\n");
}
else if (str) {
stdout.write(str);
}
}
let out = key.sequence;
if (out === "\r") {
out = "\n";
}
device.programInput.write(Buffer.from(out, "utf-8"));
}
});
});
},
options: {
"no-echo": new Opt("Echo input", { isFlag: true }),
},
chainable: true
});
export default cmd;