diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/generation.rs | 44 | ||||
| -rw-r--r-- | src/main.rs | 2 | ||||
| -rw-r--r-- | src/patterns.rs | 2638 | ||||
| -rw-r--r-- | src/ui.rs | 53 |
4 files changed, 2673 insertions, 64 deletions
diff --git a/src/generation.rs b/src/generation.rs index 249754d..4031c46 100644 --- a/src/generation.rs +++ b/src/generation.rs @@ -38,24 +38,29 @@ pub enum Cell { Dead, } -pub fn render_gen(chunk: &Rect, gen: &Gen) { - for i in 0..chunk.height as usize { - for j in 0..chunk.width as usize { - match gen[i][j] { - // Cell::Alive => print!("😎"), - // Cell::Alive => print!("🦀"), - Cell::Alive => print!("{}", "X".color("blue")), - Cell::Dead => print!("{}", "-".color("red")), - } - } - } +pub fn render_gen<B: Backend>(f: &mut Frame<B>, chunk: Rect, spans: &Vec<Spans>) { + let create_block = |title| { + Block::default() + .borders(Borders::ALL) + .style(Style::default().bg(Color::Black).fg(Color::Red)) + .title(Span::styled( + title, + Style::default().add_modifier(Modifier::BOLD), + )) + .title_alignment(Alignment::Center) + }; + let paragraph = Paragraph::new(spans.clone()) + .style(Style::default().bg(Color::Black).fg(Color::Blue)) + .block(create_block(" Conway's Game-Of-Life ")) + .alignment(Alignment::Center); + f.render_widget(paragraph, chunk); } pub fn new_gen(chunk: &Rect, app: &mut App) -> Gen { app.flag_cur = true; let cells = vec![Cell::Dead, Cell::Dead, Cell::Alive, Cell::Dead, Cell::Alive]; - let cols: u16 = chunk.width - 90; - let rows: u16 = chunk.height - 2; + let cols: u16 = 72; + let rows: u16 = 42; let mut grid: Vec<Vec<Cell>> = Vec::new(); for _ in 0..rows { let mut row: Vec<Cell> = Vec::new(); @@ -70,19 +75,18 @@ 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("⬜"), + // Cell::Dead => txt.push_str("⬛️"), + Cell::Dead => txt.push_str(" "), // Cell::Alive => print!("😎"), // Cell::Alive => txt.push_str("🦀"), - // Cell::Alive => txt.push_str(alive_cells[rand % alive_cells.len()]), - Cell::Alive => txt.push_str(alive_cells[0]), - Cell::Dead => txt.push_str("⬛️"), - // Cell::Alive => txt.push('X'), - // Cell::Dead => txt.push('-'), } } spans.push(Spans::from(txt)); @@ -186,7 +190,7 @@ pub fn init() -> Result<(), Box<dyn Error>> { // create app and run it let tick_rate = Duration::from_millis(250); let app = App::new(); - let res = run_app(&mut terminal, app, tick_rate); + let res = run_app(&mut terminal, app); // restore terminal disable_raw_mode()?; diff --git a/src/main.rs b/src/main.rs index 7b4544b..cd1b603 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,8 @@ use std::error::Error; mod generation; +mod patterns; +use patterns::*; use generation::*; mod ui; use ui::*; diff --git a/src/patterns.rs b/src/patterns.rs new file mode 100644 index 0000000..8a6f224 --- /dev/null +++ b/src/patterns.rs @@ -0,0 +1,2638 @@ +// use std::path::Path; +// use crate::generation::*; +// use tui::{ +// backend::{Backend, CrosstermBackend}, +// layout::{Alignment, Constraint, Corner, Direction, Layout, Rect}, +// style::{Color, Modifier, Style}, +// text::{Span, Spans}, +// widgets::{Block, Borders, List, ListItem, ListState, Paragraph}, +// Frame, Terminal, +// }; +// +// +// pub struct StatefulList<T> { +// pub state: ListState, +// pub items: Vec<T>, +// } +// +// impl<T> StatefulList<T> { +// fn with_items(items: Vec<T>) -> StatefulList<T> { +// StatefulList { +// state: ListState::default(), +// items, +// } +// } +// +// pub fn next(&mut self) { +// let i = match self.state.selected() { +// Some(i) => { +// if i >= self.items.len() - 1 { +// 0 +// } else { +// i + 1 +// } +// } +// None => 0, +// }; +// self.state.select(Some(i)); +// } +// +// pub fn previous(&mut self) { +// let i = match self.state.selected() { +// Some(i) => { +// if i == 0 { +// self.items.len() - 1 +// } else { +// i - 1 +// } +// } +// None => 0, +// }; +// self.state.select(Some(i)); +// } +// +// pub fn unselect(&mut self) { +// self.state.select(None); +// } +// } +// +// pub struct App<'a> { +// pub items: StatefulList<(&'a str, &'a Path)>, +// pub flag_cur: bool, +// pub layout: Layout, +// pub cur_gen: Gen, +// } +// +// impl<'a> App<'a> { +// pub fn new() -> App<'a> { +// App { +// items: StatefulList::with_items(PATTERNS), +// flag_cur: false, +// layout: Layout::default() +// .direction(Direction::Horizontal) +// .constraints([Constraint::Percentage(15), Constraint::Percentage(85)].as_ref()), +// cur_gen: Gen::new(), +// } +// } +// } +// +// static PATTERNS: Vec<(&'static str, &'static Path)> = vec