Function grep_cli::is_readable_stdin

source ·
pub fn is_readable_stdin() -> bool
Expand description

Returns true if and only if stdin is believed to be readable.

When stdin is readable, command line programs may choose to behave differently than when stdin is not readable. For example, command foo might search the current directory for occurrences of foo where as command foo < some-file or cat some-file | command foo might instead only search stdin for occurrences of foo.

Note that this isn’t perfect and essentially corresponds to a heuristic. When things are unclear (such as if an error occurs during introspection to determine whether stdin is readable), this prefers to return false. That means it’s possible for an end user to pipe something into your program and have this return false and thus potentially lead to ignoring the user’s stdin data. While not ideal, this is perhaps better than falsely assuming stdin is readable, which would result in blocking forever on reading stdin. Regardless, commands should always provide explicit fallbacks to override behavior. For example, rg foo - will explicitly search stdin and rg foo ./ will explicitly search the current working directory.