Skip to content

Commit e560bd2

Browse files
authored
Merge pull request #12 from santoshxshrestha/lists
refactor: removed the redundant code block to display list
2 parents c69c9c1 + ec585da commit e560bd2

3 files changed

Lines changed: 52 additions & 37 deletions

File tree

CHANGELOG.md

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

88
## [Unreleased]
99

10+
## [0.1.15] -
11+
12+
### Changed
13+
14+
- Refactored the logic for lising the trash items and removed the redundant block for lising the items in table
15+
1016
## [0.1.14] - 2025-10-06
1117

1218
### Changed
1319

1420
- Update dependencies to latest versions
1521

16-
### Fixed
22+
### Fixed
23+
1724
- Fixed the linting issues in the codebase
1825

1926
## [0.1.13] - 2025-09-18

flake.nix

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,32 @@
66
naersk.url = "github:nix-community/naersk";
77
};
88

9-
outputs = { nixpkgs, naersk, ... }:
9+
outputs =
10+
{ nixpkgs, naersk, ... }:
1011
let
1112
system = "x86_64-linux";
1213
pkgs = nixpkgs.legacyPackages.${system};
1314
naerskLib = pkgs.callPackage naersk { };
14-
in {
15+
in
16+
{
1517
packages.${system}.default = naerskLib.buildPackage {
1618
src = ./.;
1719
nativeBuildInputs = [ pkgs.pkg-config ];
1820
};
1921
devShells.${system}.default = pkgs.mkShell {
20-
buildInputs = with pkgs; [ rustc cargo clippy rustfmt rust-analyzer ];
22+
buildInputs = with pkgs; [
23+
rustc
24+
cargo
25+
clippy
26+
rustfmt
27+
rust-analyzer
28+
];
2129
nativeBuildInputs = [ pkgs.pkg-config ];
2230

2331
# env.RUST_SRC_PATH =
2432
# "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
2533
};
2634
formatter = pkgs.rustfmt;
35+
DIRENV_LOG_FORMAT = "";
2736
};
2837
}

src/main.rs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl List {
2929
}
3030
}
3131

32-
fn list_specific_trash(seconds: i64) -> Result<(), trash::Error> {
32+
fn list_specific_trash(seconds: i64) -> Result<Vec<List>, trash::Error> {
3333
let mut list: Vec<List> = vec![];
3434
let entries = os_limited::list()?;
3535
let now = Local::now().timestamp();
@@ -48,39 +48,33 @@ fn list_specific_trash(seconds: i64) -> Result<(), trash::Error> {
4848
));
4949
}
5050
}
51-
let mut table = Table::new(&list);
52-
table.with(Style::rounded());
53-
table.modify(Columns::first(), Alignment::right());
54-
println!("{table}");
55-
Ok(())
51+
Ok(list)
5652
}
5753

58-
fn list_trash() {
54+
fn list_trash() -> Result<Vec<List>, trash::Error> {
5955
let mut list: Vec<List> = vec![];
60-
match os_limited::list() {
61-
Ok(trash) => {
62-
for entry in trash {
63-
let time_deleted = Local
64-
.timestamp_opt(entry.time_deleted, 0)
65-
.single()
66-
.map(|dt| dt.format("%Y-%m-%d %H:%M:%S").to_string())
67-
.unwrap_or_else(|| "Unknown time".to_string());
68-
69-
list.push(List::new(
70-
entry.name.to_string_lossy().to_string(),
71-
entry.original_path().to_string_lossy().to_string(),
72-
time_deleted,
73-
))
74-
}
75-
let mut table = Table::new(&list);
76-
table.with(Style::rounded());
77-
table.modify(Columns::first(), Alignment::right());
78-
println!("{table}");
79-
}
80-
Err(e) => {
81-
eprintln!("{}", format!("Failed to list trash entries: {e}").red())
82-
}
56+
let trash = os_limited::list()?;
57+
for entry in trash {
58+
let time_deleted = Local
59+
.timestamp_opt(entry.time_deleted, 0)
60+
.single()
61+
.map(|dt| dt.format("%Y-%m-%d %H:%M:%S").to_string())
62+
.unwrap_or_else(|| "Unknown time".to_string());
63+
64+
list.push(List::new(
65+
entry.name.to_string_lossy().to_string(),
66+
entry.original_path().to_string_lossy().to_string(),
67+
time_deleted,
68+
))
8369
}
70+
Ok(list)
71+
}
72+
73+
fn display_table(list: Vec<List>) {
74+
let mut table = Table::new(&list);
75+
table.with(Style::rounded());
76+
table.modify(Columns::first(), Alignment::right());
77+
println!("{table}");
8478
}
8579

8680
fn tidy_trash(days: i64) -> Result<(), trash::Error> {
@@ -244,11 +238,16 @@ fn main() {
244238
if args.is_list() {
245239
let seconds = args.get_time_list() * 86400;
246240
if seconds == 0 {
247-
list_trash();
241+
match list_trash() {
242+
Ok(list) => display_table(list),
243+
Err(e) => eprintln!("{}", format!("Failed to list trash entries: {e}").red()),
244+
}
245+
248246
return;
249247
} else {
250-
if let Err(e) = list_specific_trash(seconds) {
251-
eprintln!("{}", format!("Failed to list trash entries: {e}").red());
248+
match list_specific_trash(seconds) {
249+
Ok(list) => display_table(list),
250+
Err(e) => eprintln!("{}", format!("Failed to list trash entries: {e}").red()),
252251
}
253252
return;
254253
}

0 commit comments

Comments
 (0)