Skip to content

Commit

Permalink
Merge pull request #21 from alexwlchan/update-clap
Browse files Browse the repository at this point in the history
Update to the newest version of clap
  • Loading branch information
alexwlchan authored Oct 24, 2022
2 parents c98a8ae + 2836aee commit 4ecb6ec
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 83 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v1.1.7 - 2022-10-24

Some internal refactoring to use newer versions of libraries.

This has no feature changes.

## v1.1.6 - 2022-10-23

Provide precompiled binaries for more targets, so the following targets are now supported:
Expand Down
83 changes: 42 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "dominant_colours"
version = "1.1.6"
version = "1.1.7"
edition = "2018"

[dependencies]
assert_cmd = "2.0.2"
clap = "2.33"
assert_cmd = "2.0.5"
clap = "4.0.18"

[dependencies.kmeans_colors]
version = "0.4.0"
Expand Down
40 changes: 40 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use clap::{Arg, ArgAction, Command};

const VERSION: &str = env!("CARGO_PKG_VERSION");

pub fn app() -> clap::Command {
Command::new("dominant_colours")
.version(VERSION)
.author("Alex Chan <[email protected]>")
.about("Find the dominant colours in an image")
.arg(
Arg::new("PATH")
.help("path to the image to inspect")
.required(true)
.index(1),
)
.arg(
Arg::new("MAX-COLOURS")
.long("max-colours")
.help("how many colours to find")
.value_parser(value_parser!(usize))
.default_value("5"),
)
.arg(
Arg::new("no-palette")
.long("no-palette")
.help("Just print the hex values, not colour previews")
.action(ArgAction::SetTrue),
)
}

#[cfg(test)]
mod tests {
use crate::cli::app;

// See https://github.com/clap-rs/clap/blob/master/CHANGELOG.md#300---2021-12-31
#[test]
fn verify_app() {
app().debug_assert();
}
}
54 changes: 15 additions & 39 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,29 @@
#[macro_use]
extern crate clap;

use clap::{App, Arg};
use kmeans_colors::get_kmeans_hamerly;
use palette::{Lab, Pixel, Srgb, Srgba};

mod cli;
mod get_bytes;

const VERSION: &str = env!("CARGO_PKG_VERSION");

fn main() {
let matches = App::new("dominant_colours")
.version(VERSION)
.author("Alex Chan <[email protected]>")
.about("Find the dominant colours in an image")
.arg(
Arg::with_name("PATH")
.help("path to the image to inspect")
.required(true)
.index(1),
)
.arg(
Arg::with_name("MAX-COLOURS")
.long("max-colours")
.help("how many colours to find")
.default_value("5")
.takes_value(true),
)
.arg(
Arg::with_name("no-palette")
.long("no-palette")
.help("Just print the hex values, not colour previews")
.takes_value(false),
)
.get_matches();

// This .unwrap() is safe because "path" is a required param
let path = matches.value_of("PATH").unwrap();

// Get the max colours as a number.
// See https://github.com/clap-rs/clap/blob/v2.33.1/examples/12_typed_values.rs
let max_colours = value_t!(matches, "MAX-COLOURS", usize).unwrap_or_else(|e| e.exit());
let matches = cli::app().get_matches();

let path = matches
.get_one::<String>("PATH")
.expect("`path` is required");

let max_colours: usize = *matches
.get_one::<usize>("MAX-COLOURS")
.expect("`max-colours` is required");

// There's different code for fetching bytes from GIF images because
// GIFs are often animated, and we want a selection of frames.
let img_bytes = if path.to_lowercase().ends_with(".gif") {
get_bytes::get_bytes_for_gif(path)
get_bytes::get_bytes_for_gif(&path)
} else {
get_bytes::get_bytes_for_image(path)
get_bytes::get_bytes_for_image(&path)
};

// This is based on code from the kmeans-colors binary, but with a bunch of
Expand Down Expand Up @@ -79,7 +55,7 @@ fn main() {
for c in rgb {
let display_value = format!("#{:02x}{:02x}{:02x}", c.red, c.green, c.blue);

if matches.is_present("no-palette") {
if matches.get_flag("no-palette") {
println!("{}", display_value);
} else {
println!(
Expand Down Expand Up @@ -213,11 +189,11 @@ mod tests {
fn it_fails_if_you_pass_an_invalid_max_colours() {
let output = get_failure(&["./src/tests/red.png", "--max-colours=NaN"]);

assert_eq!(output.exit_code, 1);
assert_eq!(output.exit_code, 2);
assert_eq!(output.stdout, "");
assert_eq!(
output.stderr,
"error: Invalid value: The argument 'NaN' isn't a valid value\n"
"error: Invalid value 'NaN' for '--max-colours <MAX-COLOURS>': invalid digit found in string\n\nFor more information try '--help'\n"
);
}

Expand Down

0 comments on commit 4ecb6ec

Please sign in to comment.