From fe61271d720b68f2d8d220fb60f9ed33a0a37b26 Mon Sep 17 00:00:00 2001 From: Kornel Date: Sun, 14 Apr 2024 01:45:20 +0100 Subject: [PATCH] Warn about stdin/stdout terminal --- Cargo.toml | 4 ++-- src/bin/gifski.rs | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6af40f3..1ebb0ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,8 +21,8 @@ name = "gifski" required-features = ["binary"] [dependencies] -clap = { version = "4.3.24", features = ["cargo"], optional = true } -imgref = "1.10.0" +clap = { version = "4.5.4", features = ["cargo"], optional = true } +imgref = "1.10.1" gif = { version = "0.13.1", default-features = false, features = ["std", "raii_no_panic"] } gif-dispose = "5.0.0-beta.2" imagequant = "4.3.0" diff --git a/src/bin/gifski.rs b/src/bin/gifski.rs index f82c0d8..55bbd5a 100644 --- a/src/bin/gifski.rs +++ b/src/bin/gifski.rs @@ -12,6 +12,7 @@ use clap::builder::NonEmptyStringValueParser; use std::io::stdin; use std::io::BufRead; use std::io::BufReader; +use std::io::IsTerminal; use std::io::Read; use std::io::StdinLock; use std::io::Stdout; @@ -260,7 +261,11 @@ fn bin_main() -> BinResult<()> { let decode_thread = thread::Builder::new().name("decode".into()).spawn_scoped(scope, move || { let mut decoder = if let [path] = &frames[..] { let mut src = if path.as_os_str() == "-" { - SrcPath::Stdin(BufReader::new(stdin().lock())) + let fd = stdin().lock(); + if fd.is_terminal() { + eprintln!("warning: used '-' as the input path, but the stdin is a terminal, not a file."); + } + SrcPath::Stdin(BufReader::new(fd)) } else { SrcPath::Path(path.to_path_buf()) }; @@ -303,6 +308,7 @@ fn bin_main() -> BinResult<()> { let mut file_tmp; let mut stdio_tmp; + let mut print_terminal_err = false; let out: &mut dyn io::Write = match output_path { DestPath::Path(p) => { file_tmp = File::create(p) @@ -311,6 +317,7 @@ fn bin_main() -> BinResult<()> { }, DestPath::Stdout => { stdio_tmp = io::stdout().lock(); + print_terminal_err = stdio_tmp.is_terminal(); &mut stdio_tmp }, }; @@ -333,6 +340,10 @@ fn bin_main() -> BinResult<()> { &mut pb }; + if print_terminal_err { + eprintln!("warning: used '-' as the output path, but the stdout is a terminal, not a file"); + std::thread::sleep(Duration::from_secs(3)); + } let write_result = writer.write(io::BufWriter::new(out), progress); let thread_result = decode_thread.join().map_err(panic_err)?; check_errors(write_result, thread_result)?;