Browse Source

Basic functionality

tags/v0.1.0
Stephen 6 months ago
commit
53e9cc01c6
6 changed files with 226 additions and 0 deletions
  1. +2
    -0
      .gitignore
  2. +144
    -0
      Cargo.lock
  3. +11
    -0
      Cargo.toml
  4. +13
    -0
      README.md
  5. +33
    -0
      src/lol.rs
  6. +23
    -0
      src/main.rs

+ 2
- 0
.gitignore View File

@ -0,0 +1,2 @@
/target
.idea

+ 144
- 0
Cargo.lock View File

@ -0,0 +1,144 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "colored"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd"
dependencies = [
"atty",
"lazy_static",
"winapi",
]
[[package]]
name = "getrandom"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "hermit-abi"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c30f6d0bc6b00693347368a67d41b58f2fb851215ff1da49e90fe2c5c667151"
dependencies = [
"libc",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2f96b10ec2560088a8e76961b00d47107b3a625fecb76dedb29ee7ccbf98235"
[[package]]
name = "lolcatrs"
version = "0.1.0"
dependencies = [
"colored",
"rand",
]
[[package]]
name = "ppv-lite86"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20"
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom",
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core",
]
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

+ 11
- 0
Cargo.toml View File

@ -0,0 +1,11 @@
[package]
name = "lolcatrs"
version = "0.1.0"
authors = ["Stephen <webmaster@scd31.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
colored = "2.0"
rand = "0.7"

+ 13
- 0
README.md View File

@ -0,0 +1,13 @@
# Lolcat.rs
Why?
```
$ time echo hi | lolcat
real 0m0.422s
user 0m0.393s
sys 0m0.028s
```
I use lolcat in my `.bashrc` file, so this amount of time is not ideal for me.

+ 33
- 0
src/lol.rs View File

@ -0,0 +1,33 @@
use colored::{ColoredString, Colorize};
use std::f64::consts;
use std::io::{stdout, Write};
use std::process::exit;
fn rainbow(freq: f64, i: f64) -> (u8, u8, u8) {
let red = ((freq * i).sin() * 127.0) as u8 + 128;
let green = ((freq * i + 2.0 * consts::PI / 3.0).sin() * 127.0) as i16 + 128;
let blue = ((freq * i + 4.0 * consts::PI / 3.0).sin() * 127.0) as i16 + 128;
(red as u8, green as u8, blue as u8)
}
pub fn print_rainbow(line: &str, freq: f64, seed: f64, spread: f64, invert: bool) {
let t = line
.chars()
.enumerate()
.map(|(i, c)| {
let (r, g, b) = rainbow(freq, seed + i as f64 / spread);
if invert {
if c == '\n' || c == '\r' {
c.to_string().normal()
} else {
c.to_string().on_truecolor(r, g, b)
}
} else {
c.to_string().truecolor(r, g, b)
}
})
.fold("".to_string(), |acc, v| format!("{}{}", acc, v));
if stdout().write_all(t.as_bytes()).is_err() {
exit(0);
}
}

+ 23
- 0
src/main.rs View File

@ -0,0 +1,23 @@
use std::io;
use std::process::exit;
mod lol;
fn read_line() -> Option<(String, usize)> {
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(n) => Some((input, n)),
_ => None,
}
}
fn main() {
let mut seed: f64 = rand::random::<f64>() * 1_000_000.0;
while let Some((x, n)) = read_line() {
lol::print_rainbow(&x, 0.1, seed, 3.0, true);
if n == 0 {
// EOF
exit(0);
}
seed += 1.0;
}
}

Loading…
Cancel
Save