From 357a59010824c1f4e32c899bf48c1ea9e438dcef Mon Sep 17 00:00:00 2001 From: Bo Maryniuk Date: Tue, 7 Nov 2023 16:06:04 +0100 Subject: [PATCH] Move dry-run result to the formatter --- src/procdata.rs | 21 +-------------------- src/scanner/dlst.rs | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/procdata.rs b/src/procdata.rs index 1d60953..a977f69 100644 --- a/src/procdata.rs +++ b/src/procdata.rs @@ -4,8 +4,6 @@ use crate::{ rootfs, scanner::{binlib::ElfScanner, debpkg::DebPackageScanner, dlst::ContentFormatter, general::Scanner}, }; -use bytesize::ByteSize; -use filesize::PathExt; use std::fs::{self, canonicalize, remove_file, DirEntry, File}; use std::{ collections::HashSet, @@ -127,22 +125,6 @@ impl TintProcessor { Ok(()) } - /// Perform only a dry-run - fn dry_run(&self, paths: Vec) -> Result<(), Error> { - let mut total_size: u64 = 0; - let mut total_files: usize = 0; - - for p in paths { - total_size += p.size_on_disk_fast(&p.metadata().unwrap()).unwrap(); - total_files += 1; - log::debug!(" - {}", p.to_str().unwrap()); - } - - println!("\nTotal files to be removed: {}, disk size freed: {}\n", total_files, ByteSize::b(total_size)); - - Ok(()) - } - fn ext_path(p: HashSet, mut np: HashSet) -> HashSet { for tgt in p.iter() { if tgt.is_symlink() { @@ -228,8 +210,7 @@ impl TintProcessor { paths.sort(); if self.dry_run { - self.dry_run(p)?; - ContentFormatter::new(&paths).format(); + ContentFormatter::new(&paths).set_removed(&p).format(); } else { self.apply_changes(p)?; } diff --git a/src/scanner/dlst.rs b/src/scanner/dlst.rs index 609f0f1..b3602ec 100644 --- a/src/scanner/dlst.rs +++ b/src/scanner/dlst.rs @@ -5,6 +5,7 @@ Data lister (fancy STDOUT printer) use crate::filters::resources; use bytesize::ByteSize; use colored::Colorize; +use filesize::PathExt; use std::{ os::unix::prelude::PermissionsExt, path::{Path, PathBuf}, @@ -15,11 +16,33 @@ use std::{ pub struct ContentFormatter<'a> { fs_data: &'a Vec, last_dir: String, + fs_removed: Option<&'a Vec>, } impl<'a> ContentFormatter<'a> { pub(crate) fn new(fs_data: &'a Vec) -> Self { - ContentFormatter { fs_data, last_dir: "".to_string() } + Self { fs_data, last_dir: "".to_string(), fs_removed: None } + } + + /// Set removed data + pub(crate) fn set_removed(&mut self, r: &'a Vec) -> &mut Self { + self.fs_removed = Some(r); + self + } + + /// Perform only a dry-run + fn format_removed(&self) -> (u64, u64) { + let mut total_size: u64 = 0; + let mut total_files: u64 = 0; + + if let Some(fsr) = self.fs_removed { + for p in fsr { + total_size += p.size_on_disk_fast(&p.metadata().unwrap()).unwrap(); + total_files += 1; + log::debug!(" - {}", p.to_str().unwrap()); + } + } + (total_files, total_size) } #[allow(clippy::println_empty_string)] @@ -30,6 +53,7 @@ impl<'a> ContentFormatter<'a> { let mut j_total: u64 = 0; // total junk files let mut d_total: u64 = 0; let mut d_size: u64 = 0; + let (t_r_files, t_r_size) = self.format_removed(); for (pi, p) in self.fs_data.iter().enumerate() { let mut t_leaf: String = "".to_string(); @@ -86,7 +110,12 @@ impl<'a> ContentFormatter<'a> { // Print the summary println!( - "\nPreserved {} files, taking {} of a disk space", + "\nRemoved {} files, releasing {} of a disk space", + t_r_files.to_string().bright_green(), + ByteSize::b(t_r_size).to_string().bright_yellow() + ); + println!( + "Preserved {} files, taking {} of a disk space", (d_len + 1).to_string().bright_green(), ByteSize::b(t_size).to_string().bright_yellow() );