From 35dc7438a57b5e9ced345b50a3dcfd79cd3d2aed Mon Sep 17 00:00:00 2001 From: Jason Gedge Date: Sun, 29 Mar 2020 12:30:36 -0400 Subject: [PATCH] Make use of interruptible stream in various places --- crates/nu-cli/src/commands/du.rs | 51 +++++++++------------ crates/nu-cli/src/shell/filesystem_shell.rs | 7 +-- 2 files changed, 23 insertions(+), 35 deletions(-) diff --git a/crates/nu-cli/src/commands/du.rs b/crates/nu-cli/src/commands/du.rs index 2b1c39e4fc..7648f135e6 100644 --- a/crates/nu-cli/src/commands/du.rs +++ b/crates/nu-cli/src/commands/du.rs @@ -9,7 +9,6 @@ use nu_errors::ShellError; use nu_protocol::{CallInfo, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; use std::path::PathBuf; -use std::sync::atomic::Ordering; const NAME: &str = "du"; const GLOB_PARAMS: MatchOptions = MatchOptions { @@ -142,35 +141,29 @@ fn du(args: DuArgs, ctx: &RunnablePerItemContext) -> Result { - if p.is_dir() { - yield Ok(ReturnSuccess::Value( - DirInfo::new(p, ¶ms, max_depth).into(), - )); - } else { - match FileInfo::new(p, deref, tag.clone()) { - Ok(f) => yield Ok(ReturnSuccess::Value(f.into())), - Err(e) => yield Err(e) - } - } - } - Err(e) => yield Err(e), - } - } + let params = DirBuilder { + tag: tag.clone(), + min: min_size, + deref, + ex: exclude, + all, }; + + let stream = futures::stream::iter(paths) + .interruptible(ctrl_c) + .map(move |path| match path { + Ok(p) => { + if p.is_dir() { + Ok(ReturnSuccess::Value( + DirInfo::new(p, ¶ms, max_depth).into(), + )) + } else { + FileInfo::new(p, deref, tag.clone()).map(|v| ReturnSuccess::Value(v.into())) + } + } + Err(e) => Err(e), + }); + Ok(stream.to_output_stream()) } diff --git a/crates/nu-cli/src/shell/filesystem_shell.rs b/crates/nu-cli/src/shell/filesystem_shell.rs index 6b10642fb4..1caf164318 100644 --- a/crates/nu-cli/src/shell/filesystem_shell.rs +++ b/crates/nu-cli/src/shell/filesystem_shell.rs @@ -16,7 +16,6 @@ use rustyline::completion::FilenameCompleter; use rustyline::hint::{Hinter, HistoryHinter}; use std::collections::HashMap; use std::path::{Component, Path, PathBuf}; -use std::sync::atomic::Ordering; use trash as SendToTrash; #[cfg(unix)] @@ -163,10 +162,6 @@ impl Shell for FilesystemShell { // Generated stream: impl Stream let stream = async_stream::try_stream! { for path in paths { - if ctrl_c.load(Ordering::SeqCst) { - break; - } - let path = path.map_err(|e| ShellError::from(e.into_error()))?; if !all && is_hidden_dir(&path) { @@ -196,7 +191,7 @@ impl Shell for FilesystemShell { } }; - Ok(stream.to_output_stream()) + Ok(stream.interruptible(ctrl_c).to_output_stream()) } fn cd(&self, args: EvaluatedWholeStreamCommandArgs) -> Result {