garbage/src/ops/list.rs

43 lines
1 KiB
Rust
Raw Normal View History

2020-03-04 06:13:46 +00:00
use std::path::PathBuf;
use anyhow::Result;
use crate::utils;
2020-06-06 04:32:34 +00:00
use crate::TrashDir;
2020-03-04 04:48:06 +00:00
2020-03-04 06:18:18 +00:00
/// Options to pass to list
2020-03-04 06:13:46 +00:00
#[derive(StructOpt)]
pub struct ListOptions {
/// The path to the trash directory to list.
/// 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>,
}
2020-03-04 06:18:18 +00:00
/// List the contents of a trash directory
2020-03-04 06:13:46 +00:00
pub fn list(options: ListOptions) -> Result<()> {
let trash_dir = TrashDir::from_opt(options.trash_dir);
let mut files = trash_dir
2020-03-04 04:48:06 +00:00
.iter()
.unwrap()
.filter_map(|entry| match entry {
Ok(info) => Some(info),
Err(err) => {
eprintln!("failed to get file info: {:?}", err);
None
}
})
.collect::<Vec<_>>();
files.sort_unstable_by_key(|info| info.deletion_date);
for info in files {
2020-06-06 04:32:34 +00:00
println!(
"{}\t{}",
info.deletion_date,
utils::percent_encode(info.path)
);
2020-03-04 04:48:06 +00:00
}
2020-03-04 06:13:46 +00:00
Ok(())
2020-03-04 04:48:06 +00:00
}