use std::collections::BTreeMap;
use std::io;
use std::path::{Path, PathBuf};
use humansize::{DECIMAL, format_size};
use owo_colors::{OwoColorize, Style};
use super::{Entry, RunResult};
use crate::classify::Provenance;
use crate::delete::Outcome;
const GUTTER: &str = " ";
const BECAUSE: &str = "— ";
const UNANCHORED: &str = "unanchored";
const MAX_ECOSYSTEMS_NAMED: usize = 6;
const MAX_PATH_COLUMN: usize = 40;
#[derive(Debug, Clone, Copy, Default)]
pub struct HumanOptions {
pub verbose: bool,
pub summary_only: bool,
}
mod palette {
use owo_colors::Style;
pub fn removed() -> Style {
Style::new().green()
}
pub fn would_remove() -> Style {
Style::new().yellow()
}
pub fn refused() -> Style {
Style::new().cyan()
}
pub fn failed() -> Style {
Style::new().red()
}
pub fn quiet() -> Style {
Style::new().dimmed()
}
pub fn unanchored() -> Style {
Style::new().magenta()
}
}
fn outcome_label(outcome: &Outcome) -> &'static str {
match outcome {
Outcome::Removed => "removed",
Outcome::WouldRemove => "would remove",
Outcome::Refused(_) => "refused",
Outcome::Failed(_) => "failed",
}
}
fn outcome_style(outcome: &Outcome) -> Style {
match outcome {
Outcome::Removed => palette::removed(),
Outcome::WouldRemove => palette::would_remove(),
Outcome::Refused(_) => palette::refused(),
Outcome::Failed(_) => palette::failed(),
}
}
struct Line {
label: &'static str,
label_style: Style,
size: Option<String>,
tag: Option<&'static str>,
tag_style: Style,
path: String,
detail: Option<String>,
}
#[derive(Debug, Default, Clone, Copy)]
struct Widths {
label: usize,
size: usize,
tag: usize,
path: usize,
}
impl Widths {
fn measure(lines: &[Line]) -> Self {
let mut widths = Self::default();
for line in lines {
widths.label = widths.label.max(width_of(line.label));
if let Some(size) = &line.size {
widths.size = widths.size.max(width_of(size));
}
if let Some(tag) = line.tag {
widths.tag = widths.tag.max(width_of(tag));
}
if line.detail.is_some() {
widths.path = widths.path.max(width_of(&line.path));
}
}
widths.path = widths.path.min(MAX_PATH_COLUMN);
widths
}
}
fn width_of(text: &str) -> usize {
text.chars().count()
}
fn write_fill(out: &mut impl io::Write, spaces: usize) -> io::Result<()> {
write!(out, "{:spaces$}", "")
}
fn write_column(out: &mut impl io::Write, text: &str, style: Style, width: usize) -> io::Result<()> {
write!(out, "{}", style.style(text))?;
write_fill(out, width.saturating_sub(width_of(text)))
}
fn write_column_right(out: &mut impl io::Write, text: &str, style: Style, width: usize) -> io::Result<()> {
write_fill(out, width.saturating_sub(width_of(text)))?;
write!(out, "{}", style.style(text))
}
fn write_line(line: &Line, widths: Widths, out: &mut impl io::Write) -> io::Result<()> {
write!(out, "{GUTTER}")?;
write_column(out, line.label, line.label_style, widths.label)?;
if widths.size > 0 {
write!(out, "{GUTTER}")?;
write_column_right(
out,
line.size.as_deref().unwrap_or_default(),
palette::quiet(),
widths.size,
)?;
}
if widths.tag > 0 {
write!(out, "{GUTTER}")?;
write_column(out, line.tag.unwrap_or_default(), line.tag_style, widths.tag)?;
}
write!(out, "{GUTTER}{}", line.path)?;
match &line.detail {
None => writeln!(out),
Some(detail) => {
write_fill(out, widths.path.saturating_sub(width_of(&line.path)))?;
writeln!(
out,
"{GUTTER}{}",
format_args!("{BECAUSE}{detail}").style(palette::quiet())
)
}
}
}
fn write_block(lines: &[Line], out: &mut impl io::Write) -> io::Result<()> {
let widths = Widths::measure(lines);
for line in lines {
write_line(line, widths, out)?;
}
Ok(())
}
fn base_path(roots: &[PathBuf]) -> Option<&Path> {
let first = roots.first()?;
let base = first
.ancestors()
.find(|candidate| roots.iter().all(|root| root.starts_with(candidate)))?;
(base.is_absolute() && base.parent().is_some()).then_some(base)
}
fn shorten(path: &Path, base: Option<&Path>) -> String {
let Some(base) = base else {
return path.display().to_string();
};
match path.strip_prefix(base) {
Ok(relative) if relative.as_os_str().is_empty() => ".".to_owned(),
Ok(relative) => relative.display().to_string(),
Err(_) => path.display().to_string(),
}
}
fn artifact_line(entry: &Entry, base: Option<&Path>) -> Line {
let detail = match &entry.outcome {
Outcome::Refused(refusal) => Some(refusal.to_string()),
Outcome::Failed(message) => Some(message.clone()),
Outcome::Removed | Outcome::WouldRemove => match &entry.finding.provenance {
Provenance::Included { pattern } => {
let shown = shorten(Path::new(pattern), base);
(shown != shorten(&entry.path, base))
.then(|| format!("from config `{shown}`"))
.or_else(|| Some("from config".to_owned()))
}
Provenance::Anchored { .. } => None,
},
};
let ecosystem = entry.ecosystem();
Line {
label: outcome_label(&entry.outcome),
label_style: outcome_style(&entry.outcome),
size: Some(format_size(entry.bytes, DECIMAL)),
tag: Some(ecosystem.unwrap_or(UNANCHORED)),
tag_style: if ecosystem.is_some() {
palette::quiet()
} else {
palette::unanchored()
},
path: shorten(&entry.path, base),
detail,
}
}
fn skip_lines(result: &RunResult, base: Option<&Path>) -> Vec<Line> {
result
.skip_reasons()
.map(|(path, reason)| Line {
label: "skipped",
label_style: palette::quiet(),
size: None,
tag: None,
tag_style: palette::quiet(),
path: shorten(path, base),
detail: Some(reason.to_string()),
})
.collect()
}
fn failure_lines(result: &RunResult, verbose: bool, base: Option<&Path>) -> Vec<Line> {
result
.failures
.iter()
.filter(|failure| verbose || !failure.transient)
.map(|failure| Line {
label: if failure.transient { "interrupted" } else { "walk error" },
label_style: if failure.transient {
palette::quiet()
} else {
palette::failed()
},
size: None,
tag: None,
tag_style: palette::quiet(),
path: failure
.path
.as_deref()
.map_or_else(|| "-".to_owned(), |path| shorten(path, base)),
detail: Some(failure.message.clone()),
})
.collect()
}
pub fn render(result: &RunResult, options: HumanOptions, out: &mut impl io::Write) -> io::Result<()> {
let mut wrote_body = false;
if !options.summary_only {
let base = base_path(&result.roots);
let artifacts: Vec<Line> = result.entries.iter().map(|entry| artifact_line(entry, base)).collect();
let skips = if options.verbose {
skip_lines(result, base)
} else {
Vec::new()
};
let failures = failure_lines(result, options.verbose, base);
if let Some(base) = base.filter(|_| !(artifacts.is_empty() && skips.is_empty() && failures.is_empty())) {
writeln!(out, "{}", base.display().bold())?;
wrote_body = true;
}
let mut wrote_block = false;
for block in [&artifacts, &skips, &failures] {
if block.is_empty() {
continue;
}
if wrote_block {
writeln!(out)?;
}
write_block(block, out)?;
(wrote_block, wrote_body) = (true, true);
}
}
write_footer(result, options, wrote_body, out)
}
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.iter().filter(|entry| entry.outcome.is_reclaimed()) {
let slot = totals.entry(entry.ecosystem().unwrap_or(UNANCHORED)).or_default();
slot.0 += 1;
slot.1 += entry.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
}
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 plural(count: usize, noun: &str) -> String {
if count == 1 {
noun.to_owned()
} else {
format!("{noun}s")
}
}
fn write_footer(
result: &RunResult,
options: HumanOptions,
wrote_body: bool,
out: &mut impl io::Write,
) -> io::Result<()> {
let totals = result.totals();
let verb = if result.dry_run { "reclaimable" } else { "reclaimed" };
if wrote_body {
writeln!(out)?;
}
writeln!(
out,
"{} {}, {} {verb} in {:.2?}",
totals.reclaimed.bold(),
plural(totals.reclaimed, "artifact"),
format_size(totals.bytes, DECIMAL).bold(),
result.elapsed
)?;
if let Some(summary) = ecosystem_summary(result) {
writeln!(out, "{GUTTER}{}", summary.style(palette::quiet()))?;
}
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()))?;
}
}
if totals.refused > 0 {
let text = format!(
"{} {} refused by a safety rail",
totals.refused,
plural(totals.refused, "artifact")
);
writeln!(out, "{GUTTER}{}", text.style(palette::refused()))?;
}
if totals.failed > 0 {
let text = format!(
"{} {} could not be removed",
totals.failed,
plural(totals.failed, "artifact")
);
writeln!(out, "{GUTTER}{}", text.style(palette::failed()))?;
}
if result.dry_run {
writeln!(out, "{}", "Dry run — nothing was removed.".style(palette::quiet()))?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::*;
use crate::classify::SkipReason;
use crate::delete::Refusal;
use crate::report::fixtures;
use crate::report::fixtures::{spell, unspell};
use crate::scan::Skipped;
fn entry(path: &str, spec: &str, bytes: u64, outcome: Outcome) -> Entry {
fixtures::entry(&spell(path), spec, bytes, outcome)
}
fn included(path: &str, pattern: &str, bytes: u64, outcome: Outcome) -> Entry {
fixtures::included(&spell(path), &spell(pattern), bytes, outcome)
}
fn result(entries: Vec<Entry>, dry_run: bool) -> RunResult {
RunResult {
roots: vec![PathBuf::from(spell("/projects"))],
..fixtures::result(entries, dry_run)
}
}
fn path(literal: &str) -> PathBuf {
PathBuf::from(spell(literal))
}
fn render_to_string(result: &RunResult, options: HumanOptions) -> String {
let mut buffer = Vec::new();
render(result, options, &mut buffer).expect("writing to a Vec cannot fail");
let text = String::from_utf8(buffer).expect("the renderer emits UTF-8");
unspell(&anstream::adapter::strip_str(&text).to_string())
}
fn verbose() -> HumanOptions {
HumanOptions {
verbose: true,
summary_only: false,
}
}
fn interrupted() -> RunResult {
let mut result = result(Vec::new(), true);
result.failures = vec![
crate::scan::WalkFailure {
path: Some(path("/projects/locked")),
message: "Permission denied (os error 13)".to_owned(),
transient: false,
},
crate::scan::WalkFailure {
path: Some(path("/projects/containers")),
message: "Interrupted system call (os error 4)".to_owned(),
transient: true,
},
];
result
}
#[test]
fn should_not_list_a_transient_interruption_by_default() {
let text = render_to_string(&interrupted(), HumanOptions::default());
assert!(text.contains("Permission denied"), "{text}");
assert!(!text.contains("Interrupted system call"), "{text}");
}
#[test]
fn should_list_a_transient_interruption_when_asked_for_detail() {
let text = render_to_string(&interrupted(), verbose());
assert!(text.contains("Interrupted system call"), "{text}");
assert!(
text.contains("interrupted"),
"it is labelled as an interruption, not as a walk error: {text}"
);
}
#[test]
fn should_label_an_unanchored_removal_as_unanchored() {
let result = result(
vec![included(
"/scratch/build-junk",
"/scratch/build-junk",
1024,
Outcome::Removed,
)],
false,
);
let text = render_to_string(&result, HumanOptions::default());
assert!(text.contains("unanchored"), "{text}");
assert!(text.contains("from config"), "{text}");
}
fn mixed() -> RunResult {
result(
vec![
entry("/projects/api/target", "rust.target", 4_200_000_000, Outcome::Removed),
entry("/projects/web/dist", "node.dist", 18_500_000, Outcome::Removed),
entry(
"/projects/link/target",
"rust.target",
0,
Outcome::Refused(Refusal::Symlink),
),
entry(
"/projects/locked/target",
"rust.target",
900,
Outcome::Failed("Permission denied".to_owned()),
),
],
false,
)
}
#[test]
fn should_render_a_run_with_every_outcome() {
insta::assert_snapshot!(render_to_string(&mixed(), HumanOptions::default()));
}
#[test]
fn should_render_a_dry_run_as_reclaimable_and_say_nothing_was_removed() {
let result = result(
vec![entry(
"/projects/api/target",
"rust.target",
4_200_000_000,
Outcome::WouldRemove,
)],
true,
);
insta::assert_snapshot!(render_to_string(&result, HumanOptions::default()));
}
fn skipped() -> RunResult {
let mut result = result(
vec![entry("/projects/api/target", "rust.target", 100, Outcome::Removed)],
false,
);
result.skipped_count = 2;
result.skips = vec![
Skipped {
path: path("/projects/data/target"),
reason: SkipReason::NoMarker {
ecosystem: "rust",
markers: &["Cargo.toml"],
},
},
Skipped {
path: path("/projects/tool/bin"),
reason: SkipReason::NotEnabled {
spec: "go.bin".to_owned(),
note: Some("`bin/` beside a go.mod is as often hand-written scripts as output."),
},
},
];
result.sort();
result
}
#[test]
fn should_show_a_skip_count_by_default_and_reasons_when_verbose() {
insta::assert_snapshot!(
"default_hides_skip_detail",
render_to_string(&skipped(), HumanOptions::default())
);
insta::assert_snapshot!("verbose_explains_every_skip", render_to_string(&skipped(), verbose()));
}
#[test]
fn should_not_leave_empty_columns_on_a_skip_line() {
let text = render_to_string(&skipped(), verbose());
let skip = text.lines().find(|line| line.contains("skipped")).expect("a skip line");
assert!(
!skip.contains(" "),
"no run of blanks where a size and an ecosystem would be: {skip:?}"
);
}
#[test]
fn should_introduce_a_reason_without_nesting_parentheses() {
let text = render_to_string(&skipped(), verbose());
assert!(text.contains("— no rust marker (Cargo.toml) proves it"), "{text}");
assert!(!text.contains("(no rust marker"), "{text}");
}
#[test]
fn should_render_only_the_footer_when_asked_for_a_summary() {
let options = HumanOptions {
verbose: false,
summary_only: true,
};
insta::assert_snapshot!(render_to_string(&mixed(), options));
}
#[test]
fn should_render_a_clean_tree_without_a_leading_blank_line() {
insta::assert_snapshot!(render_to_string(&result(Vec::new(), false), HumanOptions::default()));
}
#[test]
fn should_print_paths_relative_to_the_scan_root_named_in_the_header() {
let text = render_to_string(&mixed(), HumanOptions::default());
assert!(text.starts_with("/projects\n"), "the root is stated once: {text}");
assert!(text.contains(" api/target\n"), "{text}");
assert!(
!text.contains(" /projects/api/target"),
"no line repeats the root: {text}"
);
}
#[test]
fn should_print_a_path_outside_the_roots_in_full() {
let result = result(
vec![included("/scratch/build-junk", "/scratch/*", 1024, Outcome::Removed)],
false,
);
let text = render_to_string(&result, HumanOptions::default());
assert!(text.contains("/scratch/build-junk"), "{text}");
}
#[test]
fn should_use_the_common_parent_as_the_base_for_several_roots() {
let roots = vec![path("/projects/api"), path("/projects/web")];
assert_eq!(base_path(&roots), Some(path("/projects").as_path()));
}
#[test]
fn should_not_shorten_against_a_useless_base() {
assert_eq!(base_path(&[path("/a"), path("/b")]), None);
assert_eq!(base_path(&[PathBuf::from(".")]), None);
assert_eq!(base_path(&[]), None);
}
#[test]
fn should_not_pad_a_reason_out_to_the_longest_path_in_the_block() {
let long = format!("/projects/{}/target", "deep/".repeat(20));
let result = result(
vec![
entry(&long, "rust.target", 1_000, Outcome::Removed),
entry(
"/projects/link/target",
"rust.target",
0,
Outcome::Refused(Refusal::Symlink),
),
],
false,
);
let text = render_to_string(&result, HumanOptions::default());
assert!(text.contains("link/target — is a symlink"), "{text}");
}
#[test]
fn should_bound_the_path_column() {
let long = format!("/projects/{}/target", "deep/".repeat(20));
let lines = vec![
artifact_line(
&entry(&long, "rust.target", 0, Outcome::Refused(Refusal::Symlink)),
None,
),
artifact_line(
&entry(
"/projects/a/target",
"rust.target",
0,
Outcome::Refused(Refusal::Symlink),
),
None,
),
];
assert_eq!(Widths::measure(&lines).path, MAX_PATH_COLUMN);
}
#[test]
fn should_break_the_reclaimed_total_down_by_ecosystem() {
let text = render_to_string(&mixed(), HumanOptions::default());
assert!(text.contains("rust 4.20 GB (1), node 18.50 MB (1)"), "{text}");
}
#[test]
fn should_omit_the_breakdown_when_there_is_only_one_ecosystem() {
assert_eq!(ecosystem_summary(&skipped()), None);
}
#[test]
fn should_say_how_many_ecosystems_the_breakdown_left_out() {
let specs = [
"rust.target",
"node.dist",
"go.bin",
"python.build",
"maven.target",
"dotnet.obj",
"zig.zig-out",
];
let entries = specs
.iter()
.enumerate()
.map(|(index, spec)| {
let bytes = (specs.len() - index) as u64 * 1_000_000;
entry(&format!("/projects/p{index}/artifact"), spec, bytes, Outcome::Removed)
})
.collect();
let summary = ecosystem_summary(&result(entries, false)).expect("several ecosystems");
assert_eq!(summary.matches('(').count(), MAX_ECOSYSTEMS_NAMED);
assert!(summary.ends_with(", +1 more"), "{summary}");
}
#[test]
fn should_never_write_trailing_whitespace() {
for options in [HumanOptions::default(), verbose()] {
for result in [&mixed(), &skipped(), &interrupted()] {
for line in render_to_string(result, options).lines() {
assert_eq!(line, line.trim_end(), "trailing blanks on {line:?}");
}
}
}
}
#[test]
fn should_write_column_fill_outside_the_styled_span() {
let mut buffer = Vec::new();
render(&mixed(), HumanOptions::default(), &mut buffer).expect("writing to a Vec cannot fail");
let coloured = String::from_utf8(buffer).expect("the renderer emits UTF-8");
assert!(coloured.contains('\u{1b}'), "the renderer emits colour");
for opening in ["\u{1b}[1m", "\u{1b}[2m", "\u{1b}[31m", "\u{1b}[32m", "\u{1b}[33m"] {
assert!(
!coloured.contains(&format!("{opening} ")),
"a styled span opens on padding: {coloured:?}"
);
}
for closing in ["\u{1b}[0m", "\u{1b}[39m"] {
assert!(
!coloured.contains(&format!(" {closing}")),
"a styled span closes on padding: {coloured:?}"
);
}
assert_eq!(
unspell(&anstream::adapter::strip_str(&coloured).to_string()),
render_to_string(&mixed(), HumanOptions::default()),
"stripping the colour is exactly what the snapshots pin"
);
}
#[test]
fn should_be_byte_identical_across_two_renders() {
let result = mixed();
assert_eq!(
render_to_string(&result, HumanOptions::default()),
render_to_string(&result, HumanOptions::default())
);
}
}