This commit is contained in:
Ticki 2016-04-02 18:05:31 +02:00
parent 4bd96b06ef
commit b288548756
2 changed files with 35 additions and 2 deletions

View File

@ -4,16 +4,23 @@ use termion::{TermWrite, IntoRawMode, Color, Style};
use std::io::{Read, Write, stdout, stdin};
fn main() {
// Initialize 'em all.
let stdout = stdout();
let mut stdout = stdout.lock().into_raw_mode().unwrap();
let stdin = stdin();
let stdin = stdin.lock();
// Move the cursor to (5, 5)
stdout.goto(5, 5).unwrap();
// Clear the screen.
stdout.clear().unwrap();
// Set style to bold.
stdout.style(Style::Bold).unwrap();
// Write some guiding stuff
stdout.write(b"yo, 'q' will exit.").unwrap();
// Reset the style.
stdout.reset().unwrap();
// Flush and goto (20, 10)
stdout.flush().unwrap();
stdout.goto(20, 10).unwrap();
@ -22,9 +29,13 @@ fn main() {
let b = bytes.next().unwrap().unwrap();
match b {
// Quit
b'q' => return,
// Clear the screen
b'c' => stdout.clear(),
// Set red color
b'r' => stdout.color(Color::Rgb(5, 0, 0)),
// Write it to stdout.
a => stdout.write(&[a]),
}.unwrap();

View File

@ -103,6 +103,7 @@ impl<R: Read> TermRead for R {
match c {
Err(e) => return Err(e),
Ok(0) | Ok(3) | Ok(4) => return Ok(None),
Ok(0x7f) => { passbuf.pop(); },
Ok(b'\n') | Ok(b'\r') => break,
Ok(c) => passbuf.push(c),
}
@ -116,11 +117,12 @@ impl<R: Read> TermRead for R {
#[cfg(test)]
mod test {
use super::*;
use std::io;
#[cfg(feature = "nightly")]
#[test]
fn test_keys() {
use {TermRead, Key};
let mut i = b"\x1Bayo\x7F\x1B[D".keys();
assert_eq!(i.next(), Some(Key::Alt('a')));
@ -130,4 +132,24 @@ mod test {
assert_eq!(i.next(), Some(Key::Left));
assert_eq!(i.next(), None);
}
#[test]
fn test_passwd() {
let test1 = "this is the first test";
let test2 = "this is the second test";
let mut sink = io::sink();
assert_eq!(&test1.as_bytes().read_passwd(&mut sink).unwrap().unwrap(), test1);
assert_eq!(&test2.as_bytes().read_passwd(&mut sink).unwrap().unwrap(), test2);
}
#[test]
fn test_passwd_backspace() {
let test1 = "this is the\x7f first\x7f\x7f test";
let test2 = "this is the seco\x7fnd test\x7f";
let mut sink = io::sink();
assert_eq!(&test1.as_bytes().read_passwd(&mut sink).unwrap().unwrap(), "this is th fir test");
assert_eq!(&test2.as_bytes().read_passwd(&mut sink).unwrap().unwrap(), "this is the secnd tes");
}
}