Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check if there is any stdin to get #1

Closed
matthewmueller opened this issue Aug 10, 2014 · 14 comments
Closed

check if there is any stdin to get #1

matthewmueller opened this issue Aug 10, 2014 · 14 comments

Comments

@matthewmueller
Copy link

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

Otherwise, this function just hangs.

@sindresorhus
Copy link
Owner

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?

@sindresorhus
Copy link
Owner

🐹

@matthewmueller
Copy link
Author

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

@sindresorhus
Copy link
Owner

@matthewmueller Like d2c098e, right?

@matthewmueller
Copy link
Author

yep, looks good. thanks!

@shinnn
Copy link

shinnn commented Nov 23, 2014

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.

@sindresorhus
Copy link
Owner

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

@shinnn
Copy link

shinnn commented Nov 23, 2014

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.

@kevva
Copy link
Contributor

kevva commented Nov 23, 2014

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

@sindresorhus
Copy link
Owner

@kevva yeah... fixed: 47062c7

@shinnn
Copy link

shinnn commented Nov 23, 2014

@sindresorhus @kevva Thanks!

@moos
Copy link

moos commented Dec 30, 2015

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?

@sindresorhus
Copy link
Owner

@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
Copy link

moos commented Dec 30, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants