jag

2048_rust

public

Small 2048 game in Rust

Star0
Watch0
72d88c7f9762520c03e916be39306eef4afb9657
1
ssh://git@git.jaglekon.de:30/jag/2048_rust.git
src/term.rsRust32 lines639 B
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
}