node.js: readSync from stdin?

前端 未结 11 1545
北海茫月
北海茫月 2020-12-08 01:48

Is it possible to synchronously read from stdin in node.js? Because I\'m writing a brainfuck to JavaScript compiler in JavaScript (just for fun). Brainfuck supports a read o

11条回答
  •  温柔的废话
    2020-12-08 02:24

    I used this workaround on node 0.10.24/linux:

    var fs = require("fs")
    var fd = fs.openSync("/dev/stdin", "rs")
    fs.readSync(fd, new Buffer(1), 0, 1)
    fs.closeSync(fd)
    

    This code waits for pressing ENTER. It reads one character from line, if user enters it before pressing ENTER. Other characters will be remained in the console buffer and will be read on subsequent calls to readSync.

提交回复
热议问题