Fix #18
This commit is contained in:
parent
4bd96b06ef
commit
b288548756
|
@ -4,16 +4,23 @@ use termion::{TermWrite, IntoRawMode, Color, Style};
|
||||||
use std::io::{Read, Write, stdout, stdin};
|
use std::io::{Read, Write, stdout, stdin};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
// Initialize 'em all.
|
||||||
let stdout = stdout();
|
let stdout = stdout();
|
||||||
let mut stdout = stdout.lock().into_raw_mode().unwrap();
|
let mut stdout = stdout.lock().into_raw_mode().unwrap();
|
||||||
let stdin = stdin();
|
let stdin = stdin();
|
||||||
let stdin = stdin.lock();
|
let stdin = stdin.lock();
|
||||||
|
|
||||||
|
// Move the cursor to (5, 5)
|
||||||
stdout.goto(5, 5).unwrap();
|
stdout.goto(5, 5).unwrap();
|
||||||
|
// Clear the screen.
|
||||||
stdout.clear().unwrap();
|
stdout.clear().unwrap();
|
||||||
|
// Set style to bold.
|
||||||
stdout.style(Style::Bold).unwrap();
|
stdout.style(Style::Bold).unwrap();
|
||||||
|
// Write some guiding stuff
|
||||||
stdout.write(b"yo, 'q' will exit.").unwrap();
|
stdout.write(b"yo, 'q' will exit.").unwrap();
|
||||||
|
// Reset the style.
|
||||||
stdout.reset().unwrap();
|
stdout.reset().unwrap();
|
||||||
|
// Flush and goto (20, 10)
|
||||||
stdout.flush().unwrap();
|
stdout.flush().unwrap();
|
||||||
stdout.goto(20, 10).unwrap();
|
stdout.goto(20, 10).unwrap();
|
||||||
|
|
||||||
|
@ -22,9 +29,13 @@ fn main() {
|
||||||
let b = bytes.next().unwrap().unwrap();
|
let b = bytes.next().unwrap().unwrap();
|
||||||
|
|
||||||
match b {
|
match b {
|
||||||
|
// Quit
|
||||||
b'q' => return,
|
b'q' => return,
|
||||||
|
// Clear the screen
|
||||||
b'c' => stdout.clear(),
|
b'c' => stdout.clear(),
|
||||||
|
// Set red color
|
||||||
b'r' => stdout.color(Color::Rgb(5, 0, 0)),
|
b'r' => stdout.color(Color::Rgb(5, 0, 0)),
|
||||||
|
// Write it to stdout.
|
||||||
a => stdout.write(&[a]),
|
a => stdout.write(&[a]),
|
||||||
}.unwrap();
|
}.unwrap();
|
||||||
|
|
||||||
|
|
26
src/input.rs
26
src/input.rs
|
@ -103,6 +103,7 @@ impl<R: Read> TermRead for R {
|
||||||
match c {
|
match c {
|
||||||
Err(e) => return Err(e),
|
Err(e) => return Err(e),
|
||||||
Ok(0) | Ok(3) | Ok(4) => return Ok(None),
|
Ok(0) | Ok(3) | Ok(4) => return Ok(None),
|
||||||
|
Ok(0x7f) => { passbuf.pop(); },
|
||||||
Ok(b'\n') | Ok(b'\r') => break,
|
Ok(b'\n') | Ok(b'\r') => break,
|
||||||
Ok(c) => passbuf.push(c),
|
Ok(c) => passbuf.push(c),
|
||||||
}
|
}
|
||||||
|
@ -116,11 +117,12 @@ impl<R: Read> TermRead for R {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
use std::io;
|
||||||
|
|
||||||
#[cfg(feature = "nightly")]
|
#[cfg(feature = "nightly")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_keys() {
|
fn test_keys() {
|
||||||
use {TermRead, Key};
|
|
||||||
|
|
||||||
let mut i = b"\x1Bayo\x7F\x1B[D".keys();
|
let mut i = b"\x1Bayo\x7F\x1B[D".keys();
|
||||||
|
|
||||||
assert_eq!(i.next(), Some(Key::Alt('a')));
|
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(), Some(Key::Left));
|
||||||
assert_eq!(i.next(), None);
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue