Small 2048 game in Rust
Commit 5744522c3239
tarball2 files changed+37-34
@@ -1,36 +1,8 @@
1
-use std::io::{self, Write};2
-use std::io::{Read, stdin};3
-use std::os::unix::io::AsRawFd;4
-use termios::*;5
-6
-#[derive(Debug, Clone)]7
-pub struct TermMode {8
- original: Termios,9
- fd: i32,10
-}11
-12
-impl TermMode {13
- pub fn new() -> Self {14
- let fd = stdin().as_raw_fd();15
-16
- let original = Termios::from_fd(fd).unwrap();17
-18
- let mut new = original.clone();19
- new.c_lflag &= !(ECHO | ICANON);20
- new.c_cc[VMIN] = 1;21
- new.c_cc[VTIME] = 0;22
-23
- tcsetattr(fd, TCSANOW, &new).unwrap();1
+use std::io;2
+use std::io::{Read, Write, stdin};243
25
- Self { original, fd }26
- }27
-}28
-29
-impl Drop for TermMode {30
- fn drop(&mut self) {31
- tcsetattr(self.fd, TCSANOW, &self.original).unwrap();32
- }33
-}4
+mod term;5
+use term::TermMode;346
357
#[derive(Debug, PartialEq)]368
enum Direction {@@ -55,7 +27,6 @@ fn print_field(field: [[u16; 4]; 4]) {
5527
5628
print!("{}", if j == 3 { "┃\n" } else { "│" });5729
}58
- //print!("\x1b[D┃\n┃ │ │ │ ┃\n");5930
6031
if i != 3 {6132
print!("┠────┼────┼────┼────┨\n");@@ -181,7 +152,7 @@ fn get_direction(input: u8) -> Option<Direction> {
181152
}182153
183154
fn main() {184
- let _term = TermMode::new();155
+ let _term = TermMode::new().unwrap();185156
186157
let mut field: [[u16; 4]; 4] = [[0; 4]; 4];187158
@@ -0,0 +1,32 @@
1
+use crate::stdin;2
+use std::os::unix::io::AsRawFd;3
+use termios::*;4
+5
+#[derive(Debug, Clone)]6
+pub struct TermMode {7
+ original: Termios,8
+ fd: i32,9
+}10
+11
+impl TermMode {12
+ pub fn new() -> std::io::Result<Self> {13
+ let fd = stdin().as_raw_fd();14
+15
+ let original = Termios::from_fd(fd)?;16
+17
+ let mut new = original.clone();18
+ new.c_lflag &= !(ECHO | ICANON);19
+ new.c_cc[VMIN] = 1;20
+ new.c_cc[VTIME] = 0;21
+22
+ tcsetattr(fd, TCSANOW, &new)?;23
+24
+ Ok(Self { original, fd })25
+ }26
+}27
+28
+impl Drop for TermMode {29
+ fn drop(&mut self) {30
+ let _ = tcsetattr(self.fd, TCSANOW, &self.original);31
+ }32
+}