sindresorhus/get-stdin

check if there is any stdin to get

matthewmueller opened this issue · 14 comments

Looks like process.stdin.isTTY checks to see if there is any data to get.

Otherwise, this function just hangs.

I usually do the check in my own code as I want different behavior: https://github.com/sindresorhus/gzip-size/blob/ac77bbe818b2fc705ba38c5326caf4f0d6b90b6b/cli.js#L39-L48

What should it return when it's not tty though? Nothing, null, empty string?

haha sorry, I guess probably an empty string / empty buffer, since that's what you'd be working with in the lib anyway.

yep, looks good. thanks!

return empty string/buffer when process.stdin.isTTY === false

Is this fix really right?
Usually I'd rather use stdin just when process.stdin.isTTY === false.

@shinnn Nothing is stopping you from doing that now. This is just a shortcut.

The example script in README doesn't work with v3.0.1.

// example.js
var stdin = require('get-stdin');

stdin(function (data) {
    console.log(data);
});
$ echo unicorns | node example.js

We expect to see unicorns on console, but it doesn't output anything.

Shouldn't it be process.stdin.isTTY instead of !process.stdin.isTTY in the if statement.

@kevva yeah... fixed: 47062c7

moos commented

I was surprise to find this very popular module doesn't support TTY. This would be very useful to have for a CLI program, and is easy enough to add (i.e., by listening for ^z or ^d as EOT markers in the input). Would you be willing to entertain a PR?

@moos I'm not really sure what you're asking for. Can you elaborate or provide an example of what you're trying to achieve?

moos commented

I'd like to be able to run a program and have it read from the keyboard as well as a pipe. getStdin works for:

$ echo 'abc' | foo -

but not for:

$ foo -
abc
def
^d
$

The dash in 'foo -' tells it to read STDIN (by convention only). Hope that's clear. This can be done in a backward compatible way and I think it's a useful feature to have.