Skip to content

Commit d83b7e4

Browse files
authored
Merge pull request #5 from santoshxshrestha/multiple_files
feat: multiple files as args in purge/recover refactor: removed some old fn
2 parents 0c98637 + 7b264ea commit d83b7e4

File tree

5 files changed

+54
-49
lines changed

5 files changed

+54
-49
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
## [0.1.7] -
10+
## [0.1.7] - 2025-08-31
1111

1212
### Added
1313

@@ -19,9 +19,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
### Changed
2020

2121
- update README with new changes
22+
- improved code structure and organization
23+
- `recover` command will now can take multiple file names as args
24+
- `purge` command will now can take multiple file names as args
2225

2326
### Fixed
2427

28+
- minor bug fixes and improvements
29+
2530
## [0.1.6] - 2025-08-30
2631

2732
### Added

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rmxt"
3-
version = "0.1.6"
3+
version = "0.1.7"
44
edition = "2024"
55
authors = ["Santosh Shrestha <santoshxshrestha@gmail.com> "]
66
description = "A replacement for the 'rm' command with a trash feature for safer file deletion."

src/args.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ pub enum Commands {
5959
Recover {
6060
/// Name of the file to recover
6161
#[arg(help = "Name of the file to recover from trash")]
62-
name: String,
62+
name: Vec<String>,
6363
},
6464

6565
/// Purge files from the trash directory
6666
#[command(name = "purge")]
6767
Purge {
6868
/// Purge files from the trash directory
6969
#[arg(help = "Name of the file to purge")]
70-
name: String,
70+
name: Vec<String>,
7171
},
7272
}
7373

@@ -101,10 +101,10 @@ impl Args {
101101
}
102102

103103
/// Get the name to recover (if recover command is active)
104-
pub fn get_recover_name(&self) -> Option<&str> {
104+
pub fn get_recover_name(&self) -> Vec<String> {
105105
match &self.command {
106-
Some(Commands::Recover { name }) => Some(name),
107-
_ => None,
106+
Some(Commands::Recover { name }) => name.clone(),
107+
_ => Vec::new(),
108108
}
109109
}
110110

@@ -114,10 +114,10 @@ impl Args {
114114
}
115115

116116
/// Get the name to purge (if purge command is active)
117-
pub fn get_purge_name(&self) -> Option<&str> {
117+
pub fn get_purge_name(&self) -> Vec<String> {
118118
match &self.command {
119-
Some(Commands::Purge { name }) => Some(name),
120-
_ => None,
119+
Some(Commands::Purge { name }) => name.clone(),
120+
_ => Vec::new(),
121121
}
122122
}
123123

src/main.rs

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,6 @@ use std::{fs, result};
1111
use trash::os_limited::{self, purge_all, restore_all};
1212
use trash::{TrashItem, delete};
1313

14-
pub fn recover_from_trash(name: &str) -> Result<(), Box<dyn std::error::Error>> {
15-
let list = trash::os_limited::list()?;
16-
let items_to_restore: Vec<_> = list.into_iter().filter(|item| item.name == name).collect();
17-
18-
if !items_to_restore.is_empty() {
19-
restore_all(items_to_restore)?;
20-
println!("Recovered '{name}'");
21-
} else {
22-
println!("No items found to recover with the name '{name}'");
23-
}
24-
Ok(())
25-
}
26-
27-
pub fn purge(name: &str) -> Result<(), trash::Error> {
28-
let content_to_remove: Vec<TrashItem> = trash::os_limited::list()
29-
.unwrap()
30-
.into_iter()
31-
.filter(|content| content.name == name)
32-
.collect();
33-
purge_all(content_to_remove)
34-
}
35-
3614
pub fn list_specific_trash(seconds: i64) {
3715
let entries = trash::os_limited::list().unwrap();
3816
let now = Local::now().timestamp();
@@ -77,17 +55,22 @@ pub fn list_trash() {
7755
}
7856
}
7957

80-
pub fn tidy_trash(days: i64) -> Result<(), Box<dyn Error>> {
58+
pub fn tidy_trash(days: i64) {
8159
let seconds: i64 = days * 86400;
82-
let entries = trash::os_limited::list()?;
8360
let now = Local::now().timestamp();
84-
for entry in entries {
85-
if now - entry.time_deleted > seconds {
86-
purge(&entry.name.to_string_lossy())?;
87-
println!("Purged: {}", entry.name.to_string_lossy());
61+
let content_to_purge = trash::os_limited::list()
62+
.unwrap()
63+
.into_iter()
64+
.filter(|item| item.time_deleted < seconds)
65+
.collect::<Vec<TrashItem>>();
66+
67+
if !content_to_purge.is_empty() {
68+
if let Err(e) = trash::os_limited::purge_all(content_to_purge) {
69+
eprintln!("Error purging items: {e}");
70+
} else {
71+
println!("No items found to purge older than {days} days");
8872
}
8973
}
90-
Ok(())
9174
}
9275

9376
fn main() {
@@ -101,10 +84,19 @@ fn main() {
10184
let ignore = args.ignore;
10285

10386
if args.is_purge() {
104-
if let Some(filename) = args.get_purge_name() {
105-
if let Err(e) = purge(filename) {
106-
eprintln!("Error removing the content from the bin: {e}")
87+
let names = args.get_purge_name();
88+
let content_to_purge = trash::os_limited::list()
89+
.unwrap()
90+
.into_iter()
91+
.filter(|item| names.contains(&item.name.to_string_lossy().to_string()))
92+
.collect::<Vec<TrashItem>>();
93+
94+
if !content_to_purge.is_empty() {
95+
if let Err(e) = trash::os_limited::purge_all(content_to_purge) {
96+
eprintln!("Error purging items: {e}");
10797
}
98+
} else {
99+
println!("No items found to purge with such names");
108100
}
109101
}
110102

@@ -133,15 +125,25 @@ fn main() {
133125
return;
134126
} else {
135127
list_specific_trash(seconds);
128+
return;
136129
}
130+
return;
137131
}
138132

139133
// recovering files from trash if the recover command is used
140134
if args.is_recover() {
141-
if let Some(name) = args.get_recover_name() {
142-
if let Err(e) = recover_from_trash(name) {
143-
eprintln!("Error recovering from trash: {e}");
135+
let names = args.get_recover_name();
136+
let content_to_recover = trash::os_limited::list()
137+
.unwrap()
138+
.into_iter()
139+
.filter(|item| names.contains(&item.name.to_string_lossy().to_string()))
140+
.collect::<Vec<TrashItem>>();
141+
if !content_to_recover.is_empty() {
142+
if let Err(e) = trash::os_limited::restore_all(content_to_recover) {
143+
eprintln!("Error recovering items: {e}");
144144
}
145+
} else {
146+
println!("No items found to recover with such names");
145147
}
146148
return;
147149
}
@@ -163,9 +165,7 @@ fn main() {
163165
return;
164166
}
165167

166-
if let Err(e) = tidy_trash(days) {
167-
eprintln!("Error tidying the trash: {e}");
168-
}
168+
tidy_trash(days);
169169
return;
170170
}
171171

0 commit comments

Comments
 (0)