diff --git a/Cargo.lock b/Cargo.lock index 68b0ddb..14712a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -327,6 +327,7 @@ dependencies = [ "regex", "rusqlite", "rustc-hash", + "size", "thiserror", "unicode-width 0.2.0", "yansi", @@ -780,6 +781,12 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "size" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b6709c7b6754dca1311b3c73e79fcce40dd414c782c66d88e8823030093b02b" + [[package]] name = "smallvec" version = "1.15.0" diff --git a/Cargo.toml b/Cargo.toml index 3202ba1..a6c1b79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ ref-cast = "1.0.24" regex = "1.11.1" rusqlite = { version = "0.35.0", features = [ "bundled" ] } rustc-hash = "2.1.1" +size = "0.5.0" thiserror = "2.0.12" unicode-width = "0.2.0" yansi = { version = "1.0.1", features = [ "detect-env", "detect-tty" ] } diff --git a/src/diff.rs b/src/diff.rs index 5227b81..06f8252 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -43,7 +43,7 @@ impl DiffStatus { } } -pub fn diff<'a>( +pub fn write_diffln<'a>( writer: &mut dyn fmt::Write, paths_old: impl Iterator, paths_new: impl Iterator, @@ -107,8 +107,6 @@ pub fn diff<'a>( for (name, versions, status) in diffs { if last_status != Some(status) { - last_status = Some(status); - writeln!( writer, "{nl}{status}", @@ -120,6 +118,8 @@ pub fn diff<'a>( } .bold(), )?; + + last_status = Some(status); } write!( diff --git a/src/lib.rs b/src/lib.rs index 3b83c13..c94991e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,7 +13,7 @@ use anyhow::{ use derive_more::Deref; mod diff; -pub use diff::diff; +pub use diff::write_diffln; pub mod store; diff --git a/src/main.rs b/src/main.rs index 9c75010..11bd87c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,12 +16,9 @@ use anyhow::{ Context as _, Error, Result, + anyhow, }; use clap::Parser as _; -use dix::{ - diff, - store, -}; use yansi::Paint as _; struct WriteFmt(W); @@ -57,15 +54,15 @@ fn real_main() -> Result<()> { // Handle to the thread collecting closure size information. // We do this as early as possible because Nix is slow. - let _closure_size_handle = { + let closure_size_handle = { log::debug!("calculating closure sizes in background"); - let mut connection = store::connect()?; - let old_path = old_path.clone(); let new_path = new_path.clone(); thread::spawn(move || { + let mut connection = dix::store::connect()?; + Ok::<_, Error>(( connection.query_closure_size(&old_path)?, connection.query_closure_size(&new_path)?, @@ -73,7 +70,7 @@ fn real_main() -> Result<()> { }) }; - let mut connection = store::connect()?; + let mut connection = dix::store::connect()?; let paths_old = connection.query_depdendents(&old_path).with_context(|| { @@ -105,64 +102,56 @@ fn real_main() -> Result<()> { let mut out = WriteFmt(io::stdout()); + writeln!( + out, + "{arrows} {old_path}", + arrows = "<<<".bold(), + old_path = old_path.display(), + )?; + writeln!( + out, + "{arrows} {new_path}", + arrows = ">>>".bold(), + new_path = new_path.display(), + )?; + + writeln!(out)?; + #[expect(clippy::pattern_type_mismatch)] - diff( + dix::write_diffln( &mut out, paths_old.iter().map(|(_, path)| path), paths_new.iter().map(|(_, path)| path), )?; - // println!("Difference between the two generations:"); - // println!(); + let (closure_size_old, closure_size_new) = closure_size_handle + .join() + .map_err(|_| anyhow!("failed to get closure size due to thread error"))??; - // let width_changes = changed.iter().filter(|&&p| { - // match (pre.get(p), post.get(p)) { - // (Some(version_pre), Some(version_post)) => version_pre != version_post, - // _ => false, - // } - // }); + let size_old = size::Size::from_bytes(closure_size_old); + let size_new = size::Size::from_bytes(closure_size_new); + let size_diff = size_new - size_old; - // let col_width = added - // .iter() - // .chain(removed.iter()) - // .chain(width_changes) - // .map(|p| p.len()) - // .max() - // .unwrap_or_default(); + writeln!(out)?; - // println!("<<< {}", cli.path.to_string_lossy()); - // println!(">>> {}", cli.path2.to_string_lossy()); - // print::print_added(&added, &post, col_width); - // print::print_removed(&removed, &pre, col_width); - // print::print_changes(&changed, &pre, &post, col_width); + writeln!( + out, + "{header}: {size_old} → {size_new}", + header = "SIZE".bold(), + size_old = size_old.red(), + size_new = size_new.green(), + )?; - // if let Some((pre_handle, post_handle)) = closure_size_handles { - // match (pre_handle.join(), post_handle.join()) { - // (Ok(Ok(pre_size)), Ok(Ok(post_size))) => { - // let pre_size = pre_size / 1024 / 1024; - // let post_size = post_size / 1024 / 1024; - // log::debug!("Pre closure size: {pre_size} MiB"); - // log::debug!("Post closure size: {post_size} MiB"); - - // println!("{}", "Closure Size:".underline().bold()); - // println!("Before: {pre_size} MiB"); - // println!("After: {post_size} MiB"); - // println!("Difference: {} MiB", post_size - pre_size); - // }, - // (Ok(Err(e)), _) | (_, Ok(Err(e))) => { - // log::error!("Error getting closure size: {e}"); - // eprintln!("Error getting closure size: {e}"); - // }, - // _ => { - // log::error!( - // "Failed to get closure size information due to a thread error" - // ); - // eprintln!( - // "Error: Failed to get closure size information due to a thread - // error" ); - // }, - // } - // } + writeln!( + out, + "{header}: {size_diff}", + header = "DIFF".bold(), + size_diff = if size_diff.bytes() > 0 { + size_diff.green() + } else { + size_diff.red() + }, + )?; Ok(()) }