diff --git a/README.md b/README.md index 48d86a1..4dab977 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,24 @@ libterm ======= -A Rust Termios wrapper, providing various useful abstractions for dealing with terminals. +A pure Rust library for handling, manipulating and reading information about terminals. This provides a full-featured alternative to Termbox. + +Features +-------- + +- Raw mode. +- Cursor movement. +- Color output. +- Text formatting. +- Console size. +- Control sequences. +- Termios control. +- Password input. + +TODO +---- + +- Mouse input Usage ----- diff --git a/examples/read.rs b/examples/read.rs new file mode 100644 index 0000000..b31816c --- /dev/null +++ b/examples/read.rs @@ -0,0 +1,22 @@ +extern crate libterm; + +use libterm::ReadExt; +use std::io::{Write, stdout, stdin}; + +fn main() { + let stdout = stdout(); + let mut stdout = stdout.lock(); + let mut stdin = stdin(); + + stdout.write(b"password: ").unwrap(); + stdout.flush().unwrap(); + + let pass = stdin.read_passwd(); + + if let Some(pass) = pass { + stdout.write(pass.as_bytes()).unwrap(); + stdout.write(b"\n").unwrap(); + } else { + stdout.write(b"Error\n").unwrap(); + } +} diff --git a/examples/simple.rs b/examples/simple.rs index 2b0d798..55cd65f 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -1,12 +1,13 @@ extern crate libterm; -use libterm::{TermControl, raw_mode, Color, Mode}; +use libterm::{TermControl, raw_mode, Color, Mode, ReadExt}; use std::io::{Read, Write, stdout, stdin}; fn main() { let _raw = raw_mode(); - let mut stdout = stdout(); - let stdin = stdin(); + let stdout = stdout(); + let mut stdout = stdout.lock(); + let mut stdin = stdin(); stdout.goto(5, 5).unwrap(); stdout.clear().unwrap(); diff --git a/src/extra.rs b/src/extra.rs new file mode 100644 index 0000000..e7c6410 --- /dev/null +++ b/src/extra.rs @@ -0,0 +1,29 @@ +use std::io::Read; +use raw_mode; + +/// Extension to `Read` trait. +pub trait ReadExt { + /// Read a password. + fn read_passwd(&mut self) -> Option; +} + +impl ReadExt for R { + fn read_passwd(&mut self) -> Option { + let _raw = raw_mode(); + let mut string = String::with_capacity(30); + + for c in self.chars() { + match if let Ok(c) = c { + c + } else { + return None; + } { + '\x00' | '\x03' | '\x04' => return None, + '\r' => return Some(string), + b => string.push(b), + } + } + + Some(string) + } +} diff --git a/src/lib.rs b/src/lib.rs index 34b2da9..03f76b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,6 @@ +#![feature(io)] #![feature(libc)] + #[warn(missing_docs)] extern crate libc; @@ -19,3 +21,6 @@ pub use color::Color; mod mode; pub use mode::Mode; + +mod extra; +pub use extra::ReadExt;