Did stuff.

This commit is contained in:
2017-02-03 10:02:33 +01:00
parent 0e1e59476b
commit f815b510b1
3 changed files with 62 additions and 36 deletions

17
Cargo.lock generated
View File

@@ -2,16 +2,11 @@
name = "loldog" 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)",
"getopts 0.2.14 (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.15 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
"termion 1.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "ansi_term"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "getopts" name = "getopts"
version = "0.2.14" version = "0.2.14"
@@ -30,8 +25,16 @@ dependencies = [
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "termion"
version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata] [metadata]
"checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"
"checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685" "checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685"
"checksum libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)" = "684f330624d8c3784fb9558ca46c4ce488073a8d22450415c5eb4f4cfb0d11b5" "checksum libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)" = "684f330624d8c3784fb9558ca46c4ce488073a8d22450415c5eb4f4cfb0d11b5"
"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d" "checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"
"checksum termion 1.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "9a90df1ede85b25f0bbb50b6313f8fab00e30b3b251f1cc2165faa40924e1417"

View File

@@ -6,4 +6,7 @@ authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
[dependencies] [dependencies]
getopts = "0.2" getopts = "0.2"
rand = "0.3" rand = "0.3"
ansi_term = "0.9" termion = "1.1"
[profile.release]
lto = true

View File

@@ -1,18 +1,28 @@
extern crate getopts; extern crate getopts;
extern crate rand; extern crate rand;
extern crate ansi_term; extern crate termion;
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 std::env;
use std::str;
use getopts::Options; use getopts::Options;
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
use ansi_term::Colour::RGB; use termion::{color, style};
#[inline]
fn rainbow(frec: f32, i: f32) -> (u8, u8, u8) {
let red = (f32::sin(frec * i) * 127.0 + 128.0) as u8;
let green = (f32::sin(frec * i + 2.0 * PI / 3.0) * 127.0 + 128.0) as u8;
let blue = (f32::sin(frec * i + 4.0 * PI / 3.0) * 127.0 + 128.0) as u8;
(red, green, blue)
}
fn print_usage(program: &str, opts: Options) { fn print_usage(program: &str, opts: Options) {
let brief = format!("Usage: {} [OPTION]... [FILE]...", program); let brief = format!("Usage: {} [OPTION]... [FILE]...", program);
@@ -43,7 +53,7 @@ fn main() {
return; return;
} }
// get spread. // Get spread.
// TODO this is nasty... fix this. // TODO this is nasty... fix this.
let spread: f32 = match matches.opt_default("spread", "3.0") { let spread: f32 = match matches.opt_default("spread", "3.0") {
Some(s) => { Some(s) => {
@@ -55,7 +65,7 @@ fn main() {
None => 3.0, None => 3.0,
}; };
// get freq. // Get freq.
// TODO this is nasty... fix this. // TODO this is nasty... fix this.
let freq: f32 = match matches.opt_default("freq", "0.1") { let freq: f32 = match matches.opt_default("freq", "0.1") {
Some(s) => { Some(s) => {
@@ -67,7 +77,7 @@ fn main() {
None => 0.1, None => 0.1,
}; };
// get seed. // Get seed.
// TODO this is nasty... fix this. // TODO this is nasty... fix this.
let mut seed: i32 = match matches.opt_default("seed", "0") { let mut seed: i32 = match matches.opt_default("seed", "0") {
Some(s) => { Some(s) => {
@@ -79,31 +89,41 @@ fn main() {
None => thread_rng().gen_range(0, 256), None => thread_rng().gen_range(0, 256),
}; };
let stdin = io::stdin(); let mut stdin = io::stdin();
let mut buffer = [0u8; 1024];
let mut n = 0;
// TODO I want to use chars() instead, but it's unstable right now :( while let Ok(count) = stdin.read(&mut buffer) {
for line in stdin.lock().lines() { if count == 0 {
seed += 1; break;
let line = line.unwrap()
.trim()
.replace("\t", " ");
for (n, c) in line.chars().enumerate() {
let (red, green, blue) = rainbow(freq, seed as f32 + n as f32 / spread);
print!("{}", RGB(red, green, blue).paint(c.to_string()));
} }
println!(); let string = match str::from_utf8(&buffer[..count]) {
Ok(string) => string,
Err(_) => panic!("oh crap"),
};
for c in string.chars() {
match c {
'\n' => {
seed += 1;
n = 0;
println!();
}
'\t' => {
n += 8;
print!("\t");
}
c @ _ => {
n += 1;
let (red, green, blue) = rainbow(freq, seed as f32 + n as f32 / spread);
print!("{}{}", color::Fg(color::Rgb(red, green, blue)), c);
}
}
}
} }
} }
#[inline]
fn rainbow(frec: f32, i: f32) -> (u8, u8, u8) {
let red = (f32::sin(frec * i) * 127.0 + 128.0) as u8;
let green = (f32::sin(frec * i + 2.0 * PI / 3.0) * 127.0 + 128.0) as u8;
let blue = (f32::sin(frec * i + 4.0 * PI / 3.0) * 127.0 + 128.0) as u8;
(red, green, blue)
}