prune,ui,docs: color the digest, add -f/--force and a sizing spinner
- -f/--force skips the size pass and the confirmation prompt: remove
immediately, reporting counts but no sizes. The fastest cleanup
path; --dry-run still previews.
- The default path shows the grouped digest and asks before removing
(the raw-mode yes/no prompt; non-interactive sessions keep their
headless path, so scripts reach for --force).
- Sizes are computed under a spinner ("Computing sizes of
directories to prune..."), and the digest colors the counts (bold)
and the sizes (cyan) on terminals only.
This commit is contained in:
1 parent
74c74f0a61
commit
b85a377b24
4 files changed
+231
-82
No files matched your search
+101
-39
@@ -1,5 +1,5 @@
|
||||
use std::env;
|
||||
use std::io::Write;
|
||||
use std::io::{IsTerminal, Write};
|
||||
|
||||
extern crate clap;
|
||||
use clap::{Command, arg, command};
|
||||
@@ -12,14 +12,35 @@ use log::{error, info};
|
||||
/// Obtain the current working directory, exiting with a helpful message on failure.
|
||||
fn current_dir_or_exit() -> std::path::PathBuf {
|
||||
match std::env::current_dir() {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
error!("Could not determine the current working directory: {}", e);
|
||||
Ok(dir) => dir,
|
||||
Err(err) => {
|
||||
error!("cannot determine the current directory: {err}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrap `text` in the ANSI `code` (e.g. "1" for bold) when stderr is a
|
||||
/// terminal; return it unchanged otherwise (piped output keeps no escape
|
||||
/// codes).
|
||||
fn ansi_wrap(code: &str, text: &str) -> String {
|
||||
if std::io::stderr().is_terminal() {
|
||||
format!("\x1b[{code}m{text}\x1b[0m")
|
||||
} else {
|
||||
text.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
/// Bold text for the prune digest.
|
||||
fn bold(text: &str) -> String {
|
||||
ansi_wrap("1", text)
|
||||
}
|
||||
|
||||
/// Cyan text for the prune digest.
|
||||
fn cyan(text: &str) -> String {
|
||||
ansi_wrap("36", text)
|
||||
}
|
||||
|
||||
/// CLI-side Ctrl+C wiring. The passive state (interrupted flag, cleanup
|
||||
/// hook registry, reporter slot) lives in `pkh::interrupt`; everything that
|
||||
/// installs, prints or exits lives here: the SIGINT handler only wakes a
|
||||
@@ -552,6 +573,14 @@ fn main() {
|
||||
.help("Remove everything, including recent build records and staged trees")
|
||||
.long_help("Remove everything, whatever its age: recent build records and their staged trees (the incremental bases `pkh deb --incremental` forks) included, plus the cached chroot tarballs, which are expensive to re-download. Records and locks a live build holds are never touched."),
|
||||
)
|
||||
.arg(
|
||||
clap::Arg::new("force")
|
||||
.short('f')
|
||||
.long("force")
|
||||
.action(clap::ArgAction::SetTrue)
|
||||
.help("Skip the size computation and the confirmation prompt")
|
||||
.long_help("Skip the size computation and the confirmation prompt: remove immediately, reporting counts but no sizes. The fastest cleanup path; a --dry-run still previews, and -a decides the policy as usual."),
|
||||
)
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
@@ -1202,45 +1231,78 @@ fn main() {
|
||||
.copied()
|
||||
.unwrap_or(false);
|
||||
let all = sub_matches.get_one::<bool>("all").copied().unwrap_or(false);
|
||||
let force = sub_matches
|
||||
.get_one::<bool>("force")
|
||||
.copied()
|
||||
.unwrap_or(false);
|
||||
|
||||
let options = pkh::prune::PruneOptions { dry_run, all };
|
||||
match pkh::prune::prune(options) {
|
||||
Ok(report) => {
|
||||
if report.is_empty() {
|
||||
info!("Nothing to prune.");
|
||||
} else {
|
||||
if report.dry_run {
|
||||
info!("Dry run - would remove:");
|
||||
} else {
|
||||
info!("Removing:");
|
||||
}
|
||||
for group in &report.groups {
|
||||
let size = if group.bytes > 0 {
|
||||
format!(" for {}", pkh::prune::format_bytes(group.bytes))
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
info!("{} {}{size}", group.count, group.label());
|
||||
}
|
||||
for (path, err) in &report.failed {
|
||||
error!("Failed to remove {}: {}", path.display(), err);
|
||||
}
|
||||
let total = pkh::prune::format_bytes(report.total_bytes);
|
||||
if report.dry_run {
|
||||
info!("Would have freed {total} total.");
|
||||
} else {
|
||||
info!("Freed {total} total.");
|
||||
}
|
||||
}
|
||||
if !report.failed.is_empty() {
|
||||
std::process::exit(1);
|
||||
}
|
||||
let options = pkh::prune::PruneOptions {
|
||||
dry_run,
|
||||
all,
|
||||
force,
|
||||
};
|
||||
// The size pass spins on a terminal; --force skips it entirely.
|
||||
let spinner = (!force)
|
||||
.then(|| pkh::ui::wait_spinner("Computing sizes of directories to prune..."));
|
||||
let plan = pkh::prune::plan(options);
|
||||
if let Some(spinner) = spinner {
|
||||
spinner.finish_and_clear();
|
||||
}
|
||||
|
||||
let digest = plan.report();
|
||||
if digest.is_empty() {
|
||||
info!("Nothing to prune.");
|
||||
return;
|
||||
}
|
||||
|
||||
// The digest: one grouped line per kind. Counts are bold and
|
||||
// sizes cyan — when there are sizes at all (--force skips the
|
||||
// walk and reports counts only).
|
||||
let header = if dry_run {
|
||||
"Dry run - would remove:"
|
||||
} else {
|
||||
"Would remove:"
|
||||
};
|
||||
info!("{}", bold(header));
|
||||
for group in &digest.groups {
|
||||
let size = if group.bytes > 0 {
|
||||
format!(" for {}", cyan(&pkh::prune::format_bytes(group.bytes)))
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
info!("{} {}{size}", bold(&group.count.to_string()), group.label());
|
||||
}
|
||||
let total = cyan(&pkh::prune::format_bytes(digest.total_bytes));
|
||||
if dry_run {
|
||||
// --force skips the size pass: counts without a total.
|
||||
if !force {
|
||||
info!("Would have freed {total} total.");
|
||||
}
|
||||
Err(e) => {
|
||||
error!("{}", e);
|
||||
std::process::exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
// Confirmation for the interactive case (--force and
|
||||
// non-interactive sessions skip it).
|
||||
if !force {
|
||||
let confirmed =
|
||||
pkh::ui::prompt::confirm("Proceed with the removal?", false).unwrap_or(false);
|
||||
if !confirmed {
|
||||
info!("Aborted.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
info!("Removing:");
|
||||
let report = pkh::prune::apply(plan);
|
||||
for (path, err) in &report.failed {
|
||||
error!("Failed to remove {}: {}", path.display(), err);
|
||||
}
|
||||
if !force {
|
||||
info!("Freed {} total.", total);
|
||||
}
|
||||
if !report.failed.is_empty() {
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
Some(("lint", sub_matches)) => {
|
||||
if sub_matches.get_flag("list_tags") {
|
||||
|
||||
Reference in new issue
Block a user