fix(xtask): skip png conversion on legacy chafa

This commit is contained in:
2025-10-25 23:16:24 +02:00
parent f6a3f235df
commit 877ece07be

View File

@@ -53,22 +53,42 @@ pub(crate) fn run(output: Option<PathBuf>, chafa: Option<PathBuf>, no_png: bool)
} }
let png_requested = !no_png; let png_requested = !no_png;
let mut chafa_binary: Option<PathBuf> = None; let mut png_skip_reason: Option<String> = None;
if png_requested { let chafa_binary: Option<PathBuf> = if png_requested {
if let Some(ref path) = chafa { let candidate = if let Some(ref path) = chafa {
if path.exists() { if path.exists() {
chafa_binary = Some(path.clone()); Some(path.clone())
} else { } else {
bail!("specified chafa binary '{}' does not exist", path.display()); bail!("specified chafa binary '{}' does not exist", path.display());
} }
} else if let Ok(found) = which("chafa") {
chafa_binary = Some(found);
} else { } else {
eprintln!( which("chafa").ok()
"warning: chafa not found in PATH; skipping PNG conversion (install `chafa` or rerun with --no-png)" };
);
if let Some(path) = candidate {
match chafa_supports_save(&path) {
Ok(true) => Some(path),
Ok(false) => {
png_skip_reason = Some(format!(
"chafa at '{}' lacks --save support; install chafa 1.14+ or rerun with --no-png",
path.display()
));
None
}
Err(err) => {
eprintln!("warning: failed to probe chafa capabilities: {err}");
png_skip_reason = Some("failed to verify chafa capabilities".to_string());
None
}
}
} else {
png_skip_reason =
Some("chafa not found in PATH; install it or rerun with --no-png".to_string());
None
} }
} } else {
None
};
let runtime = Runtime::new().context("failed to create tokio runtime")?; let runtime = Runtime::new().context("failed to create tokio runtime")?;
@@ -86,7 +106,7 @@ pub(crate) fn run(output: Option<PathBuf>, chafa: Option<PathBuf>, no_png: bool)
scene.height, scene.height,
&output_dir, &output_dir,
scene.name, scene.name,
Some(binary.as_path()), binary.as_path(),
) )
{ {
eprintln!("warning: {}", err); eprintln!("warning: {}", err);
@@ -94,11 +114,13 @@ pub(crate) fn run(output: Option<PathBuf>, chafa: Option<PathBuf>, no_png: bool)
} }
println!("Screenshots written to {}", output_dir.display()); println!("Screenshots written to {}", output_dir.display());
if png_requested { if png_requested && chafa_binary.is_none() {
if chafa_binary.is_none() { if let Some(reason) = png_skip_reason {
println!("PNG conversion skipped (install `chafa` or pass --no-png to disable)"); println!("PNG conversion skipped ({reason})");
} else {
println!("PNG conversion skipped");
} }
} else { } else if !png_requested {
println!("PNG conversion skipped (use --no-png=false or omit flag to enable)"); println!("PNG conversion skipped (use --no-png=false or omit flag to enable)");
} }
@@ -111,14 +133,10 @@ fn convert_to_png(
height: u16, height: u16,
output_dir: &Path, output_dir: &Path,
name: &str, name: &str,
chafa_path: Option<&Path>, chafa_path: &Path,
) -> Result<()> { ) -> Result<()> {
let png_path = output_dir.join(format!("{}.png", name)); let png_path = output_dir.join(format!("{}.png", name));
let mut command = if let Some(path) = chafa_path { let mut command = Command::new(chafa_path);
Command::new(path)
} else {
Command::new("chafa")
};
let status = command let status = command
.arg("--size") .arg("--size")
.arg(format!("{}x{}", width, height)) .arg(format!("{}x{}", width, height))
@@ -137,6 +155,20 @@ fn convert_to_png(
Ok(()) Ok(())
} }
fn chafa_supports_save(chafa_path: &Path) -> Result<bool> {
let output = Command::new(chafa_path)
.arg("--help")
.output()
.with_context(|| {
format!(
"failed to run '{}' to probe capabilities",
chafa_path.display()
)
})?;
let help = String::from_utf8_lossy(&output.stdout);
Ok(help.contains("--save"))
}
fn scenes() -> &'static [SceneSpec] { fn scenes() -> &'static [SceneSpec] {
&[ &[
SceneSpec { SceneSpec {