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

6
Cargo.lock generated
View File

@@ -3,6 +3,7 @@ name = "loldog"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
@@ -11,6 +12,11 @@ name = "ansi_term"
version = "0.9.0" version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "getopts"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.15" version = "0.2.15"

View File

@@ -4,5 +4,6 @@ version = "0.1.0"
authors = ["logaritmisk <anders.e.olsson@gmail.com>"] authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
[dependencies] [dependencies]
getopts = "0.2"
rand = "0.3" rand = "0.3"
ansi_term = "0.9" ansi_term = "0.9"

View File

@@ -1,20 +1,84 @@
extern crate getopts;
extern crate rand; extern crate rand;
extern crate ansi_term; extern crate ansi_term;
use std::io; use std::io;
use std::io::prelude::*; use std::io::prelude::*;
use std::f32::consts::PI; use std::f32::consts::PI;
use std::env;
use getopts::Options;
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
use ansi_term::Colour::RGB; 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() { 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(); let stdin = io::stdin();
for line in stdin.lock().lines() { for line in stdin.lock().lines() {
s += 1.0; seed += 1;
let mut d = 0; let mut d = 0;
@@ -24,7 +88,7 @@ fn main() {
_ => 1 _ => 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())); print!("{}", RGB(red, green, blue).paint(c.to_string()));
} }