get_passwd(), new example, update README

This commit is contained in:
Ticki 2016-03-07 18:42:11 +01:00
parent 098ce66b84
commit 45b1136f75
5 changed files with 78 additions and 4 deletions

View File

@ -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
-----

22
examples/read.rs Normal file
View File

@ -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();
}
}

View File

@ -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();

29
src/extra.rs Normal file
View File

@ -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<String>;
}
impl<R: Read> ReadExt for R {
fn read_passwd(&mut self) -> Option<String> {
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)
}
}

View File

@ -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;