Browse Source

Move get_view from main to lib

main
Adrian Heine 3 years ago
parent
commit
b0ebb1bb08
  1. 27
      src/lib.rs
  2. 27
      src/main.rs
  3. 7
      src/view.rs

27
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<Mutex<Option<TerminalView<TermionBackend<RawTerminal<io::Stdout>>>>>>;
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
}

27
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<Mutex<Option<TerminalView<impl Backend>>>>) {
fn run_simulation(view: &DefaultView) {
let (width, height) = (*view.lock().unwrap())
.as_mut()
.unwrap()

7
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};

Loading…
Cancel
Save