-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
26 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
#!/usr/bin/env -S deno run --unstable --allow-all | ||
import { Pty } from "https://deno.land/x/[email protected]/mod.ts"; | ||
import { stripAnsiCode } from "https://deno.land/[email protected]/fmt/colors.ts"; | ||
import { | ||
assertEquals, | ||
assertMatch, | ||
} from "https://deno.land/[email protected]/assert/mod.ts"; | ||
#!/usr/bin/env -S deno run --unstable-ffi --allow-all | ||
import { Pty } from "jsr:@sigma/[email protected]"; | ||
import { stripAnsiCode } from "jsr:@std/[email protected]/colors"; | ||
import { assertEquals, assertMatch } from "jsr:@std/[email protected]"; | ||
|
||
const ENCODER = new TextEncoder(); | ||
|
||
|
@@ -16,56 +13,52 @@ if (import.meta.main) { | |
}); | ||
|
||
while (true) { | ||
let input = await pty.read(); | ||
if (input === undefined) break; | ||
let { data: input, done } = await pty.read(); | ||
if (done) break; | ||
input = stripAnsiCode(input); | ||
|
||
if (input.includes("In:")) break; | ||
await sleep(100); | ||
} | ||
|
||
const write = async (input: string) => await pty.write(input + "\n\r"); | ||
const write = async (input: string) => await pty.write(`${input}\n\r`); | ||
const evalRs = async (input: string) => { | ||
await write(input); | ||
// detect output | ||
// the plan is: | ||
// In: ... | ||
// ... // we want his line | ||
// ... // and this line | ||
// In: ... | ||
// The output is all the part between 2 `In:` | ||
// | ||
// Should not be used with input that gives empty output | ||
// like rust statements | ||
let out = ""; | ||
let end_detect = 0; | ||
// TODO | ||
let lastResult = ""; | ||
let idx = 0; | ||
while (true) { | ||
let o = await pty.read(); | ||
if (o === undefined) break; | ||
o = stripAnsiCode(o); | ||
if (!o.startsWith("In:")) { | ||
end_detect += 1; | ||
out += o; | ||
} else if (end_detect >= 1 && o.startsWith("In:")) { | ||
break; | ||
let { data: output, done } = await pty.read(); | ||
if (done) break; | ||
output = stripAnsiCode(output).trim(); | ||
if (output && output !== "In:") lastResult = output; | ||
|
||
if (!output && lastResult) { | ||
idx++; | ||
} else { | ||
end_detect = 0; | ||
idx = 0; | ||
} | ||
if (idx === 5) { | ||
const result = lastResult.replace(/^Out:/, "").trim(); | ||
return result; | ||
} | ||
await sleep(100); | ||
} | ||
const result = out!.replace(/^Out:/, "").trim(); | ||
return result; | ||
// not really needed | ||
return ""; | ||
}; | ||
|
||
const test = async ( | ||
input: string, | ||
expected: string | RegExp, | ||
) => { | ||
Deno.stdout.write(ENCODER.encode("eval: " + input)); | ||
Deno.stdout.write(ENCODER.encode(`eval: ${input}`)); | ||
const output = await evalRs(input); | ||
// try catch just to add a new line before the error | ||
try { | ||
if (typeof expected == "string") { | ||
if (typeof expected === "string") { | ||
assertEquals( | ||
output, | ||
expected, | ||
|