From f815b510b10a371f06b18177e62e50d55f68482b Mon Sep 17 00:00:00 2001 From: logaritmisk Date: Fri, 3 Feb 2017 10:02:33 +0100 Subject: [PATCH] Did stuff. --- Cargo.lock | 17 +++++++----- Cargo.toml | 5 +++- src/main.rs | 76 +++++++++++++++++++++++++++++++++-------------------- 3 files changed, 62 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 27ba727..6a76403 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,16 +2,11 @@ name = "loldog" version = "0.1.0" 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)", "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]] name = "getopts" version = "0.2.14" @@ -30,8 +25,16 @@ dependencies = [ "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] -"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 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 termion 1.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "9a90df1ede85b25f0bbb50b6313f8fab00e30b3b251f1cc2165faa40924e1417" diff --git a/Cargo.toml b/Cargo.toml index 005a9a2..13c62ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,7 @@ authors = ["logaritmisk "] [dependencies] getopts = "0.2" rand = "0.3" -ansi_term = "0.9" +termion = "1.1" + +[profile.release] +lto = true diff --git a/src/main.rs b/src/main.rs index 12e3f86..0798623 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,28 @@ extern crate getopts; extern crate rand; -extern crate ansi_term; +extern crate termion; use std::io; use std::io::prelude::*; use std::f32::consts::PI; use std::env; +use std::str; use getopts::Options; 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) { let brief = format!("Usage: {} [OPTION]... [FILE]...", program); @@ -43,7 +53,7 @@ fn main() { return; } - // get spread. + // Get spread. // TODO this is nasty... fix this. let spread: f32 = match matches.opt_default("spread", "3.0") { Some(s) => { @@ -55,7 +65,7 @@ fn main() { None => 3.0, }; - // get freq. + // Get freq. // TODO this is nasty... fix this. let freq: f32 = match matches.opt_default("freq", "0.1") { Some(s) => { @@ -67,7 +77,7 @@ fn main() { None => 0.1, }; - // get seed. + // Get seed. // TODO this is nasty... fix this. let mut seed: i32 = match matches.opt_default("seed", "0") { Some(s) => { @@ -79,31 +89,41 @@ fn main() { 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 :( - for line in stdin.lock().lines() { - seed += 1; - - 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())); + while let Ok(count) = stdin.read(&mut buffer) { + if count == 0 { + break; } - 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) -}