Skip to content

Commit

Permalink
misc: Reimplement coloring in Clap v4 (or as much as we can)
Browse files Browse the repository at this point in the history
  • Loading branch information
amyspark authored and esden committed Aug 25, 2023
1 parent 682cfd6 commit 5b0c955
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ vendored = ["rusb/vendored"]
default = ["detect-backtrace", "vendored"]

[dependencies]
clap = { version = "4.0", default-features = false, features = ["std", "color", "help", "usage"] }
anstyle = "1.0.2"
clap = { version = "4.0", default-features = false, features = ["std", "color", "help", "usage", "unicode", "wrap_help", "unstable-styles"] }
env_logger = "0.10"
dfu-core = { version = "0.6.0", features = ["std"] }
dfu-libusb = "0.5.1"
Expand Down
23 changes: 23 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use std::io::Read;
use std::str::FromStr;
use std::time::Duration;

use anstyle;
use clap::ArgAction;
use clap::builder::styling::Styles;
use clap::{Command, Arg, ArgMatches};
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
use indicatif::{ProgressBar, ProgressStyle};
Expand Down Expand Up @@ -265,6 +267,25 @@ fn info_command(matches: &ArgMatches) -> Result<(), Error>
Ok(())
}

/// Clap v3 style (approximate)
/// See https://stackoverflow.com/a/75343828
fn style() -> clap::builder::Styles {
Styles::styled()
.usage(
anstyle::Style::new()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Yellow)))
.bold(),
)
.header(
anstyle::Style::new()
.bold()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Yellow))),
)
.literal(
anstyle::Style::new().fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Green))),
)
}

fn main()
{
env_logger::Builder::new()
Expand All @@ -285,6 +306,8 @@ fn main()
);
}
parser = parser
.styles(style())
.disable_colored_help(false)
.arg_required_else_help(true)
.arg(Arg::new("serial_number")
.short('s')
Expand Down

0 comments on commit 5b0c955

Please sign in to comment.