Commit 45b1136f authored by Ticki's avatar Ticki

get_passwd(), new example, update README

parent 098ce66b
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
-----
......
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();
}
}
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();
......
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)
}
}
#![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;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment