Skip to content

Commit

Permalink
code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
cowboy8625 committed Dec 8, 2021
1 parent b0c927a commit 22e2653
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 56 deletions.
29 changes: 14 additions & 15 deletions src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ use crate::{style, thread_rng, Characters, Rng};
use std::char;
use std::time::{Duration, Instant};

/// Generates a single column of Characters.
pub fn create_drop_chars(height: u16, group: &Characters) -> Vec<char> {
let g = group.as_vec_u32();
(0..height + 1)
.map(|_| char::from_u32(g[thread_rng().gen_range(0..g.len())]).unwrap_or('#'))
.collect()
}

/// Generates all Characters in columns.
pub fn gen_charater_vecs(width: usize, height: u16, group: &Characters) -> Vec<Vec<char>> {
let mut ch = Vec::new();
for _ in 0..width {
ch.push(create_drop_chars(height, group));
}
ch
(0..width)
.map(|_| create_drop_chars(height, group))
.collect()
}

/// Generates the color function on startup to remove branching if statements from code.
pub fn gen_color_function(
shading: bool,
) -> fn(style::Color, style::Color, u8) -> Vec<style::Color> {
Expand Down Expand Up @@ -53,25 +54,23 @@ pub fn gen_color_function(
}
}

// TODO: I feel like slowest and fastest are labeled wrong.........
/// Generates Timing for rain to fall. AKA the speed of the rain fall.
pub fn gen_times(width: usize, (slowest, fastest): (u64, u64)) -> Vec<(Instant, Duration)> {
let now = Instant::now();
let mut times = Vec::new();
let mut rng = thread_rng();
for _ in 0..width {
times.push((now, Duration::from_millis(rng.gen_range(slowest..fastest))));
}
times
(0..width)
.map(|_| (now, Duration::from_millis(rng.gen_range(slowest..fastest))))
.collect()
}

/// Generates the length of each column.
pub fn gen_lengths(width: usize, height: usize) -> Vec<usize> {
let mut len = Vec::new();
let mut rng = thread_rng();
for _ in 0..width {
len.push(rng.gen_range(4..height - 10));
}
len
(0..width).map(|_| rng.gen_range(4..height - 10)).collect()
}

/// Uses Generates function to create all the color of the Rain/Characters.
pub fn gen_colors<F: Fn(style::Color, style::Color, u8) -> Vec<style::Color>>(
create_color: F,
head: (u8, u8, u8),
Expand Down
13 changes: 6 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crossterm::{cursor, execute, queue, style, terminal, Result};
use rand::{thread_rng, Rng};

// Standard Library Crates
use std::io::{stdout, BufWriter, Stdout, Write};
use std::io::{stdout, Stdout, Write};

// Modules
use arguments::cargs;
Expand All @@ -22,7 +22,7 @@ use gen::{
};
use rain::Rain;
use term::{clear, draw};
use update::{reset, update_locations, update_queue};
use update::{reset, update};
use user_input::user_input;
use user_settings::UserSettings;

Expand All @@ -37,10 +37,10 @@ Email: cowboy8625@protonmail.com
";

fn main() -> Result<()> {
let mut stdout = BufWriter::with_capacity(8_192, stdout());
let mut stdout = stdout();
let user_settings = cargs();
let (width, height) = terminal::size()?;
let h = height as usize;
// let h = height as usize;

let create_color = gen_color_function(user_settings.shading);

Expand All @@ -52,11 +52,10 @@ fn main() -> Result<()> {

while is_running {
is_running = user_input(&mut stdout, &mut rain, &user_settings, create_color)?;
update_queue(&mut rain);
draw(&mut stdout, &rain, user_settings.group.width())?;
stdout.flush()?;
update_locations(&mut rain);
reset(create_color, &mut rain, &user_settings, h);
update(&mut rain);
reset(create_color, &mut rain, &user_settings);
}

execute!(stdout, cursor::Show, terminal::LeaveAlternateScreen)?;
Expand Down
31 changes: 16 additions & 15 deletions src/term.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
use crate::{cursor, queue, style, terminal, BufWriter, Rain, Result, Stdout};
use crate::{cursor, queue, style, terminal, Rain, Result, Stdout};

pub fn clear(w: &mut BufWriter<Stdout>) -> Result<()> {
pub fn clear(w: &mut Stdout) -> Result<()> {
queue!(w, terminal::Clear(terminal::ClearType::All))?;
Ok(())
}

pub fn draw(w: &mut BufWriter<Stdout>, rain: &Rain, spacing: u16) -> Result<()> {
let (mut chr, mut loc, mut len, mut clr);
// TODO: Clean this crap up
pub fn draw(w: &mut Stdout, rain: &Rain, spacing: u16) -> Result<()> {
let (mut chr, mut col, mut len, mut clr);
let height = rain.height();
for x in rain.queue.iter() {
chr = &rain.charaters[*x];
loc = &rain.locations[*x];
len = &rain.length[*x];
clr = &rain.colors[*x];
for row in rain.queue.iter() {
chr = &rain.charaters[*row];
col = &rain.locations[*row];
len = &rain.length[*row];
clr = &rain.colors[*row];

let start = loc.saturating_sub(*len).clamp(0, chr.len());
let end = (loc + 1).clamp(1, chr.len());
let start = col.saturating_sub(*len).clamp(0, chr.len());
let end = (col + 1).clamp(1, chr.len());
let slice = chr[start..end].iter();

let cstart = if loc > len {
let cstart = if col > len {
clr.len() - slice.len()
} else {
0
Expand All @@ -29,15 +30,15 @@ pub fn draw(w: &mut BufWriter<Stdout>, rain: &Rain, spacing: u16) -> Result<()>
for (y, ch) in slice.rev().enumerate() {
queue!(
w,
cursor::MoveTo(*x as u16 * spacing, (*loc.min(&height) - y) as u16),
cursor::MoveTo(*row as u16 * spacing, (*col.min(&height) - y) as u16),
style::SetForegroundColor(color[y]),
style::Print(ch),
)?;
}
if loc >= len {
if col >= len {
queue!(
w,
cursor::MoveTo(*x as u16 * spacing, loc.saturating_sub(*len) as u16),
cursor::MoveTo(*row as u16 * spacing, col.saturating_sub(*len) as u16),
style::Print(" ".repeat(spacing as usize)),
)?;
}
Expand Down
28 changes: 11 additions & 17 deletions src/update.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
use crate::{create_drop_chars, style, thread_rng, Rain, Rng, UserSettings};
use itertools::izip;
use std::time::{Duration, Instant};

pub fn update_queue(rain: &mut Rain) {
pub fn update(rain: &mut Rain) {
rain.queue.clear();
let now = Instant::now();
for (i, (t, d)) in rain.time.iter_mut().enumerate() {
if *t <= now {
*t += *d;
rain.queue.push(i);
for (idx, ((time, delay), location)) in izip!(&mut rain.time, &mut rain.locations).enumerate() {
if *time <= now {
*time += *delay;
*location += 1;
rain.queue.push(idx);
}
}
}

pub fn update_locations(rain: &mut Rain) {
let queue = &rain.queue;
for i in queue.iter() {
rain.locations[*i] += 1;
}
}

pub fn reset<F>(create_color: F, rain: &mut Rain, us: &UserSettings, height: usize)
pub fn reset<F>(create_color: F, rain: &mut Rain, us: &UserSettings)
where
F: Fn(style::Color, style::Color, u8) -> Vec<style::Color>,
{
// assert_eq!(height, rain.height());
let mut rng = thread_rng();
let h16 = height as u16;
let h16 = rain.height() as u16;
let now = Instant::now();
for i in rain.queue.iter() {
if rain.locations[*i] > height + rain.length[*i] {
if rain.locations[*i] > rain.height() + rain.length[*i] {
rain.charaters[*i] = create_drop_chars(h16, &us.group);
rain.locations[*i] = 0;
rain.length[*i] = rng.gen_range(4..height - 10);
rain.length[*i] = rng.gen_range(4..rain.height() - 10);
rain.colors[*i] = create_color(
us.rain_color.into(),
us.head_color.into(),
Expand Down
4 changes: 2 additions & 2 deletions src/user_input.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{clear, Rain, Result, UserSettings};
use crossterm::{event, style};
use std::io::{BufWriter, Stdout};
use std::io::Stdout;
use std::time::Duration;
pub fn user_input(
stdout: &mut BufWriter<Stdout>,
stdout: &mut Stdout,
rain: &mut Rain,
user_settings: &UserSettings,
create_color: fn(style::Color, style::Color, u8) -> Vec<style::Color>,
Expand Down

0 comments on commit 22e2653

Please sign in to comment.