From b0ebb1bb08f3ae122c1809ee6ba92f1490b37f23 Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Fri, 23 Jul 2021 15:24:44 +0200 Subject: [PATCH] Move get_view from main to lib --- src/lib.rs | 27 +++++++++++++++++++++++++++ src/main.rs | 27 +++------------------------ src/view.rs | 7 ++++--- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 486b5a1..c9f7445 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,12 @@ pub mod view; pub mod world; use agent::{Agent, SimpleAgent}; +use std::io; +use std::io::Read; +use std::process::exit; +use std::sync::{Arc, Mutex}; +use std::thread; +use view::{RawTerminal, TerminalView, TermionBackend}; use world::ActualWorld; pub fn get_world(width: isize, height: isize, spacing: usize, validating: bool) -> ActualWorld { @@ -14,3 +20,24 @@ pub fn get_world(width: isize, height: isize, spacing: usize, validating: bool) } ActualWorld::new((width, height).into(), agents, validating) } + +pub type DefaultView = Arc>>>>>; +pub fn get_view() -> DefaultView { + 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(); + for byte in stdin.bytes().flatten() { + if byte == b'q' || byte == 0x1b || byte == 0x03 { + if let Ok(mut view) = view2.lock() { + *view = None; // drop view + println!("\n"); + } + exit(0); + } + } + }); + view +} diff --git a/src/main.rs b/src/main.rs index 5f38ece..84c9d82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,35 +1,14 @@ -use gntag::get_world; -use gntag::view::{Backend, TerminalView}; -use std::io; -use std::io::Read; -use std::process::exit; -use std::sync::{Arc, Mutex}; -use std::thread; +use gntag::{get_view, get_world, DefaultView}; 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(); - for byte in stdin.bytes().flatten() { - if byte == b'q' || byte == 0x1b || byte == 0x03 { - if let Ok(mut view) = view2.lock() { - *view = None; // drop view - println!("\n"); - } - exit(0); - } - } - }); + let view = get_view(); loop { run_simulation(&view); } } -fn run_simulation(view: &Arc>>>) { +fn run_simulation(view: &DefaultView) { let (width, height) = (*view.lock().unwrap()) .as_mut() .unwrap() diff --git a/src/view.rs b/src/view.rs index 5fb7290..8aa8a21 100644 --- a/src/view.rs +++ b/src/view.rs @@ -2,9 +2,10 @@ use std::convert::TryInto; use std::error::Error; use std::io::{stdout, Stdout, Write}; use termion::clear; -use termion::raw::{IntoRawMode, RawTerminal}; -pub use tui::backend::Backend; -use tui::backend::TermionBackend; +use termion::raw::IntoRawMode; +pub use termion::raw::RawTerminal; +use tui::backend::Backend; +pub use tui::backend::TermionBackend; use tui::layout::{Alignment, Constraint, Direction, Layout, Rect}; use tui::style::{Color, Modifier, Style}; use tui::text::{Span, Spans};