diff --git a/src/main.rs b/src/main.rs index 9a51b686d..38e29b821 100644 --- a/src/main.rs +++ b/src/main.rs @@ -204,7 +204,15 @@ fn run_list() { } } +fn check_watch_tty(is_tty: bool) -> Result<(), Error> { + if !is_tty { + anyhow::bail!("--watch requires a terminal, but stdout is not a TTY"); + } + Ok(()) +} + fn run_watch(opt: &mut Opt, config: &Config, interval: u64) -> Result<(), Error> { + check_watch_tty(Term::stdout().is_term())?; Watcher::start(opt, config, interval) } @@ -310,6 +318,12 @@ mod tests { assert!(ret.is_ok()); } + #[test] + fn test_watch_requires_tty() { + assert!(check_watch_tty(false).is_err()); + assert!(check_watch_tty(true).is_ok()); + } + #[test] fn test_run_gen_config() { let ret = run_gen_config();