aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoromagdy7 <omar.professional8777@gmail.com>2022-10-27 23:22:07 +0200
committeromagdy7 <omar.professional8777@gmail.com>2022-10-27 23:22:07 +0200
commit623557259e6906dbd33a5ebf26c4ff6c4ad7eec8 (patch)
treeb8a67528fb11a54d26195352cfa62bc81b5388e5 /src
parentcbc269c1ca61ef8abc63a9cc4ba3c3344e3731e6 (diff)
downloadgof-rs-623557259e6906dbd33a5ebf26c4ff6c4ad7eec8.tar.xz
gof-rs-623557259e6906dbd33a5ebf26c4ff6c4ad7eec8.zip
Made the screen fullscreen while animating and removed uncessary imports
Diffstat (limited to 'src')
-rw-r--r--src/generation.rs34
-rw-r--r--src/lib.rs1
-rw-r--r--src/main.rs4
-rw-r--r--src/ui.rs49
4 files changed, 33 insertions, 55 deletions
diff --git a/src/generation.rs b/src/generation.rs
index f58bba9..12a06b3 100644
--- a/src/generation.rs
+++ b/src/generation.rs
@@ -1,32 +1,17 @@
-#![allow(unused_imports, unused_variables, unused_mut)]
-
use crate::ui::*;
-use colored::Colorize;
use crossterm::{
- cursor::{Hide, MoveTo, Show},
- event::{poll, Event, KeyCode, KeyEvent},
event::{DisableMouseCapture, EnableMouseCapture},
execute,
- style::Stylize,
- terminal,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use rand::{thread_rng, Rng};
-use std::{
- env,
- error::Error,
- fs::File,
- io::{self, BufRead, BufReader, Write},
- path::PathBuf,
- thread::sleep,
- time::Duration,
-};
+use std::{error::Error, io};
use tui::{
backend::{Backend, CrosstermBackend},
- layout::{Alignment, Constraint, Corner, Direction, Layout, Rect},
+ layout::{Alignment, Rect},
style::{Color, Modifier, Style},
text::{Span, Spans},
- widgets::{Block, Borders, List, ListItem, ListState, Paragraph},
+ widgets::{Block, Borders, Paragraph},
Frame, Terminal,
};
@@ -56,11 +41,11 @@ pub fn render_gen<B: Backend>(f: &mut Frame<B>, chunk: Rect, spans: &Vec<Spans>)
f.render_widget(paragraph, chunk);
}
-pub fn new_gen(chunk: &Rect, app: &mut App) -> Gen {
+pub fn new_gen(app: &mut App) -> Gen {
app.flag_cur = true;
let cells = vec![Cell::Dead, Cell::Dead, Cell::Alive, Cell::Dead, Cell::Alive];
let cols: u16 = 72;
- let rows: u16 = 42;
+ let rows: u16 = 44;
let mut grid: Vec<Vec<Cell>> = Vec::new();
for _ in 0..rows {
let mut row: Vec<Cell> = Vec::new();
@@ -75,11 +60,10 @@ pub fn new_gen(chunk: &Rect, app: &mut App) -> Gen {
pub fn gen_to_spans(gen: &Gen) -> Vec<Spans> {
let mut spans = vec![];
- let alive_cells = vec!["🟥", "🟦", "🟨", "🟪", "🟧", "🟩", "🟫"];
+ // let alive_cells = vec!["🟥", "🟦", "🟨", "🟪", "🟧", "🟩", "🟫"];
for i in 0..gen.len() {
let mut txt = String::new();
for j in 0..gen[0].len() {
- let rand = thread_rng().gen_range(0..10);
match gen[i][j] {
// Cell::Alive => txt.push_str(alive_cells[rand % alive_cells.len()]),
Cell::Alive => txt.push_str("⬜"),
@@ -159,11 +143,8 @@ pub fn next_gen(app: &mut App) -> Gen {
nxt_gen
}
-pub fn gen_from_file(s : &String) -> Gen {
+pub fn gen_from_file(s: &String) -> Gen {
let mut gen = Gen::new();
- // let file = File::open(path).expect("File not found");
- // let reader = BufReader::new(file);
-
for line in s.lines() {
let line = line;
let mut row: Vec<Cell> = vec![];
@@ -188,7 +169,6 @@ pub fn init() -> Result<(), Box<dyn Error>> {
let mut terminal = Terminal::new(backend)?;
// create app and run it
- let tick_rate = Duration::from_millis(250);
let app = App::new();
let res = run_app(&mut terminal, app);
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1 @@
+
diff --git a/src/main.rs b/src/main.rs
index 7b4544b..84e0f7f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,10 +1,6 @@
-#![allow(unused_imports, unused_variables, unused_mut)]
-
use std::error::Error;
mod generation;
-use generation::*;
mod ui;
-use ui::*;
fn main() -> Result<(), Box<dyn Error>> {
generation::init()
diff --git a/src/ui.rs b/src/ui.rs
index 692e3e3..ac9096f 100644
--- a/src/ui.rs
+++ b/src/ui.rs
@@ -1,29 +1,21 @@
-use crossterm::{
- event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
- execute,
- terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
-};
-use std::{
- error::Error,
- io,
- path::{Path, PathBuf},
- thread::sleep,
- time::{Duration, Instant},
-};
+use crate::generation::*;
+use crossterm::event::{self, Event, KeyCode};
+use include_dir::{include_dir, Dir};
+use std::{io, thread::sleep, time::Duration};
use tui::{
- backend::{Backend, CrosstermBackend},
- layout::{Alignment, Constraint, Corner, Direction, Layout, Rect},
+ backend::Backend,
+ layout::{Constraint, Direction, Layout},
style::{Color, Modifier, Style},
- text::{Span, Spans},
- widgets::{Block, Borders, List, ListItem, ListState, Paragraph},
+ text::Spans,
+ widgets::{Block, Borders, List, ListItem, ListState},
Frame, Terminal,
};
-use include_dir::{include_dir, Dir};
-use crate::generation::*;
+// for including the patterns dir in compile time
+static PRESETS_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/presets/patterns/");
-static PRESETS_DIR : Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/presets/patterns/");
+// struct that holds all the information in the left side bar
pub struct StatefulList<T> {
state: ListState,
items: Vec<T>,
@@ -85,6 +77,7 @@ pub struct App {
impl App {
pub fn new() -> App {
+ // read the patterns from the files and load it in a vector
fn read_presets() -> Vec<(String, String)> {
let mut result = Vec::new();
for i in 1..=513 {
@@ -100,14 +93,14 @@ impl App {
flag_cur: false,
layout: Layout::default()
.direction(Direction::Horizontal)
- .constraints([Constraint::Percentage(15), Constraint::Percentage(85)].as_ref()),
+ .constraints([Constraint::Percentage(12), Constraint::Percentage(90)].as_ref()),
cur_gen: Gen::new(),
}
}
}
+// gameloop and event handling
pub fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<()> {
- let mut last_tick = Instant::now();
let mut item_cnt = 1;
loop {
terminal.draw(|f| ui_list(f, &mut app))?;
@@ -115,7 +108,7 @@ pub fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Resu
if crossterm::event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
match key.code {
- KeyCode::Char('q') => return Ok(()),
+ KeyCode::Char('q') | KeyCode::Esc => return Ok(()),
KeyCode::Left | KeyCode::Char('h') => app.items.unselect(),
KeyCode::Down | KeyCode::Char('j') => {
if item_cnt >= app.items.items.len() - 1 {
@@ -139,12 +132,20 @@ pub fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Resu
terminal.draw(|f| ui_game(f, &mut app))?;
}
KeyCode::Char('a') => 'animate: loop {
+ app.layout = Layout::default()
+ .direction(Direction::Horizontal)
+ .constraints([Constraint::Percentage(0), Constraint::Percentage(100)].as_ref());
terminal.draw(|f| ui_game(f, &mut app))?;
sleep(Duration::from_millis(32));
if (crossterm::event::poll(Duration::from_millis(1))).unwrap() {
if let Event::Key(k) = event::read().unwrap() {
match k.code {
- KeyCode::Char('s') => break 'animate,
+ KeyCode::Char('s') =>{
+ break 'animate
+ app.layout = Layout::default()
+ .direction(Direction::Horizontal)
+ .constraints([Constraint::Percentage(12), Constraint::Percentage(90)].as_ref());
+ }
_ => {}
}
}
@@ -182,7 +183,7 @@ fn ui_list<B: Backend>(f: &mut Frame<B>, app: &mut App) {
f.render_stateful_widget(items, chunks[0], &mut app.items.state);
if !app.flag_cur {
- app.cur_gen = new_gen(&chunks[1], app);
+ app.cur_gen = new_gen(app);
}
let spans = gen_to_spans(&app.cur_gen);
render_gen(f, chunks[1], &spans);