Only list files in the current directory unless --all is passed
This commit is contained in:
parent
0e83c15906
commit
5579e5b951
1 changed files with 14 additions and 2 deletions
|
@ -1,4 +1,5 @@
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::env;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
|
@ -12,17 +13,28 @@ pub struct ListOptions {
|
||||||
/// By default, this is your home directory's trash ($XDG_DATA_HOME/Trash)
|
/// By default, this is your home directory's trash ($XDG_DATA_HOME/Trash)
|
||||||
#[structopt(long = "trash-dir", parse(from_os_str))]
|
#[structopt(long = "trash-dir", parse(from_os_str))]
|
||||||
trash_dir: Option<PathBuf>,
|
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
|
/// List the contents of a trash directory
|
||||||
pub fn list(options: ListOptions) -> Result<()> {
|
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
|
let mut files = trash_dir
|
||||||
.iter()
|
.iter()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.filter_map(|entry| match entry {
|
.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) => {
|
Err(err) => {
|
||||||
eprintln!("failed to get file info: {:?}", err);
|
eprintln!("failed to get file info: {:?}", err);
|
||||||
None
|
None
|
||||||
|
|
Loading…
Reference in a new issue