From 617c86fb93e537fe55e100844060221fbece5146 Mon Sep 17 00:00:00 2001 From: Dylan Beattie Date: Fri, 10 Dec 2021 10:42:32 +0000 Subject: [PATCH] Add support for pipe/redirect stdin This has a WEIRD bug. Redirected IO works fine, but if you run it WITHOUT redirected IO, you have to press Enter once the first time you're prompted for input - and then the SECOND time, it'll actually read the input. NodeJS FTW... --- satriani/rockstar.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/satriani/rockstar.js b/satriani/rockstar.js index 0964819a..5ae79150 100644 --- a/satriani/rockstar.js +++ b/satriani/rockstar.js @@ -1,6 +1,21 @@ const fs = require('fs'); const satriani = require('./satriani.js'); var readlineSync = require('readline-sync'); +var readline = require('readline'); +var rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, + terminal: false +}); + +const stdin_buffer = []; +let use_buffered_stdin = false; +rl.on('line', (line) => { + use_buffered_stdin = true; + stdin_buffer.push(line) +}); + +read_stdin = _ => (use_buffered_stdin ? stdin_buffer.shift() ?? "" : readlineSync.question()); var sourceFilePath = process.argv[2]; var watch = process.argv.filter(x => x.toLowerCase() === "--watch").length > 0; @@ -11,7 +26,7 @@ function execute() { if (err) throw err; try { let tree = rockstar.parse(data); - let input = readlineSync.question; + let input = read_stdin; let output = console.log; let result = rockstar.run(tree, input, output) console.log(result ? result : "(program returned no output)");