From b2885487569af9028dd6b79fa5dd725e84b91773 Mon Sep 17 00:00:00 2001 From: Ticki Date: Sat, 2 Apr 2016 18:05:31 +0200 Subject: [PATCH] Fix #18 --- examples/simple.rs | 11 +++++++++++ src/input.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/examples/simple.rs b/examples/simple.rs index b998a12..5cf2cc2 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -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(); diff --git a/src/input.rs b/src/input.rs index 754823e..8338634 100644 --- a/src/input.rs +++ b/src/input.rs @@ -103,6 +103,7 @@ impl 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 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"); + } }