-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathTalonRepl.ts
More file actions
93 lines (77 loc) · 2.34 KB
/
Copy pathTalonRepl.ts
File metadata and controls
93 lines (77 loc) · 2.34 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { isWindows } from "@cursorless/node-common";
import * as childProcess from "node:child_process";
import * as os from "node:os";
const MAX_OUTPUT_TO_EAT = 20;
/**
* A wrapper around the Talon REPL that allows us to send commands to Talon
*/
export class TalonRepl {
private child?: childProcess.ChildProcessWithoutNullStreams;
action(action: string): Promise<string> {
return this.command(`actions.${action}`);
}
start(): Promise<void> {
return new Promise<void>((resolve, reject) => {
const path = getReplPath();
this.child = childProcess.spawn(path, { shell: true });
if (!this.child.stdin) {
reject("stdin is null");
return;
}
// The first data from the repl is always: Talon REPL | Python 3.9.13 ...
this.child.stdout.once("data", resolve);
});
}
stop(): Promise<void> {
return new Promise<void>((resolve) => {
if (this.child != null) {
this.child.on("close", () => {
this.child = undefined;
resolve();
});
this.child.stdin.end();
} else {
resolve();
}
});
}
private command(command: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
if (this.child != null) {
this.child.stdout.once("data", (data) => {
resolve(data.toString());
});
this.child.stdin.write(command);
this.child.stdin.write("\n");
} else {
reject();
}
});
}
/**
* Eat all output from the repl until it is responsive again. Prints the
* output to the console.
*/
async eatOutput(): Promise<void> {
let tryCount = 0;
while (true) {
// As a hack, we just put `0` in the REPL, which should cause it to print
// `0` back to us (we could put any Python value in there; `0` is just a
// simple one). We keep doing this until we get `0` back, which means the
// REPL is responsive again.
const output = (await this.command("0")).trim();
if (output === "0") {
break;
}
console.log(output);
if (tryCount++ > MAX_OUTPUT_TO_EAT) {
throw Error("Too much output to eat");
}
}
}
}
function getReplPath() {
return isWindows()
? `${os.homedir()}\\AppData\\Roaming\\talon\\venv\\3.13\\Scripts\\repl.bat`
: `${os.homedir()}/.talon/.venv/bin/repl`;
}