Small 2048 game in Rust
Commit 7e8970ba55fa
tarball1 file changed+8-14
@@ -3,7 +3,7 @@ use std::io::{Read, stdin};
33
use std::os::unix::io::AsRawFd;44
use termios::*;55
6
-#[derive(Debug, Clone, Copy)]6
+#[derive(Debug, Clone)]77
pub struct TermMode {88
original: Termios,99
fd: i32,@@ -15,24 +15,20 @@ impl TermMode {
1515
1616
let original = Termios::from_fd(fd).unwrap();1717
18
- Self { original, fd }19
- }20
-21
- pub fn non_canon(&self) -> Self {22
- let mut new = self.original.clone();18
+ let mut new = original.clone();2319
new.c_lflag &= !(ECHO | ICANON);2420
new.c_cc[VMIN] = 1;2521
new.c_cc[VTIME] = 0;2622
27
- tcsetattr(self.fd, TCSANOW, &new).unwrap();23
+ tcsetattr(fd, TCSANOW, &new).unwrap();2824
29
- *self25
+ Self { original, fd }3026
}27
+}3128
32
- pub fn put_back(&self) -> Self {29
+impl Drop for TermMode {30
+ fn drop(&mut self) {3331
tcsetattr(self.fd, TCSANOW, &self.original).unwrap();34
-35
- *self3632
}3733
}3834
@@ -185,7 +181,7 @@ fn get_direction(input: u8) -> Option<Direction> {
185181
}186182
187183
fn main() {188
- let mut term_mode = TermMode::new().non_canon();184
+ let _term = TermMode::new();189185
190186
let mut field: [[u16; 4]; 4] = [[0; 4]; 4];191187
@@ -231,6 +227,4 @@ fn main() {
231227
print!("\x1b[?1049l\x1b[?25h");232228
print_field(field);233229
io::stdout().flush().unwrap();234
-235
- term_mode.put_back();236230
}