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
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
#[cfg(not(windows))]
extern crate termios;
#[cfg(not(windows))]
use std::io::Read;
#[cfg(windows)]
pub struct Getch{}

#[cfg(not(windows))]
pub enum Getch {
    Termios(termios::Termios),
    None
}

#[cfg(windows)]
extern crate libc;
#[cfg(windows)]
use libc::c_int;
#[cfg(windows)]
extern "C" {
    fn _getch()->c_int;
}

#[cfg(not(windows))]
use termios::{tcsetattr,ICANON,ECHO};

impl Getch {
    #[cfg(windows)]
    pub fn new() -> Getch {
        Getch{}
    }
    #[cfg(not(windows))]
    pub fn new() -> Getch {
        if let Ok(mut termios) = termios::Termios::from_fd(0) {
            let c_lflag = termios.c_lflag;
            termios.c_lflag &= !(ICANON|ECHO);

            if let Ok(()) = tcsetattr(0, termios::TCSADRAIN, &termios) {
                termios.c_lflag = c_lflag;
                return Getch::Termios(termios)
            }
        }
        Getch::None
    }

    #[cfg(windows)]
    pub fn getch(&self) -> Result<u8, std::io::Error> {
        loop {
            unsafe {
                let k= _getch();
                if k==0 {
                    // Ignore next input.
                    _getch();
                } else {
                    return Ok(k as u8)
                }
            }
        }
    }
    #[cfg(not(windows))]
    pub fn getch(&self) -> Result<u8, std::io::Error> {
        let mut r:[u8;1]=[0];
        let mut stdin = std::io::stdin();
        loop {
            if stdin.read(&mut r[..])? == 0 { return Ok(0) }
            else {
                if r[0]==27 {
                    if stdin.read(&mut r[..])? == 0 { return Ok(0) }
                    if r[0] == b'[' || (r[0] >= b'0' && r[0] <= b'9') {
                        if stdin.read(&mut r[..])? == 0 { return Ok(0) }
                        // Skip all until we see a letter.
                        while !((r[0] >= b'a' && r[0] <= b'z') || (r[0] >= b'A' && r[0] <= b'Z')) {
                            if stdin.read(&mut r[..])? == 0 { return Ok(0) }
                        }
                        if stdin.read(&mut r[..])? == 0 { return Ok(0) }
                        return Ok(r[0])
                    } else if r[0] == b'(' || r[0] == b')' || r[0] == b'#' {
                        // skip the next character and return
                        if stdin.read(&mut r[..])? == 0 { return Ok(0) }
                        if stdin.read(&mut r[..])? == 0 { return Ok(0) }
                        return Ok(r[0])
                    } else {
                        // return the next character
                        if stdin.read(&mut r[..])? == 0 { return Ok(0) }
                        return Ok(r[0])
                    }
                } else {
                    // TODO: accept utf-8
                    return Ok(r[0])
                }
            }
        }
    }
}

impl Drop for Getch {
    #[cfg(not(windows))]
    fn drop(&mut self) {
        if let Getch::Termios(ref mut termios) = *self {
            tcsetattr(0, termios::TCSADRAIN, &termios).unwrap_or(())
        }
    }
    #[cfg(windows)]
    fn drop(&mut self) {
    }
}