Allow to specify spread, freq, and seed as arguments.

This commit is contained in:
2016-09-02 08:17:40 +02:00
parent 60413e0431
commit 78d050a3b4
3 changed files with 74 additions and 3 deletions

View File

@@ -1,20 +1,84 @@
extern crate getopts;
extern crate rand;
extern crate ansi_term;
use std::io;
use std::io::prelude::*;
use std::f32::consts::PI;
use std::env;
use getopts::Options;
use rand::{thread_rng, Rng};
use ansi_term::Colour::RGB;
fn print_usage(program: &str, opts: Options) {
let brief = format!("Usage: {} [OPTION]... [FILE]...", program);
print!("{}", opts.usage(&brief));
}
fn main() {
let mut s = 1.0; // thread_rng().gen_range(0.0, 256.0);
let args: Vec<String> = env::args().collect();
let program = args[0].clone();
let mut opts = Options::new();
opts.optopt("p", "spread", "Rainbow spread", "<f>");
opts.optopt("F", "freq", "Rainbow frequency", "<f>");
opts.optopt("S", "seed", "Rainbow seed, 0 = random", "<i>");
opts.optflag("h", "help", "Show this message");
let matches = match opts.parse(&args[1..]) {
Ok(m) => { m }
Err(f) => { panic!(f.to_string()) }
};
if matches.opt_present("h") {
print_usage(&program, opts);
return;
}
// @todo this is nasty... fix this.
let spread : f32 = match matches.opt_default("spread", "3.0") {
Some(s) => {
match s.parse() {
Ok(0.0) => 3.0,
Ok(n) => n,
_ => 3.0
}
},
None => 3.0
};
// @todo this is nasty... fix this.
let freq : f32 = match matches.opt_default("freq", "0.1") {
Some(s) => {
match s.parse() {
Ok(0.0) => 0.1,
Ok(n) => n,
_ => 0.1
}
},
None => 0.1
};
// @todo this is nasty... fix this.
let mut seed : i32 = match matches.opt_default("seed", "0") {
Some(s) => {
match s.parse() {
Ok(0) => thread_rng().gen_range(0, 256),
Ok(n) => n,
_ => thread_rng().gen_range(0, 256)
}
},
None => thread_rng().gen_range(0, 256)
};
let stdin = io::stdin();
for line in stdin.lock().lines() {
s += 1.0;
seed += 1;
let mut d = 0;
@@ -24,7 +88,7 @@ fn main() {
_ => 1
};
let (red, green, blue) = rainbow(0.1, (s + d as f32) / 3.0);
let (red, green, blue) = rainbow(freq, (seed + d) as f32 / spread);
print!("{}", RGB(red, green, blue).paint(c.to_string()));
}