From 3415f9bb388a8519171e01c98d2def93411d550a Mon Sep 17 00:00:00 2001 From: Jack O'Sullivan Date: Fri, 27 Nov 2026 18:30:51 +0000 Subject: [PATCH] deploy: Read unsigned size changes Newer Nix prints a closure growing as a bare size (`129.0 KiB`) without a sign, and without colour when piped, so the size ended up in the version column. Recognise a trailing size by its unit and sign it. Co-Authored-By: Claude Opus 5.5 --- src/ops/deploy.rs | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/ops/deploy.rs b/src/ops/deploy.rs index f9d3044..471d42a 100644 --- a/src/ops/deploy.rs +++ b/src/ops/deploy.rs @@ -415,26 +415,35 @@ impl Changes { /// A line of `nix store diff-closures` (`name: 1.0 → 1.1, +3.4 KiB`) as package, version change /// and size change. -fn package_cells(line: &str) -> [&str; 3] { - let Some((name, rest)) = line.split_once(": ") else { return [line, "", ""] }; +fn package_cells(line: &str) -> [String; 3] { + let Some((name, rest)) = line.split_once(": ") else { return [line.into(), String::new(), String::new()] }; if let Some((version, size)) = rest.rsplit_once(", ") && let Some(size) = package_size(size) { - return [name, version, size]; + return [name.into(), version.into(), size]; } if let Some(size) = package_size(rest) { - return [name, "unchanged", size]; + return [name.into(), "unchanged".into(), size]; } - [name, rest, ""] + [name.into(), rest.into(), String::new()] } -fn package_size(value: &str) -> Option<&str> { - // Some Nix versions indicate the direction with colour even on piped output. - value +/// A size change, signed. Nix versions differ: some sign increases, some only colour them (even +/// on piped output), and some do neither; decreases always have a sign. +fn package_size(value: &str) -> Option { + let plain = value .strip_prefix("\x1b[31;1m") .or_else(|| value.strip_prefix("\x1b[32;1m")) .and_then(|size| size.strip_suffix("\x1b[0m")) - .or_else(|| value.starts_with(['+', '-']).then_some(value)) + .unwrap_or(value); + + let (sign, size) = match plain.strip_prefix(['+', '-']) { + Some(size) => (&plain[..1], size), + None => ("+", plain), + }; + let (number, unit) = size.split_once(' ')?; + let sized = number.parse::().is_ok() && ["B", "KiB", "MiB", "GiB", "TiB"].contains(&unit); + sized.then(|| format!("{sign}{size}")) } #[cfg(test)] @@ -445,7 +454,13 @@ mod tests { fn splits_package_lines() { assert_eq!(package_cells("a: 1.0 → 1.1, +3.4 KiB"), ["a", "1.0 → 1.1", "+3.4 KiB"]); assert_eq!(package_cells("b: -12.0 KiB"), ["b", "unchanged", "-12.0 KiB"]); - assert_eq!(package_cells("b: \x1b[31;1m12.0 KiB\x1b[0m"), ["b", "unchanged", "12.0 KiB"]); + assert_eq!(package_cells("b: \x1b[31;1m12.0 KiB\x1b[0m"), ["b", "unchanged", "+12.0 KiB"]); + assert_eq!(package_cells("b: 12.0 KiB"), ["b", "unchanged", "+12.0 KiB"]); + assert_eq!( + package_cells("slop: 0.1.0, 0.1.0-fish → 0.2.0, 0.2.0-fish, 129.0 KiB"), + ["slop", "0.1.0, 0.1.0-fish → 0.2.0, 0.2.0-fish", "+129.0 KiB"] + ); + assert_eq!(package_cells("c: 1.0, 2 KiB → 1.1"), ["c", "1.0, 2 KiB → 1.1", ""]); assert_eq!(package_cells("b: 1.0 → 1.1, \x1b[32;1m-12.0 KiB\x1b[0m"), ["b", "1.0 → 1.1", "-12.0 KiB"]); assert_eq!(package_cells("c: ∅ → 2.0"), ["c", "∅ → 2.0", ""]); assert_eq!(package_cells("odd"), ["odd", "", ""]); -- 2.56.0