diff --git a/src/lib.rs b/src/lib.rs index 96f6e4b..2890f61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,6 @@ extern crate lazy_static; #[macro_use] extern crate structopt; -extern crate log; #[macro_use] extern crate anyhow; #[macro_use] diff --git a/src/ops/restore.rs b/src/ops/restore.rs index 5b7e873..bd6977a 100644 --- a/src/ops/restore.rs +++ b/src/ops/restore.rs @@ -1,8 +1,10 @@ +use std::env; use std::fs; use std::io; use std::path::PathBuf; use anyhow::Result; +use chrono_humanize::HumanTime; use crate::utils; use crate::TrashDir; @@ -14,11 +16,16 @@ pub struct RestoreOptions { /// 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, } /// Restore files from a trash directory pub fn restore(options: RestoreOptions) -> Result<()> { - let trash_dir = TrashDir::from_opt(options.trash_dir); + let trash_dir = TrashDir::from_opt(options.trash_dir.as_ref()); if trash_dir.check_info_dir()?.is_none() { bail!("There's no trash directory here."); @@ -26,18 +33,24 @@ pub fn restore(options: RestoreOptions) -> Result<()> { // get list of files sorted by deletion date // TODO: possible to get this to be streaming? + let current_dir = env::current_dir()?; let files = { let mut files = trash_dir - .iter() - .unwrap() + .iter()? .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 } }) .collect::>(); + files.sort_unstable_by_key(|info| info.deletion_date); files }; @@ -48,9 +61,10 @@ pub fn restore(options: RestoreOptions) -> Result<()> { for (i, info) in files.iter().enumerate() { println!( - "[{}]\t{}\t{}", + "[{}]\t{}\t{}\t{}", i, info.deletion_date, + HumanTime::from(info.deletion_date), utils::percent_encode(&info.path) ); }