Nice one. I tried it with your code, worked for me:
const fs = require('fs');
function* __readline() {
let lines = fs.readFileSync(0).toString().split(/\n/);
while(lines.length) yield lines.shift();
return false;
}
const _readline = __readline();
function readline() {
return _readline.next().value;
}
let line, nr = 0;
while((line = readline()) !== false){
if(line) console.log(nr++, line)
}
And then:
$ node sync_read_stdin.js <<< $’ foo \n bar ’
0 ’ foo ’
1 ’ bar ’
Edit - I realised your issue is with the runs.
To have multiple runs i’d just have another delimiter for the runs. Or would that not solve your problem? Something like:
function* __readline() {
let runs = fs.readFileSync(0).toString().split(/\n\n/);
let lines = runs.map(s => s.split(/\n/));
while(lines.length) yield lines.shift(); //or however you prefer to receive your run inputs
return false;
}
$ node sync_read_stdin.js <<< $‘foo1 \n bar1 \n\n foo2 \n bar2’
0 [ 'foo1 ', ’ bar1 ’ ]
1 [ ’ foo2 ‘, ’ bar2’, ‘’ ]
(BTW those are single quotes, the code block didnt work and the quote block replaced the single quotes)