aboutsummaryrefslogtreecommitdiffstats
path: root/test/rl.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2013-03-29 01:21:45 -0500
committerJesse Luehrs <doy@tozt.net>2013-03-29 01:21:45 -0500
commit896887e9abb3dd6c704ad5aab99db93f05e1492e (patch)
tree303e8de8a060b27ba969fb3db7ffdfe71b70350e /test/rl.rs
parent4cd87702004fca9e05e7c5bb899f366caf632842 (diff)
downloadrust-term-896887e9abb3dd6c704ad5aab99db93f05e1492e.tar.gz
rust-term-896887e9abb3dd6c704ad5aab99db93f05e1492e.zip
use the trie to implement reading with escape codes
Diffstat (limited to 'test/rl.rs')
-rw-r--r--test/rl.rs32
1 files changed, 21 insertions, 11 deletions
diff --git a/test/rl.rs b/test/rl.rs
index 9b096b4..fe1103b 100644
--- a/test/rl.rs
+++ b/test/rl.rs
@@ -1,11 +1,12 @@
extern mod term;
-use core::io::ReaderUtil;
+use term::{KeyCharacter,KeyEscape,KeyUp,KeyDown,KeyLeft,KeyRight};
-fn term_app (body: &fn (w: &term::Writer)) {
+fn term_app (body: &fn (r: &term::Reader, w: &term::Writer)) {
let writer = term::Writer(true);
+ let reader = term::Reader();
do term::ios::preserve {
writer.alternate_screen(true);
- body(&writer);
+ body(&reader, &writer);
}
}
@@ -30,7 +31,7 @@ fn draw_ground (w: &term::Writer, x: uint, y: uint) {
fn main () {
let (cols, rows) = term::size();
- do term_app |w| {
+ do term_app |r, w| {
term::cbreak();
term::echo(false);
w.clear();
@@ -41,13 +42,22 @@ fn main () {
let mut cursor = true;
loop {
draw_character(w, x, y);
- match io::stdin().read_char() {
- 'q' => { break }
- 'h' if x > 0 => { draw_ground(w, x, y); x -= 1 }
- 'j' if y < rows - 1 => { draw_ground(w, x, y); y += 1 }
- 'k' if y > 0 => { draw_ground(w, x, y); y -= 1 }
- 'l' if x < cols - 1 => { draw_ground(w, x, y); x += 1 }
- ' ' => { w.cursor(cursor); cursor = !cursor }
+ let k = match r.read() {
+ Some(key) => key,
+ None => break,
+ };
+ draw_ground(w, x, y);
+
+ match k {
+ KeyCharacter('q') | KeyEscape => { break }
+
+ KeyCharacter('h') | KeyLeft if x > 0 => { x -= 1 }
+ KeyCharacter('j') | KeyDown if y < rows - 1 => { y += 1 }
+ KeyCharacter('k') | KeyUp if y > 0 => { y -= 1 }
+ KeyCharacter('l') | KeyRight if x < cols - 1 => { x += 1 }
+
+ KeyCharacter(' ') => { w.cursor(cursor); cursor = !cursor }
+
_ => { }
}
}