aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/generation.rs61
-rw-r--r--src/ui.rs129
2 files changed, 93 insertions, 97 deletions
diff --git a/src/generation.rs b/src/generation.rs
index df971d7..f889c72 100644
--- a/src/generation.rs
+++ b/src/generation.rs
@@ -48,12 +48,11 @@ pub fn render_gen(chunk: &Rect, gen: &Gen) {
}
}
-pub fn new_gen(chunk: &Rect) -> Gen {
+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;
- eprintln!("heigt: {}", rows);
- eprintln!("width: {}", cols);
let mut grid: Vec<Vec<Cell>> = Vec::new();
for _ in 0..rows {
let mut row: Vec<Cell> = Vec::new();
@@ -68,13 +67,15 @@ pub fn new_gen(chunk: &Rect) -> Gen {
pub fn gen_to_spans(gen: &Gen) -> Vec<Spans> {
let mut spans = 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 => print!("😎"),
// Cell::Alive => txt.push_str("🦀"),
- Cell::Alive => txt.push_str("❌"),
+ Cell::Alive => txt.push_str(alive_cells[rand % alive_cells.len()]),
Cell::Dead => txt.push_str("⬛️"),
// Cell::Alive => txt.push('X'),
// Cell::Dead => txt.push('-'),
@@ -85,6 +86,8 @@ pub fn gen_to_spans(gen: &Gen) -> Vec<Spans> {
spans
}
+// pub fn
+
pub fn is_valid_idx(i: i32, j: i32, m: i32, n: i32) -> bool {
i >= 0 && i < m && j >= 0 && j < n
}
@@ -113,7 +116,6 @@ pub fn get_alive(x: i32, y: i32, cur_gen: &Gen) -> i32 {
}
pub fn next_gen(app: &mut App) -> Gen {
- app.flag = true;
let m: i32 = app.cur_gen.len() as i32;
let n: i32 = app.cur_gen[0].len() as i32;
let mut nxt_gen: Gen = Gen::new();
@@ -176,53 +178,4 @@ pub fn init() -> Result<(), Box<dyn Error>> {
}
Ok(())
-
- // let mut stdout = io::stdout();
- // let mut frame: Gen = new_gen();
- // let mut nxt;
- //
- // terminal::enable_raw_mode().unwrap();
- // stdout.execute(EnterAlternateScreen).unwrap();
- // stdout.execute(Hide).unwrap();
- //
- // 'gameloop: loop {
- // while event::poll(Duration::default()).unwrap() {
- // if let Event::Key(key_event) = event::read().unwrap() {
- // match key_event.code {
- // KeyCode::Esc | KeyCode::Char('q') => {
- // break 'gameloop;
- // }
- // KeyCode::Char('s') => {
- // frame = new_gen();
- // render_gen(&mut stdout, &frame)
- // }
- //
- // KeyCode::Char('n') => {
- // nxt = next_gen(&mut frame);
- // render_gen(&mut stdout, &nxt)
- // }
- // KeyCode::Char('a') => 'animate: loop {
- // nxt = next_gen(&mut frame);
- // render_gen(&mut stdout, &nxt);
- // sleep(Duration::from_millis(16));
- // if (poll(Duration::from_millis(1))).unwrap() {
- // if let Event::Key(k) = event::read().unwrap() {
- // match k.code {
- // KeyCode::Char('q') => break 'animate,
- // _ => {}
- // }
- // }
- // } else {
- // }
- // },
- // _ => {}
- // }
- // }
- // }
- // }
- //
- // stdout.execute(Show).unwrap();
- // stdout.execute(LeaveAlternateScreen).unwrap();
- // terminal::disable_raw_mode().unwrap();
- // Ok(())
}
diff --git a/src/ui.rs b/src/ui.rs
index 7317901..16f8640 100644
--- a/src/ui.rs
+++ b/src/ui.rs
@@ -74,7 +74,7 @@ impl<T> StatefulList<T> {
/// Check the drawing logic for items on how to specify the highlighting style for selected items.
pub struct App<'a> {
pub items: StatefulList<(&'a str, usize)>,
- pub flag: bool,
+ pub flag_cur: bool,
pub layout: Layout,
pub cur_gen: Gen,
pub nxt_gen: Gen,
@@ -86,11 +86,22 @@ impl<'a> App<'a> {
App {
items: StatefulList::with_items(vec![
("Glider", 1),
- ("Glider", 2),
- ("Glider", 1),
- ("Glider", 3),
+ ("achimsotherp16", 2),
+ ("achimsp11", 3),
+ ("achimsp144", 4),
+ ("achimsp16", 4),
+ ("achimsp4", 4),
+ ("achimsp5original", 4),
+ ("achimsp8", 4),
+ ("aforall", 4),
+ ("againstthegraingeryshep", 4),
+ ("aircraftcarrier", 4),
+ ("almostgun1", 4),
+ ("almostgun2", 4),
+ ("almostknightship", 4),
+ ("alternatewickstretcher1", 4),
]),
- flag: false,
+ flag_cur: false,
layout: Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(15), Constraint::Percentage(85)].as_ref()),
@@ -114,7 +125,7 @@ pub fn run_app<B: Backend>(
) -> io::Result<()> {
let mut last_tick = Instant::now();
loop {
- terminal.draw(|f| ui(f, &mut app))?;
+ terminal.draw(|f| ui_list(f, &mut app))?;
let timeout = Duration::from_millis(10);
if crossterm::event::poll(timeout)? {
@@ -124,46 +135,29 @@ pub fn run_app<B: Backend>(
KeyCode::Left | KeyCode::Char('h') => app.items.unselect(),
KeyCode::Down | KeyCode::Char('j') => app.items.next(),
KeyCode::Up | KeyCode::Char('k') => app.items.previous(),
- _ => {} // KeyCode::Char('s') => {
- // frame = new_gen();
- // render_gen(&mut stdout, &frame);
- // Ok(());
- // }
- // KeyCode::Char('n') => {
- // nxt = next_gen(&mut frame);
- // render_gen(&mut stdout, &nxt)
- // }
- // KeyCode::Char('a') => 'animate: loop {
- // nxt = next_gen(&mut frame);
- // render_gen(&mut stdout, &nxt);
- // sleep(Duration::from_millis(16));
- // if (crossterm::event::poll(Duration::from_millis(1))).unwrap() {
- // if let Event::Key(k) = event::read().unwrap() {
- // match k.code {
- // KeyCode::Char('q') => break 'animate,
- // _ => {}
- // }
- // }
- // } else {
- // }
- // },
- // _ => {}
+ KeyCode::Char('n') => {
+ terminal.draw(|f| ui_game(f, &mut app))?;
+ }
+ KeyCode::Char('a') => 'animate: loop {
+ terminal.draw(|f| ui_game(f, &mut app))?;
+ sleep(Duration::from_millis(16));
+ 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,
+ _ => {}
+ }
+ }
+ }
+ },
+ _ => {}
}
}
}
- // if last_tick.elapsed() >= tick_rate {
- // app.on_tick();
- // last_tick = Instant::now();
- // }
}
}
-fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
- // Create two chunks with equal horizontal screen space
- // let chunks = Layout::default()
- // .direction(Direction::Horizontal)
- // .constraints([Constraint::Percentage(15), Constraint::Percentage(85)].as_ref());
- // .split(f.size());
+fn ui_list<B: Backend>(f: &mut Frame<B>, app: &mut App) {
let chunks = app.layout.split(f.size());
// Iterate through all elements in the `items` app and append some debug text to it.
@@ -182,6 +176,57 @@ fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
.block(Block::default().borders(Borders::ALL).title("List"))
.highlight_style(
Style::default()
+ .bg(Color::White)
+ .add_modifier(Modifier::ITALIC),
+ )
+ .highlight_symbol("> ");
+
+ // We can now render the item list
+ f.render_stateful_widget(items, chunks[0], &mut app.items.state);
+
+
+ if !app.flag_cur {
+ app.cur_gen = new_gen(&chunks[1], app);
+ }
+ let spans = gen_to_spans(&app.cur_gen);
+
+ 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(" Game Of Life "))
+ .alignment(Alignment::Center);
+ f.render_widget(paragraph, chunks[1]);
+
+}
+
+
+fn ui_game<B: Backend>(f: &mut Frame<B>, app: &mut App) {
+ let chunks = app.layout.split(f.size());
+
+ let items: Vec<ListItem> = app
+ .items
+ .items
+ .iter()
+ .map(|i| {
+ let lines = vec![Spans::from(i.0)];
+ ListItem::new(lines).style(Style::default().fg(Color::Red).bg(Color::Black))
+ })
+ .collect();
+
+ // Create a List from all list items and highlight the currently selected one
+ let items = List::new(items)
+ .block(Block::default().borders(Borders::ALL).title("List"))
+ .highlight_style(
+ Style::default()
.bg(Color::Blue)
.add_modifier(Modifier::BOLD),
)
@@ -190,9 +235,6 @@ fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
// We can now render the item list
f.render_stateful_widget(items, chunks[0], &mut app.items.state);
- if !app.flag {
- app.cur_gen = new_gen(&chunks[1]);
- }
let nxt = next_gen(app);
let spans = gen_to_spans(&nxt);
@@ -211,4 +253,5 @@ fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
.block(create_block(" Game Of Life "))
.alignment(Alignment::Center);
f.render_widget(paragraph, chunks[1]);
+
}