Skip to content

Commit 176a0ec

Browse files
committed
added the args to list the content in the trash
1 parent f4c8824 commit 176a0ec

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

Cargo.lock

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ readme = "README.md"
1313
[dependencies]
1414
clap = { version = "4.5.45", features = ["derive"] }
1515
dirs = "6.0.0"
16+
walkdir = "2.5.0"

src/args.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ pub struct Args {
1212
/// Path of the file
1313
pub file: Vec<PathBuf>,
1414

15+
/// List files in the trash directory
16+
#[arg(long, default_value = "false")]
17+
pub list: bool,
18+
1519
/// Clean up the trash directory
1620
#[arg(long, default_value = "false")]
1721
pub tidy: bool,

src/main.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use args::Args;
33
use clap::Parser;
44
use dirs::home_dir;
55
use std::fs::{self, rename};
6+
use walkdir::WalkDir;
67

78
pub fn get_trash_directory() -> std::path::PathBuf {
89
match home_dir() {
@@ -57,6 +58,24 @@ pub fn move_to_trash(path: &std::path::Path) -> Result<(), std::io::Error> {
5758
rename(path, new_path)
5859
}
5960

61+
pub fn list_trash() {
62+
let trash = get_trash_directory();
63+
match WalkDir::new(&trash)
64+
.into_iter()
65+
.collect::<Result<Vec<_>, _>>()
66+
{
67+
Ok(entries) => {
68+
for entry in entries {
69+
match entry.path().to_str() {
70+
Some(path) => println!("{path}"),
71+
None => eprintln!("Error: Unable to convert path to string"),
72+
}
73+
}
74+
}
75+
Err(e) => eprintln!("Error: Failed to read trash directory - {e}"),
76+
}
77+
}
78+
6079
fn main() {
6180
// parsing the args
6281
let args = Args::parse();
@@ -66,9 +85,16 @@ fn main() {
6685
let force = args.force;
6786
let tidy = args.tidy;
6887
let dir = args.dir;
88+
let list = args.list;
6989

7090
let trash = get_trash_directory();
7191

92+
// listing the trash directory if the flag is set
93+
if list {
94+
list_trash();
95+
return;
96+
}
97+
7298
// tidying the trash directory if the flag is set
7399
if tidy {
74100
tidy_trash_directory();

0 commit comments

Comments
 (0)