use std::collections::BTreeMap;
use std::io;
use humansize::{DECIMAL, format_size};
use owo_colors::{OwoColorize, Style};
use super::table::GUTTER;
use super::{HumanOptions, MAX_ECOSYSTEMS_NAMED, UNANCHORED, palette};
use crate::delete::Outcome;
use crate::report::RunResult;
pub(super) fn reclaimed_by_ecosystem(result: &RunResult) -> Vec<(&'static str, usize, u64)> {
let mut totals: BTreeMap<&'static str, (usize, u64)> = BTreeMap::new();
for entry in &result.entries {
let bytes = entry.reclaimed_bytes();
if bytes == 0 && !entry.outcome.is_reclaimed() {
continue;
}
let slot = totals.entry(entry.ecosystem().unwrap_or(UNANCHORED)).or_default();
if entry.outcome.is_reclaimed() {
slot.0 += 1;
}
slot.1 += bytes;
}
let mut rows: Vec<_> = totals
.into_iter()
.map(|(id, (count, bytes))| (id, count, bytes))
.collect();
rows.sort_by(|left, right| right.2.cmp(&left.2).then_with(|| left.0.cmp(right.0)));
rows
}
pub(super) fn ecosystem_summary(result: &RunResult) -> Option<String> {
let rows = reclaimed_by_ecosystem(result);
if rows.len() < 2 {
return None;
}
let named: Vec<String> = rows
.iter()
.take(MAX_ECOSYSTEMS_NAMED)
.map(|(id, count, bytes)| format!("{id} {} ({count})", format_size(*bytes, DECIMAL)))
.collect();
let line = named.join(", ");
match rows.len().saturating_sub(MAX_ECOSYSTEMS_NAMED) {
0 => Some(line),
hidden => Some(format!("{line}, +{hidden} more")),
}
}
fn remaining_bytes(result: &RunResult) -> u64 {
result
.entries
.iter()
.filter_map(|entry| match &entry.outcome {
Outcome::PartiallyRemoved { remaining, .. } => Some(*remaining),
_ => None,
})
.sum()
}
fn plural(count: usize, noun: &str) -> String {
if count == 1 {
noun.to_owned()
} else {
format!("{noun}s")
}
}
pub(super) fn write_footer(
result: &RunResult,
options: HumanOptions,
wrote_body: bool,
out: &mut impl io::Write,
) -> io::Result<()> {
if wrote_body {
writeln!(out)?;
}
write_headline(result, out)?;
write_breakdown(result, out)?;
write_skipped_and_inaccessible(result, options, out)?;
write_removal_notices(result, out)?;
write_failure_notices(result, out)?;
if result.dry_run {
writeln!(out, "{}", "Dry run — nothing was removed.".style(palette::quiet()))?;
}
Ok(())
}
fn write_line(out: &mut impl io::Write, text: &str, style: Style) -> io::Result<()> {
writeln!(out, "{GUTTER}{}", text.style(style))
}
fn write_headline(result: &RunResult, out: &mut impl io::Write) -> io::Result<()> {
let totals = result.totals();
let verb = if result.dry_run { "reclaimable" } else { "reclaimed" };
writeln!(
out,
"{} {}, {} {verb} in {:.2?}",
totals.reclaimed.bold(),
plural(totals.reclaimed, "artifact"),
format_size(totals.bytes, DECIMAL).bold(),
result.timings.total
)
}
fn write_breakdown(result: &RunResult, out: &mut impl io::Write) -> io::Result<()> {
write_line(out, &stage_breakdown(result), palette::quiet())?;
if let Some(summary) = ecosystem_summary(result) {
write_line(out, &summary, palette::quiet())?;
}
Ok(())
}
fn write_skipped_and_inaccessible(
result: &RunResult,
options: HumanOptions,
out: &mut impl io::Write,
) -> io::Result<()> {
let totals = result.totals();
if totals.skipped > 0 {
let text = format!("{} {} skipped", totals.skipped, plural(totals.skipped, "candidate"));
write!(out, "{GUTTER}{}", text.style(palette::quiet()))?;
if options.verbose {
writeln!(out)?;
} else {
writeln!(out, "{GUTTER}{}", "--verbose for why".style(palette::quiet()))?;
}
}
let inaccessible = result.failures.iter().filter(|failure| failure.inaccessible).count();
if inaccessible > 0 {
let text = format!(
"{inaccessible} {} inaccessible (permission denied){}",
if inaccessible == 1 { "directory" } else { "directories" },
if options.verbose {
""
} else {
" — --verbose lists them"
},
);
write_line(out, &text, palette::quiet())?;
}
Ok(())
}
fn write_removal_notices(result: &RunResult, out: &mut impl io::Write) -> io::Result<()> {
let totals = result.totals();
if totals.detached > 0 {
let text = format!(
"{} {} detached into the trash — the space returns as the background reaper frees them (`voom reap` waits)",
totals.detached,
plural(totals.detached, "artifact"),
);
write_line(out, &text, palette::quiet())?;
}
if totals.refused > 0 {
let text = format!(
"{} {} refused by a safety rail",
totals.refused,
plural(totals.refused, "artifact")
);
write_line(out, &text, palette::refused())?;
}
Ok(())
}
fn write_failure_notices(result: &RunResult, out: &mut impl io::Write) -> io::Result<()> {
let totals = result.totals();
if totals.unmeasured > 0 {
let text = format!(
"{} {} could not be measured in full — a directory inside could not be read, so the total is a lower bound",
totals.unmeasured,
plural(totals.unmeasured, "artifact"),
);
write_line(out, &text, palette::failed())?;
}
if totals.dependencies > 0 {
let noun = if totals.dependencies == 1 {
"directory"
} else {
"directories"
};
let text = format!(
"{} of them {} a dependency {noun} — a re-download, not build output",
totals.dependencies,
if totals.dependencies == 1 { "is" } else { "are" },
);
write_line(out, &text, palette::refused())?;
}
if totals.partial > 0 {
let text = format!(
"{} {} partly removed: {} freed and counted above, {}",
totals.partial,
plural(totals.partial, "artifact"),
format_size(totals.partial_bytes, DECIMAL),
super::remaining_words(remaining_bytes(result)),
);
write_line(out, &text, palette::partial())?;
}
if totals.failed > 0 {
let text = format!(
"{} {} could not be removed",
totals.failed,
plural(totals.failed, "artifact")
);
write_line(out, &text, palette::failed())?;
}
Ok(())
}
fn stage_breakdown(result: &RunResult) -> String {
let timings = &result.timings;
format!(
"scan {:.2?} · policy {:.2?} · size {:.2?} · {} {:.2?}{}",
timings.scan,
timings.policy,
timings.size,
if result.dry_run { "withheld" } else { "delete" },
timings.delete,
if timings.git.is_zero() {
String::new()
} else {
format!(" · git {:.2?}", timings.git)
},
)
}