diff --git a/src/io.rs b/src/io.rs index 8ac0fb7..6892f1c 100644 --- a/src/io.rs +++ b/src/io.rs @@ -1,4 +1,6 @@ use std::error::Error; +use std::io::IsTerminal as _; +use std::os::fd::AsFd as _; use std::str::FromStr; use async_stream::try_stream; @@ -7,13 +9,22 @@ use tokio::io::{self, AsyncBufReadExt as _, BufReader}; use crate::error::Result; -pub fn read_input() -> impl TryStream> +/// Read standard input as a [`TryStream`] of parsed lines of type `T`. +/// +/// Returns `None` if the stdin handle does not refer to a terminal/tty. +pub fn read_input() -> Option>> where T: FromStr + 'static, ::Err: Error + Send + Sync, { - try_stream! { - let mut stdin = BufReader::new(io::stdin()); + let stdin = io::stdin(); + + if stdin.as_fd().is_terminal() { + return None; + } + + Some(try_stream! { + let mut stdin = BufReader::new(stdin); let mut buf = String::new(); loop { @@ -27,7 +38,7 @@ where buf.clear(); } - } + }) } #[inline] diff --git a/src/main.rs b/src/main.rs index 0dd368b..5891a2b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,9 +39,10 @@ async fn main() { match cmd { Command::Shorten(args) => { let urls = if args.urls.is_empty() { - io::read_input::() - .map(|url| crash_if_err!(url)) - .boxed() + let Some(urls) = io::read_input::() else { + return; + }; + urls.map(|url| crash_if_err!(url)).boxed() } else { stream::iter(args.urls).boxed() };