Skip to content

Commit

Permalink
feat: add --min --max support
Browse files Browse the repository at this point in the history
  • Loading branch information
ekroon committed Dec 29, 2022
1 parent 50ec76b commit 769673b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ license = "MIT"
[dependencies]
itertools = "^0.10.5"
clap = "^4.0.32"
sparklines = "^0.1.1"
sparklines = "^0.2.0"

[dev-dependencies]
assert_cmd = "2.0.7"
Expand Down
26 changes: 24 additions & 2 deletions src/bin/spark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@ extern crate core;

use std::io::{stdin, Read};

use clap::{arg, Arg, Command};
use clap::{arg, Arg, ArgGroup, Command};
use itertools::Itertools;

fn main() {
let clap_config = Command::new("Spark")
.about("Sparklines for the terminal")
.version(env!("CARGO_PKG_VERSION"))
.arg(arg!(-t --ticks <TICKS> "The characters to use for the sparkline"))
.arg(arg!(--min <MIN> "The minimum value").value_parser(clap::value_parser!(f64)))
.arg(arg!(--max <MAX> "The maximum value").value_parser(clap::value_parser!(f64)))
.arg(
Arg::new("INPUT")
.help("The input to use, space or comma separated")
.allow_hyphen_values(true)
.num_args(0..)
.use_value_delimiter(true),
)
.group(
ArgGroup::new("min_max")
.args(["min", "max"])
.requires("min")
.requires("max")
.multiple(true),
);

let matches = clap_config.get_matches();
Expand All @@ -28,7 +37,20 @@ fn main() {
ticks = Some(default.to_vec());
}
let ticks = ticks.unwrap();
let string_spark = sparklines::StringSpark::new(ticks.as_slice());

let mut min = None;
let mut max = None;
if let Some(input_min) = matches.get_one::<f64>("min") {
min = Some(*input_min);
}
if let Some(input_max) = matches.get_one::<f64>("max") {
max = Some(*input_max);
}
let string_spark = if min.is_some() && max.is_some() {
sparklines::StringSpark::new_with_min_max(&ticks, min.unwrap(), max.unwrap())
} else {
sparklines::StringSpark::new(ticks.as_slice())
};
if let Some(input) = matches.get_many::<String>("INPUT") {
let numbers = input.flat_map(|s| s.parse::<f64>()).collect_vec();
println!("{}", string_spark.spark(&numbers))
Expand Down

0 comments on commit 769673b

Please sign in to comment.