Only list files in the current directory unless --all is passed

This commit is contained in:
Michael Zhang 2020-11-02 20:24:50 -06:00
parent 0e83c15906
commit 5579e5b951
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B

View file

@ -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<PathBuf>,
/// 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(&current_dir) {
return None;
}
Some(info)
}
Err(err) => {
eprintln!("failed to get file info: {:?}", err);
None