From 5579e5b9519421c935fb304bc22b9d6c2d38718c Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Mon, 2 Nov 2020 20:24:50 -0600 Subject: [PATCH] Only list files in the current directory unless --all is passed --- src/ops/list.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/ops/list.rs b/src/ops/list.rs index b04ab6b..40a4bf5 100644 --- a/src/ops/list.rs +++ b/src/ops/list.rs @@ -1,4 +1,5 @@ use std::path::PathBuf; +use std::env; use anyhow::Result; @@ -12,17 +13,28 @@ pub struct ListOptions { /// By default, this is your home directory's trash ($XDG_DATA_HOME/Trash) #[structopt(long = "trash-dir", parse(from_os_str))] trash_dir: Option, + + /// List all files in the trash (by default, only files in the current + /// directory are listed) + #[structopt(short = "a", long = "all")] + all: bool, } /// List the contents of a trash directory pub fn list(options: ListOptions) -> Result<()> { - let trash_dir = TrashDir::from_opt(options.trash_dir); + let trash_dir = TrashDir::from_opt(options.trash_dir.as_ref()); + let current_dir = env::current_dir()?; let mut files = trash_dir .iter() .unwrap() .filter_map(|entry| match entry { - Ok(info) => Some(info), + Ok(info) => { + if !options.all && !info.path.starts_with(¤t_dir) { + return None; + } + Some(info) + } Err(err) => { eprintln!("failed to get file info: {:?}", err); None