~iptq/garbage

ref: 02ba29053be29149ec4ca4f73cd375f02f2cf21b garbage/src/ops/list.rs -rw-r--r-- 1.0 KiB
02ba2905Michael Zhang fix panic problem by not converting to utf-8 all the time 1 year, 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use std::path::PathBuf;

use anyhow::Result;

use crate::TrashDir;
use crate::utils;

/// Options to pass to list
#[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>,
}

/// List the contents of a trash directory
pub fn list(options: ListOptions) -> Result<()> {
    let trash_dir = TrashDir::from_opt(options.trash_dir);

    let mut files = trash_dir
        .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 {
        println!("{}\t{}", info.deletion_date, utils::percent_encode(info.path));
    }

    Ok(())
}