Skip to content

Commit

Permalink
Added the details and json features.
Browse files Browse the repository at this point in the history
and all I needed were three more dependencies :d

I believe it's ready to be released as v1 now!
  • Loading branch information
onzecki committed Sep 30, 2023
1 parent 589f66e commit ac5bd80
Show file tree
Hide file tree
Showing 3 changed files with 270 additions and 7 deletions.
230 changes: 230 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chrono = "0.4.31"
clap = { version = "4.4.6", features = ["derive"] }
regex = "1.9.5"
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
walkdir = "2.4.0"
44 changes: 37 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use walkdir::{DirEntry, WalkDir};
use regex::Regex;
use clap::Parser;
use std::fs;
use std::time::SystemTime;
use chrono::{DateTime, Utc};
use serde::Serialize;

#[derive(Parser, Debug)]
struct Args {
Expand All @@ -23,7 +27,10 @@ struct Args {
hidden: bool
}


#[derive(Serialize)]
struct PathInfo {
path: String,
}

fn is_hidden(entry: &DirEntry) -> bool {
entry.file_name()
Expand All @@ -39,29 +46,52 @@ fn matches_requirements(entry: &DirEntry, pattern: &Regex) -> bool {
.unwrap_or(false)
}

fn format_system_time(time: SystemTime) -> String {
let datetime: DateTime<Utc> = time.into();
datetime.to_rfc2822()
}


fn main() {
let args = Args::parse();
// Parse the cli arguments
let re = Regex::new(&args.pattern).unwrap();
// Set "re" as the Regex pattern
let mut json_paths = Vec::new();
for entry_result in WalkDir::new(&args.path)
.into_iter()
.filter_entry(|e| !is_hidden(e) || args.hidden) // Include hidden entries if args.hidden is true
{

let entry = match entry_result{

Ok(entry) => entry,
Err(_) => {
println!("Error reading.");
println!("Error reading, do you have the rights to access this?");
continue;
// For each file in a directory/directories check whether an entry is valid and unwrap the result
},
};

if matches_requirements(&entry, &re){
println!("{}", entry.path().display());
// Check whether entry matches the Regex
if matches_requirements(&entry, &re){ // Check whether entry matches the Regex
if !args.json { // Might hinder the performance a tiny bit, but I don't care
println!("\n{}", entry.path().display()); // Show the entries full path
if args.detail { // If we want to show details, then show the details ^^
if let Ok(metadata) = fs::metadata(entry.path()) {
println!("Name: {}", entry.file_name().to_str().unwrap());
println!("Size: {} bytes", metadata.len());
println!("Modified: {:?}", format_system_time(metadata.modified().unwrap()));
println!("Accessed: {:?}", format_system_time(metadata.accessed().unwrap()));
println!("Created: {:?}", format_system_time(metadata.created().unwrap()));
}
}
}
else {
json_paths.push(entry.path().to_string_lossy().to_string());
// Push the path of every single result to the json thingy
}
}
}
if args.json {
println!("{}", serde_json::to_string(&json_paths).unwrap())
// Output the json string
}
}

0 comments on commit ac5bd80

Please sign in to comment.