Created
October 13, 2018 12:53
-
-
Save alexisvisco/3d088c4283c6b403434279c2569e243d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] | |
mod cli; | |
mod commands; | |
extern crate termios; | |
use std::io; | |
use std::io::Read; | |
use std::io::Write; | |
use std::time; | |
fn main() { | |
let stdin = 0; | |
let termios = termios::Termios::from_fd(stdin).unwrap(); | |
let mut new_termios = termios.clone(); | |
new_termios.c_iflag &= !(termios::IGNBRK | termios::BRKINT | termios::PARMRK | termios::ISTRIP | termios::INLCR | termios::IGNCR | termios::ICRNL | termios::IXON); | |
new_termios.c_iflag &= !(termios::OPOST); | |
new_termios.c_lflag &= !(termios::ECHO | termios::ECHONL | termios::ICANON | termios::ISIG | termios::IEXTEN); | |
new_termios.c_cflag &= !(termios::CSIZE | termios::PARENB); | |
new_termios.c_cflag |= termios::CS8; | |
new_termios.c_cc[termios::VMIN] = 1; | |
new_termios.c_cc[termios::VTIME] = 0; | |
termios::tcsetattr(stdin, termios::TCSANOW, &mut new_termios).unwrap(); | |
let stdout = io::stdout(); | |
let mut reader = io::stdin(); | |
let mut last = [0; 8]; | |
let mut last_time = time::Instant::now(); | |
loop { | |
let mut buffer = [0; 8]; | |
stdout.lock().flush().unwrap(); | |
reader.read(&mut buffer).unwrap(); | |
println!("result: {:?}", buffer); | |
let elapsed_duration = last_time.elapsed(); | |
let nanoseconds = elapsed_duration.subsec_nanos() as u64; | |
let ms = (1000*1000*1000 * elapsed_duration.as_secs() + nanoseconds)/(1000 * 1000); | |
if is_ctrl_a(&last) && is_ctrl_d(&buffer) && ms <= 500 { | |
println!("Detach"); | |
break; | |
} | |
last = buffer; | |
last_time = time::Instant::now(); | |
} | |
termios::tcsetattr(stdin, termios::TCSANOW, &termios).unwrap(); | |
} | |
fn is_ctrl_d(v: &[u8]) -> bool { | |
return v == [4, 0, 0, 0, 0, 0, 0, 0]; | |
} | |
fn is_ctrl_a(v: &[u8]) -> bool { | |
return v == [1, 0, 0, 0, 0, 0, 0, 0]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment