diff options
| author | Yuval Adam <_@yuv.al> | 2018-07-08 22:51:21 +0300 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2018-07-08 22:51:21 +0300 |
| commit | 0bf59b49f10cff3aff18272d624f678a7369ce35 (patch) | |
| tree | f0c2981903dea3c5e4341d60db4feb1f95f1a618 /src | |
| parent | d1e30b39f962fb1e69184dc62ca6595e41695702 (diff) | |
Implemented a properly threaded user input loop
Diffstat (limited to 'src')
| -rw-r--r-- | src/cli.rs | 56 | ||||
| -rw-r--r-- | src/main.rs | 26 |
2 files changed, 65 insertions, 17 deletions
@@ -1,21 +1,47 @@ extern crate termios; use std::io; -use std::io::{Read, Write}; +use std::io::{Stdout, Stdin, 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 +pub struct Controller { + termios: Termios, + new_termios: Termios, + stdin: i32, + stdout: Stdout, + reader: Stdin, + buffer: [u8; 1] +} + +impl Controller { + pub fn init() -> Controller { + 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 reader = io::stdin(); + let buffer = [0; 1]; + Controller { + termios: termios, + new_termios: new_termios, + stdin: stdin, + stdout: stdout, + reader: reader, + buffer: buffer + } + } + + pub fn read(&mut self) -> u8 { + print!("Hit a key! "); + self.stdout.lock().flush().unwrap(); + self.reader.read_exact(&mut self.buffer).unwrap(); + println!("You have hit: {:?}", self.buffer); + self.buffer[0] + } + + pub fn destroy(&self) { + tcsetattr(self.stdin, TCSANOW, & self.termios).unwrap(); // reset the stdin to original termios data + } } diff --git a/src/main.rs b/src/main.rs index 972e7b0..86d5b85 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,8 @@ use colored::*; use clap::{Arg, App}; use std::path::Path; use std::process; +use std::thread; +use std::sync::mpsc; mod cli; @@ -30,6 +32,26 @@ fn main() { process::exit(1); } - // test a single char read - cli::read_char(); + let (tx, rx) = mpsc::channel(); + + let child = thread::spawn(move || { + let mut controller = cli::Controller::init(); + loop { + let c = controller.read(); + tx.send(c).unwrap(); + if c == 113 { + break; + } + } + controller.destroy(); + }); + + for received in rx { + println!("Got char: {}", received); + } + + println!("Waiting for all thread"); + let _res = child.join(); + println!("Done!"); + } |
