diff options
| author | Yuval Adam <_@yuv.al> | 2018-07-05 22:05:22 +0300 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2018-07-05 22:05:22 +0300 |
| commit | 5880c9d7d6fb10b9294f7e1b7a30c3ed327e076e (patch) | |
| tree | a024d98a84737e5f4d2923e72f44902d3a2a5531 | |
| parent | a0537ab035ac31bb0f4f1f2107c9296c64cc4763 (diff) | |
Termios solution works
| -rw-r--r-- | Cargo.lock | 10 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/main.rs | 23 |
3 files changed, 34 insertions, 0 deletions
@@ -72,6 +72,7 @@ version = "0.1.0" dependencies = [ "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", "colored 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "termios 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -90,6 +91,14 @@ dependencies = [ ] [[package]] +name = "termios" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "textwrap" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -138,6 +147,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" +"checksum termios 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "70226acdf12d182df757d9fb07c0257a1558ec48c8059f607d6b38145ce4e2fa" "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" @@ -6,3 +6,4 @@ authors = ["Yuval Adam <_@yuv.al>"] [dependencies] colored = "1.6" clap = "2.32" +termios = "0.3.0" diff --git a/src/main.rs b/src/main.rs index 8c025af..d3d5f1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,32 @@ 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 +} + fn main() { let app = App::new("Rustcast") .version("0.1") @@ -27,4 +48,6 @@ fn main() { println!("\n{} {}\n", "Input file does not exist:".red(), infile.red()); process::exit(1); } + + read_char(); } |
