From 6a2379ce7817200eec5209452e28da5d3bf77f2d Mon Sep 17 00:00:00 2001 From: Filip Czaplicki Date: Sat, 6 Dec 2025 16:42:41 +0100 Subject: [PATCH] Add colors to --help/-h (#745) ~I used [clap-cargo](https://github.com/crate-ci/clap-cargo) as it's easy to use and provides sensible default styles.~ User can disable colors via `NO_COLOR=1` environment variable Output of `oxipng --help`: | before | after | `NO_COLOR=1` | | - | - | - | | oxipng-before | oxipng-colors | oxipng-no-color | --- src/cli.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/cli.rs b/src/cli.rs index 21132fe6..6c1397e0 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,10 +1,18 @@ use std::{num::NonZeroU64, path::PathBuf}; +use clap::builder::Styles; +use clap::builder::styling::{AnsiColor, Effects}; use clap::{Arg, ArgAction, Command, builder::ArgPredicate, value_parser}; use parse_size::parse_size; include!("display_chunks.rs"); +const STYLES: Styles = Styles::styled() + .header(AnsiColor::Green.on_default().effects(Effects::BOLD)) + .usage(AnsiColor::Green.on_default().effects(Effects::BOLD)) + .literal(AnsiColor::Cyan.on_default().effects(Effects::BOLD)) + .placeholder(AnsiColor::Cyan.on_default()); + pub fn build_command() -> Command { // Note: clap 'wrap_help' is enabled to automatically wrap lines according to terminal width. // To keep things tidy though, short help descriptions should be no more than 54 characters, @@ -15,6 +23,7 @@ pub fn build_command() -> Command { .version(env!("CARGO_PKG_VERSION")) .author("Joshua Holmer ") .about("Losslessly improve compression of PNG files") + .styles(STYLES) .arg( Arg::new("files") .help("File(s) to compress (use '-' for stdin)")