diff options
author | Jesse Luehrs <doy@tozt.net> | 2013-03-31 23:18:00 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2013-03-31 23:18:00 -0500 |
commit | 21cdc442df63f6d9897a0941a72ec387a27446e3 (patch) | |
tree | f52e2f80ac12bc4e400835c5af738988c0d9017a /src | |
parent | 8e5057c4289c14dbc9e3b67330964729762714a1 (diff) | |
download | rust-term-21cdc442df63f6d9897a0941a72ec387a27446e3.tar.gz rust-term-21cdc442df63f6d9897a0941a72ec387a27446e3.zip |
buffer escapes, so we can back out and treat them as individual chars
Diffstat (limited to 'src')
-rw-r--r-- | src/term.rs | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/src/term.rs b/src/term.rs index 21c4263..001138f 100644 --- a/src/term.rs +++ b/src/term.rs @@ -80,14 +80,19 @@ enum Keypress { struct Reader { priv escapes: ~Trie<Keypress>, + priv buf: ~str, } pub fn Reader () -> Reader { - Reader { escapes: build_escapes_trie() } + Reader { escapes: build_escapes_trie(), buf: ~"" } } impl Reader { - pub fn read (&self) -> Option<Keypress> { + pub fn read (&mut self) -> Option<Keypress> { + if str::len(self.buf) > 0 { + return Some(KeyCharacter(str::shift_char(&mut self.buf))); + } + let mut buf = ~""; loop { let c = io::stdin().read_char(); @@ -96,22 +101,14 @@ impl Reader { } str::push_char(&mut buf, c); - io::print(str::escape_default(buf)); if !self.escapes.has_prefix(buf) { - match self.escapes.find(buf) { - &Some(k) => { return Some(k) } + return match self.escapes.find(buf) { + &Some(k) => { Some(k) } &None => { - if str::len(buf) == 1 { - return Some(KeyCharacter(str::char_at(buf, 0))); - } - else { - // XXX this is... suboptimal - // really, we need to do an ungetc sort of thing - // with the characters in buf, and then just - // return the first character as a KeyCharacter - fail!(~"unknown escape"); - } + str::push_str(&mut self.buf, buf); + let next = str::shift_char(&mut self.buf); + Some(KeyCharacter(next)) } } } |