summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 38d80878da0321c2a7cefa0a84193c58bdaafdd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
extern crate termion;

use termion::event::Key;
use termion::input::TermRead;
use termion::raw::IntoRawMode;
use termion::screen::AlternateScreen;

use std::io::{self, Write};
use std::thread;
use std::time::Duration;

static SNEK: &'static [u8] = include_bytes!("snek.six");

fn main() {
    let input = termion::async_stdin();
    let mut keys = input.keys();
    let mut output = io::stdout().into_raw_mode().unwrap();
    let mut screen = Vec::new();

    let mut size = termion::terminal_size().expect("Can't get terminal size");
    write!(screen, "{}", termion::clear::All);


    loop {
        let new_size = termion::terminal_size().unwrap();
        if new_size != size {
            write!(screen, "{}", termion::clear::All);
            size = new_size;
        }

        write!(screen, "{}", termion::cursor::Goto(20, 20));

        screen.write_all(SNEK).expect("Can't write snek");

        match keys.next() {
            Some(Ok(Key::Char('q'))) => break,
            Some(k) => println!("{:?}", k),
            None => ()
        }

        output.write_all(&screen).expect("Can't write screen");
        screen.clear();

        thread::sleep(Duration::from_millis(16));
    }
}