Skip to content

Commit

Permalink
Placed all user input into its own function in its own file.
Browse files Browse the repository at this point in the history
  • Loading branch information
cowboy8625 committed Oct 21, 2021
1 parent ce4fb88 commit b0c927a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
4 changes: 3 additions & 1 deletion src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ pub fn gen_charater_vecs(width: usize, height: u16, group: &Characters) -> Vec<V
ch
}

pub fn gen_color_function(shading: bool) -> fn(style::Color, style::Color, u8) -> Vec<style::Color> {
pub fn gen_color_function(
shading: bool,
) -> fn(style::Color, style::Color, u8) -> Vec<style::Color> {
// This Creates a closure off of the args
// given to the program at start that will crates the colors for the rain
match shading {
Expand Down
30 changes: 6 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ mod gen;
mod rain;
mod term;
mod update;
mod user_input;
mod user_settings;

// None Standard Crates
use crossterm::{cursor, event, execute, queue, style, terminal, Result};
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::time::Duration;

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

const MAXSPEED: u64 = 40;
Expand All @@ -44,32 +45,13 @@ fn main() -> Result<()> {
let create_color = gen_color_function(user_settings.shading);

let mut rain = Rain::new(create_color, width, height, &user_settings);
let mut is_running = true;

terminal::enable_raw_mode()?;
execute!(stdout, terminal::EnterAlternateScreen, cursor::Hide)?;

loop {
if event::poll(Duration::from_millis(50))? {
match event::read()? {
event::Event::Key(keyevent) => {
if keyevent
== event::KeyEvent::new(
event::KeyCode::Char('c'),
event::KeyModifiers::CONTROL,
)
|| keyevent
== event::KeyEvent::new(event::KeyCode::Esc, event::KeyModifiers::NONE)
{
break;
}
}
event::Event::Resize(w, h) => {
clear(&mut stdout)?;
rain = Rain::new(create_color, w, h, &user_settings);
}
_ => {}
}
}
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()?;
Expand Down
30 changes: 30 additions & 0 deletions src/user_input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::{clear, Rain, Result, UserSettings};
use crossterm::{event, style};
use std::io::{BufWriter, Stdout};
use std::time::Duration;
pub fn user_input(
stdout: &mut BufWriter<Stdout>,
rain: &mut Rain,
user_settings: &UserSettings,
create_color: fn(style::Color, style::Color, u8) -> Vec<style::Color>,
) -> Result<bool> {
if event::poll(Duration::from_millis(50))? {
match event::read()? {
event::Event::Key(keyevent) => {
if keyevent
== event::KeyEvent::new(event::KeyCode::Char('c'), event::KeyModifiers::CONTROL)
|| keyevent
== event::KeyEvent::new(event::KeyCode::Esc, event::KeyModifiers::NONE)
{
return Ok(false);
}
}
event::Event::Resize(w, h) => {
clear(stdout)?;
*rain = Rain::new(create_color, w, h, &user_settings);
}
_ => {}
}
}
Ok(true)
}

0 comments on commit b0c927a

Please sign in to comment.