Skip to content

Commit

Permalink
Refactor filters to share resources between other objects
Browse files Browse the repository at this point in the history
  • Loading branch information
isbm committed Nov 2, 2023
1 parent f78d245 commit a4ecf1c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/filters/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub const DOC_FP_EXT: &[&str] = &[".eps", ".pdf", ".ps"];
pub const DOC_LOCATIONS: &[&str] = &["/usr/share/doc"];

/// Headers
pub const H_SRC_F_EXT: &[&str] = &[".h", ".hpp"];
pub const SRC_FH_EXT: &[&str] = &[".h", ".hpp"];

/// Archives
pub const ARC_F_EXT: &[&str] = &[".gz", ".bz2", ".xz", ".zip", ".tar"];
Expand Down
24 changes: 24 additions & 0 deletions src/filters/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ impl ResourcesDataFilter {

false
}

/// Detects if a file is still a potential junk (but unsure)
pub fn is_potential_junk(fname: &str) -> bool {
for ext in
defs::DOC_F_EXT.iter().chain(defs::ARC_F_EXT.iter()).chain(defs::SRC_FH_EXT.iter()).chain(defs::DOC_FP_EXT.iter())
{
if fname.ends_with(ext) {
return true;
}
}

for sf in defs::DOC_STUB_FILES {
if fname == *sf {
return true;
}
}

// Potentially doc stubfile that doesn't look like a known one
if fname == fname.to_uppercase() {
return true;
}

false
}
}

impl DataFilter for ResourcesDataFilter {
Expand Down
4 changes: 2 additions & 2 deletions src/filters/texts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl TextDataFilter {

let p = p.to_str().unwrap();

for c in ["/doc/"].iter().chain(defs::DOC_STUB_FILES.iter()) {
for c in ["/doc/"].iter().chain(defs::DOC_STUB_FILES) {
if p.to_lowercase().contains(c.to_lowercase().as_str()) {
return true;
}
Expand All @@ -77,7 +77,7 @@ impl TextDataFilter {
}
}

for c in defs::DOC_F_EXT {
for c in defs::DOC_F_EXT.iter().chain(defs::DOC_FP_EXT).chain(defs::SRC_FH_EXT) {
if p.ends_with(c) {
return true;
}
Expand Down
48 changes: 23 additions & 25 deletions src/scanner/dlst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
Data lister (fancy STDOUT printer)
*/

use crate::filters::defs::{self};
use crate::filters::{
defs::{self},
resources,
};
use bytesize::ByteSize;
use colored::Colorize;
use std::{
Expand All @@ -25,6 +28,9 @@ impl<'a> ContentFormatter<'a> {
pub(crate) fn format(&mut self) {
let d_len = self.fs_data.len() - 1;
let mut t_size: u64 = 0;
let mut j_size: u64 = 0; // size of junk
let mut j_total: u64 = 0; // total junk files

for (pi, p) in self.fs_data.iter().enumerate() {
t_size += p.metadata().unwrap().len();
let (dname, mut fname) = self.dn(p);
Expand Down Expand Up @@ -53,38 +59,30 @@ impl<'a> ContentFormatter<'a> {
} else {
if fname.ends_with(".so") || fname.contains(".so.") {
fname = fname.green().to_string();
} else if self.is_potential_junk(&fname) {
} else if resources::ResourcesDataFilter::is_potential_junk(&fname) {
j_total += 1;
j_size += p.metadata().unwrap().len();
fname = format!("{} {}", "⚠️".bright_red().bold(), fname.bright_red());
}

println!("{} {}", leaf.blue(), fname);
}
}

println!("\nPreserved {} files, taking space: {}\n", d_len + 1, ByteSize::b(t_size));
}

fn is_potential_junk(&self, fname: &str) -> bool {
for ext in
defs::DOC_F_EXT.iter().chain(defs::ARC_F_EXT.iter()).chain(defs::H_SRC_F_EXT.iter()).chain(defs::DOC_FP_EXT.iter())
{
if fname.ends_with(ext) {
return true;
}
// Print the summary
println!(
"\nPreserved {} files, taking {} of a disk space",
(d_len + 1).to_string().bright_green(),
ByteSize::b(t_size).to_string().bright_yellow()
);
if j_total > 0 {
println!(
"Potentially {} junk files, taking {} of a disk space",
j_total.to_string().bright_red(),
ByteSize::b(j_size).to_string().bright_yellow()
);
}

for sf in defs::DOC_STUB_FILES {
if fname == *sf {
return true;
}
}

// Potentially doc stubfile that doesn't look like a known one
if fname == fname.to_uppercase() {
return true;
}

false
println!("");
}

/// Get dir/name split, painted accordingly
Expand Down

0 comments on commit a4ecf1c

Please sign in to comment.