diff options
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/cli.rs | 21 | ||||
| -rw-r--r-- | src/main.rs | 26 |
3 files changed, 24 insertions, 24 deletions
@@ -4,6 +4,5 @@ version = "0.1.0" authors = ["Yuval Adam <_@yuv.al>"] [dependencies] -colored = "1.6" clap = "2.32" termios = "0.3.0" diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..05a88bd --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,21 @@ +extern crate termios; + +use std::io; +use std::io::{Read, Write}; +use self::termios::{Termios, TCSANOW, ECHO, ICANON, tcsetattr}; + +pub fn read_char() { + let stdin = 0; + let termios = Termios::from_fd(stdin).unwrap(); + let mut new_termios = termios.clone(); // make a mutable copy of termios that we will modify + new_termios.c_lflag &= !(ICANON | ECHO); // no echo and canonical mode + tcsetattr(stdin, TCSANOW, &mut new_termios).unwrap(); + let stdout = io::stdout(); + let mut reader = io::stdin(); + let mut buffer = [0;1]; // read exactly one byte + print!("Hit a key! "); + stdout.lock().flush().unwrap(); + reader.read_exact(&mut buffer).unwrap(); + println!("You have hit: {:?}", buffer); + tcsetattr(stdin, TCSANOW, & termios).unwrap(); // reset the stdin to original termios data +} diff --git a/src/main.rs b/src/main.rs index d3d5f1d..fa67bcf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,31 +1,10 @@ extern crate clap; -extern crate colored; -extern crate termios; -use colored::*; use clap::{Arg, App}; use std::path::Path; use std::process; -use std::io; -use std::io::{Read, Write}; -use termios::{Termios, TCSANOW, ECHO, ICANON, tcsetattr}; - -fn read_char() { - let stdin = 0; - let termios = Termios::from_fd(stdin).unwrap(); - let mut new_termios = termios.clone(); // make a mutable copy of termios that we will modify - new_termios.c_lflag &= !(ICANON | ECHO); // no echo and canonical mode - tcsetattr(stdin, TCSANOW, &mut new_termios).unwrap(); - let stdout = io::stdout(); - let mut reader = io::stdin(); - let mut buffer = [0;1]; // read exactly one byte - print!("Hit a key! "); - stdout.lock().flush().unwrap(); - reader.read_exact(&mut buffer).unwrap(); - println!("You have hit: {:?}", buffer); - tcsetattr(stdin, TCSANOW, & termios).unwrap(); // reset the stdin to original termios data -} +mod cli; fn main() { let app = App::new("Rustcast") @@ -49,5 +28,6 @@ fn main() { process::exit(1); } - read_char(); + // test a single char read + cli::read_char(); } |
