diff options
author | Sebastian Thiel <sebastian.thiel@icloud.com> | 2021-05-29 13:13:49 +0800 |
---|---|---|
committer | Sebastian Thiel <sebastian.thiel@icloud.com> | 2021-05-29 13:13:49 +0800 |
commit | a0d628898226e272e9f29137da148991e07f3641 (patch) | |
tree | 997cb3a5bab819799bdced29bf108c4843ded42a | |
parent | e68481f3524d214b76d2895a10febc3a524c3256 (diff) |
Only display progress on if stderr is a tty
-rw-r--r-- | src/aggregate.rs | 32 | ||||
-rw-r--r-- | src/main.rs | 19 |
2 files changed, 29 insertions, 22 deletions
diff --git a/src/aggregate.rs b/src/aggregate.rs index 8087060..5ff530b 100644 --- a/src/aggregate.rs +++ b/src/aggregate.rs @@ -19,7 +19,7 @@ use std::{ /// If `sort_by_size_in_bytes` is set, we will sort all sizes (ascending) before outputting them. pub fn aggregate( mut out: impl io::Write, - mut err: impl io::Write + Send + 'static, + err: Option<impl io::Write + Send + 'static>, walk_options: WalkOptions, compute_total: bool, sort_by_size_in_bytes: bool, @@ -36,19 +36,23 @@ pub fn aggregate( let mut inodes = InodeFilter::default(); let paths: Vec<_> = paths.into_iter().collect(); let shared_count = Arc::new(AtomicU64::new(0)); - thread::spawn({ - let shared_count = Arc::clone(&shared_count); - thread::sleep(Duration::from_secs(1)); - move || loop { - thread::sleep(Duration::from_millis(100)); - write!( - err, - "Enumerating {} entries\r", - shared_count.load(Ordering::Relaxed) - ) - .ok(); - } - }); + + if let Some(mut err) = err { + thread::spawn({ + let shared_count = Arc::clone(&shared_count); + thread::sleep(Duration::from_secs(1)); + move || loop { + thread::sleep(Duration::from_millis(100)); + write!( + err, + "Enumerating {} entries\r", + shared_count.load(Ordering::Relaxed) + ) + .ok(); + } + }); + } + for path in paths.into_iter() { num_roots += 1; let mut num_bytes = 0u128; diff --git a/src/main.rs b/src/main.rs index f8cdabc..aa9d82e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,18 +2,21 @@ use anyhow::Result; use clap::Clap; use dua::{ByteFormat, TraversalSorting}; -use std::{ - fs, io, - io::{stderr, Write}, - path::PathBuf, - process, -}; +use std::{fs, io, io::Write, path::PathBuf, process}; mod crossdev; #[cfg(any(feature = "tui-unix", feature = "tui-crossplatform"))] mod interactive; mod options; +fn stderr_if_tty() -> Option<io::Stderr> { + if atty::is(atty::Stream::Stderr) { + Some(io::stderr()) + } else { + None + } +} + fn main() -> Result<()> { use options::Command::*; @@ -72,7 +75,7 @@ fn main() -> Result<()> { let stdout_locked = stdout.lock(); let (res, stats) = dua::aggregate( stdout_locked, - stderr(), + stderr_if_tty(), walk_options, !no_total, !no_sort, @@ -88,7 +91,7 @@ fn main() -> Result<()> { let stdout_locked = stdout.lock(); dua::aggregate( stdout_locked, - stderr(), + stderr_if_tty(), walk_options, true, true, |