Node: "console.read/readline" wanted

Created on 19 Feb 2018  Â·  9Comments  Â·  Source: nodejs/node

Just a simple method but very useful for console app's IO.
Until now we don't have console.read/readline yet but console.log instead.

console feature request

All 9 comments

you can use the readline module which comes with node or go lower level with process.stdin which is just a stream.

as a sidenote, console's methods are actually specified by whatwg, and node would be ill-advised to add props that aren't part of the spec.

@devsnk:
Thanks I know about that and I can do something like this:

process.stdin.setEncoding('utf8');

process.stdin.on('readable', function() {
  var chunk = process.stdin.read();
  if (chunk !== null) {
    process.stdout.write('data: ' + chunk);
  }
});

process.stdin.on('end', function() {
  process.stdout.write('end');
});

But it would be better if a method can be offered directly to cope with this senario, because for a console app this is a frequently-used method for interactions. And what's more, it's not easy to use like that for a simple read/readline:)

@MaleDong Using the previously mentioned built-in readline is simpler:

require('readline').createInterface({
  input: process.stdin,
  crlfDelay: Infinity
}).once('line', (line) => {
  console.log(`Line from stdin: ${line}`);
});

console methods implemented by browsers are already implemented in node on a case-by-case basis. Seeing as how console.read() and/or console.readline() do not even exist as part of the standard Console API yet, I'd think there's an even slimmer chance of having them in core. Besides, I'm personally not a fan of having synchronous I/O methods like that (at least without a Sync suffix to the method name).

If you really want to read from stdin synchronously, there are ways to do that, but you'd still have to add a bit of extra coding to find the newline, etc.

@mscdex :
Many thanks first.
Then in my mind, I think it just depends on senarios——If you are writing a console app with synchronized steps by inputting something and do outputting (just like what we see for fs.readSync/writeSync....ect).

So that's why the synchronization version is submitted (Both Sync/Async are welcomed for different usages). And I'm still looking forward to a way convertable from async to sync for methods easily for this.

Besides, Nodejs is not only a simple js like what we see for js running on webbrowser, so we should make it "more different" like a server-side language running on different kinds of platforms with brilliant features or functions. "Console" is just one of its choices for desktop app's interaction.

@MaleDong Here is a very simple synchronous console readline interface

'use strict';

var fs = require('fs');

console.read = len => {
  var buff = Buffer.alloc(len);
  fs.readSync(process.stdin.fd, buff, 0, len);
  return buff.toString();
};

console.readline = () => {
  var str = '';
  do{
    var char = console.read(1);
    if(char !== '\n') str += char;
  }while(char !== '\n');
  return str;
};

Why do you think it would be worth implementing this in the core?

Consider the prompt package. I don't think this belongs in core.

Unless browsers implement such a interface, I don't see us extending console like that.

@6-8-axnw1bom81v5xa3nh48c :The reason why I wanna make "console" has a readline/read function is because I think Nodejs is quite different from pure js in the browser. And this language is more based on the server-side running than running on the client. As for this, read/readline/log is the most basic function:)

But still say "Many thanks for your help",and if I have free time I'll try to add these features to Node as the extended functions.

Thanks for your suggestion and participation @MaleDong - I'm going to close this issue now - if you feel like there is still discussion to be had - please feel free to let me know and we'll reopen it for further discussion.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Brekmister picture Brekmister  Â·  3Comments

vsemozhetbyt picture vsemozhetbyt  Â·  3Comments

addaleax picture addaleax  Â·  3Comments

danialkhansari picture danialkhansari  Â·  3Comments

sandeepks1 picture sandeepks1  Â·  3Comments