From 2f06f9b0b7ae40dca11b6125ca27a3da767e6c36 Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Thu, 22 Jul 2021 16:45:45 +0200 Subject: [PATCH] Somewhat structure main.rs --- src/main.rs | 86 ++++++++++++++++++++++++++++------------------------- src/view.rs | 3 +- 2 files changed, 48 insertions(+), 41 deletions(-) diff --git a/src/main.rs b/src/main.rs index b21a9c9..518c107 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use gntag::agent::{Agent, SimpleAgent}; -use gntag::view::TerminalView; +use gntag::view::{Backend, TerminalView}; use gntag::world::ActualWorld; use std::io; use std::io::Read; @@ -9,6 +9,8 @@ use std::thread; fn main() { let view = Arc::new(Mutex::new(Some(TerminalView::try_new().unwrap()))); + + // Exit on q, ESC and Ctrl-C and reset terminal let view2 = view.clone(); thread::spawn(move || { let stdin = io::stdin(); @@ -22,48 +24,52 @@ fn main() { } } }); + loop { - let (width, height) = (*view.lock().unwrap()) + run_simulation(&view); + } +} + +fn run_simulation(view: &Arc>>>) { + let (width, height) = (*view.lock().unwrap()) + .as_mut() + .unwrap() + .content_size() + .unwrap(); + let mut agents: Vec<(_, Box)> = vec![]; + for x in (0..width).step_by(10) { + for y in (0..height).step_by(10) { + agents.push(((x, y).into(), Box::new(SimpleAgent))); + } + } + let mut world = ActualWorld::new((width, height).into(), agents); + + let mut gen = 0; + loop { + let resized = (*view.lock().unwrap()) .as_mut() .unwrap() - .content_size() + .draw( + gen, + world + .state + .agent_positions + .get(&world.state.tagged) + .map(|pos| (pos.x, pos.y)) + .unwrap(), + world + .state + .agent_positions + .iter() + .map(|(_id, pos)| (pos.x, pos.y)) + .collect::>() + .as_ref(), + ) .unwrap(); - let mut agents: Vec<(_, Box)> = vec![]; - for x in (0..width).step_by(10) { - for y in (0..height).step_by(10) { - agents.push(((x, y).into(), Box::new(SimpleAgent))); - } - } - let mut world = ActualWorld::new((width, height).into(), agents); - - let mut gen = 0; - loop { - let resized = (*view.lock().unwrap()) - .as_mut() - .unwrap() - .draw( - gen, - world - .state - .agent_positions - .get(&world.state.tagged) - .map(|pos| (pos.x, pos.y)) - .unwrap(), - world - .state - .agent_positions - .iter() - .map(|(_id, pos)| (pos.x, pos.y)) - .collect::>() - .as_ref(), - ) - .unwrap(); - if resized { - break; - }; - world.do_step(); - gen += 1; - //std::thread::sleep(std::time::Duration::from_millis(500)); - } + if resized { + return; + }; + world.do_step(); + gen += 1; } } diff --git a/src/view.rs b/src/view.rs index 320eaee..5fb7290 100644 --- a/src/view.rs +++ b/src/view.rs @@ -3,7 +3,8 @@ use std::error::Error; use std::io::{stdout, Stdout, Write}; use termion::clear; use termion::raw::{IntoRawMode, RawTerminal}; -use tui::backend::{Backend, TermionBackend}; +pub use tui::backend::Backend; +use tui::backend::TermionBackend; use tui::layout::{Alignment, Constraint, Direction, Layout, Rect}; use tui::style::{Color, Modifier, Style}; use tui::text::{Span, Spans};