Skip to content

Commit

Permalink
Detect potential junk, add more coloring
Browse files Browse the repository at this point in the history
  • Loading branch information
isbm committed Nov 1, 2023
1 parent 76e5dd0 commit 9114691
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/scanner/dlst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use std::{
use bytesize::ByteSize;
use colored::Colorize;

use crate::filters::defs::{self};

/// ContentFormatter is a lister for finally gathered information,
/// that needs to be displayed on the screen for the user for review
pub struct ContentFormatter<'a> {
Expand All @@ -27,7 +29,7 @@ impl<'a> ContentFormatter<'a> {
let mut t_size: u64 = 0;
for (pi, p) in self.fs_data.iter().enumerate() {
t_size += p.metadata().unwrap().len();
let (dname, fname) = self.dn(p);
let (dname, mut fname) = self.dn(p);

if self.last_dir != dname {
self.last_dir = dname.to_owned();
Expand All @@ -36,12 +38,8 @@ impl<'a> ContentFormatter<'a> {
}

let mut leaf = " ├─";
if pi == d_len {
if pi == d_len || (pi < d_len && dname != self.fs_data[pi + 1].parent().unwrap().to_str().unwrap()) {
leaf = " ╰─";
} else if pi < d_len {
if dname != self.fs_data[pi + 1].parent().unwrap().to_str().unwrap().to_string() {
leaf = " ╰─";
}
}

if p.is_symlink() {
Expand All @@ -55,13 +53,42 @@ impl<'a> ContentFormatter<'a> {
} else if p.metadata().unwrap().permissions().mode() & 0o111 != 0 {
println!("{} {}", leaf.blue(), fname.bright_green().bold());
} else {
if fname.ends_with(".so") || fname.contains(".so.") {
fname = fname.green().to_string();
} else if self.is_potential_junk(&fname) {
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;
}
}

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
}

/// Get dir/name split, painted accordingly
fn dn(&mut self, p: &Path) -> (String, String) {
let dname = p.parent().unwrap().to_str().unwrap().to_string();
Expand Down

0 comments on commit 9114691

Please sign in to comment.