From 38ffcaad7ba38305bc2f3e90b6dc1f0d06ecd413 Mon Sep 17 00:00:00 2001 From: Wind Date: Wed, 25 Dec 2024 21:40:02 +0800 Subject: [PATCH] make du streaming (#14665) # Description Following up for issue comment: https://github.com/nushell/nushell/pull/14407#issuecomment-2532343036 > it looks like it just hangs when it's actually counting things I noticed that `du` command collects output internally, so it doesn't streaming. This pr is trying to make it streaming # User-Facing Changes NaN # Tests + Formatting NaN --- crates/nu-command/src/filesystem/du.rs | 29 +++++++++++++------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/crates/nu-command/src/filesystem/du.rs b/crates/nu-command/src/filesystem/du.rs index f90af2f8ad..d0a2d3d5f3 100644 --- a/crates/nu-command/src/filesystem/du.rs +++ b/crates/nu-command/src/filesystem/du.rs @@ -165,6 +165,7 @@ fn du_for_one_pattern( span: Span, signals: &Signals, ) -> Result + Send, ShellError> { + let signals_clone = signals.clone(); let exclude = args.exclude.map_or(Ok(None), move |x| { Pattern::new(x.item.as_ref()) .map(Some) @@ -174,7 +175,7 @@ fn du_for_one_pattern( }) })?; - let mut paths = match args.path { + let paths = match args.path { Some(p) => nu_engine::glob_from(&p, current_dir, span, None), // The * pattern should never fail. None => nu_engine::glob_from( @@ -202,22 +203,22 @@ fn du_for_one_pattern( long, }; - let mut output: Vec = vec![]; - for p in paths.by_ref() { - match p { - Ok(a) => { - if a.is_dir() { - output.push(DirInfo::new(a, ¶ms, max_depth, span, signals)?.into()); - } else if let Ok(v) = FileInfo::new(a, deref, span, params.long) { - output.push(v.into()); + Ok(paths.filter_map(move |p| match p { + Ok(a) => { + if a.is_dir() { + match DirInfo::new(a, ¶ms, max_depth, span, &signals_clone) { + Ok(v) => Some(Value::from(v)), + Err(_) => None, + } + } else { + match FileInfo::new(a, deref, span, params.long) { + Ok(v) => Some(Value::from(v)), + Err(_) => None, } } - Err(e) => { - output.push(Value::error(e, span)); - } } - } - Ok(output.into_iter()) + Err(e) => Some(Value::error(e, span)), + })) } #[cfg(test)]