My Deno REPL program lacks interactiveness - Stack Overflow

I am attempting to create a Deno REPL (read-evaluate-print-loop).The code I have so far only functions

I am attempting to create a Deno REPL (read-evaluate-print-loop).

The code I have so far only functions as a RELELEL...P (read-evaluate-loop-evaluate-loop-evaluate-loop-...-print).

What I wish to achieve is that each "echo" command be displayed immediately, instead of delaying all output until after each process has completed.

Using streaming, is it possible to achieve the REPL affect that I desire with Deno?

Thanks!

The code I have thus far is as follows...

import {
  mergeReadableStreams,
} from ".ts";

import { red, green } from ".ts";

import { TextLineStream } from "jsr:@std/streams/text-line-stream";
import { sleep } from ".ts"
import { writeAllSync } from "jsr:@std/io/write-all";

const prompt = "> ";

async function main(line: string) {
  const command = new Deno.Command("bash", {
    args: ["-c", `${line}`],
    stdout: "piped",
    stderr: "piped",
  });

  const process = command.spawn();
  const { stdout, stderr, status } = process;

  const [stdoutReader, stderrReader] = [
    stdout.pipeThrough(new TextDecoderStream()).pipeThrough(new TextLineStream()).getReader(),
    stderr.pipeThrough(new TextDecoderStream()).pipeThrough(new TextLineStream()).getReader(),
  ];

  let stdoutDone = false;
  let stderrDone = false;

  while (!stdoutDone || !stderrDone) {
    const [stdoutResult, stderrResult] = await Promise.all([
      stdoutReader.read(),
      stderrReader.read(),
    ]);

    if (stdoutResult.done) {
      stdoutDone = true;
    } else {
      writeAllSync(Deno.stdout, new TextEncoder().encode(green(stdoutResult.value) + "\n"));
    }

    if (stderrResult.done) {
      stderrDone = true;
    } else {
      writeAllSync(Deno.stdout, new TextEncoder().encode(red(stderrResult.value) + "\n"));
    }
  }

  const { success, code, signal } = await status;
  console.log(`done! success: ${success} code: ${code} signal: ${signal}`);
}

try {
  const reader = Deno.stdin.readable
    .pipeThrough(new TextDecoderStream())
    .pipeThrough(new TextLineStream())
    .getReader();

  while (true) {
    writeAllSync(Deno.stdout, new TextEncoder().encode(`${prompt} `));

    const { value, done } = await reader.read();
    if (done) break;

    await main(value);
  }
} catch (e) {
  console.error(e);
}

The test program I am using that depicts the issue is this...

#!/usr/bin/env bash

echo "hello stdout"
echo "hello stderr" 1>&2 

for ((i=0;i<10;i++)); do

echo $i
sleep 2
done

I wish to see each "echo $i" every two seconds as bash interprets them, but instead I see them all at once after twenty seconds of delay.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742313990a4420466.html

相关推荐

  • My Deno REPL program lacks interactiveness - Stack Overflow

    I am attempting to create a Deno REPL (read-evaluate-print-loop).The code I have so far only functions

    18小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信