diff options
author | Jesse Luehrs <doy@tozt.net> | 2013-04-01 04:02:18 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2013-04-01 04:02:18 -0500 |
commit | 0d8e05039d691dde1c471d75186884476dd72712 (patch) | |
tree | f70a007dc36f4076dbca418c7c948099d8675bb6 | |
parent | 297f9bba6c646ea9aa55e9181f377cc75c06bdb8 (diff) | |
download | rust-term-0d8e05039d691dde1c471d75186884476dd72712.tar.gz rust-term-0d8e05039d691dde1c471d75186884476dd72712.zip |
buffer writes
-rw-r--r-- | src/term.rs | 36 | ||||
-rw-r--r-- | test/rl.rs | 19 |
2 files changed, 34 insertions, 21 deletions
diff --git a/src/term.rs b/src/term.rs index 9e98688..88c34e7 100644 --- a/src/term.rs +++ b/src/term.rs @@ -9,45 +9,57 @@ use info::{init,escape,escape2}; use util::Trie; struct Writer { + priv buf: ~str, priv cleanup: bool, } pub fn Writer (cleanup: bool) -> Writer { init(); - Writer { cleanup: cleanup } + Writer { buf: ~"", cleanup: cleanup } } impl Writer { - pub fn clear (&self) { - io::print(escape("clear")); + pub fn clear (&mut self) { + str::push_str(&mut self.buf, escape("clear")); } - pub fn move (&self, col: uint, row: uint) { + pub fn move (&mut self, col: uint, row: uint) { if col == 0u && row == 0u { - io::print(escape("home")); + str::push_str(&mut self.buf, escape("home")); } else { - io::print(escape2("cup", row as int, col as int)); + str::push_str(&mut self.buf, + escape2("cup", row as int, col as int)); } } - pub fn cursor (&self, enabled: bool) { + pub fn cursor (&mut self, enabled: bool) { if enabled { - io::print(escape("civis")); + str::push_str(&mut self.buf, escape("civis")); } else { - io::print(escape("cnorm")); + str::push_str(&mut self.buf, escape("cnorm")); } } - pub fn alternate_screen (&self, enable: bool) { + pub fn alternate_screen (&mut self, enable: bool) { if enable { - io::print(escape("smcup")); + str::push_str(&mut self.buf, escape("smcup")); } else { - io::print(escape("rmcup")); + str::push_str(&mut self.buf, escape("rmcup")); } } + + pub fn write (&mut self, text: &str) { + str::push_str(&mut self.buf, text); + } + + pub fn flush (&mut self) { + io::print(self.buf); + io::stdout().flush(); + self.buf = ~""; + } } impl Drop for Writer { @@ -1,31 +1,31 @@ extern mod term; use term::{KeyCharacter,KeyEscape,KeyUp,KeyDown,KeyLeft,KeyRight}; -fn term_app (body: &fn (r: &mut term::Reader, w: &term::Writer)) { - let writer = term::Writer(true); +fn term_app (body: &fn (r: &mut term::Reader, w: &mut term::Writer)) { + let mut writer = term::Writer(true); let mut reader = term::Reader(true); do term::ios::preserve { writer.alternate_screen(true); - body(&mut reader, &writer); + body(&mut reader, &mut writer); } } -fn draw_map (w: &term::Writer, rows: uint, cols: uint) { +fn draw_map (w: &mut term::Writer, rows: uint, cols: uint) { for uint::range(0, rows) |i| { w.move(0, i); - io::print(str::repeat(".", cols)); + w.write(str::repeat(".", cols)); } } -fn draw_character (w: &term::Writer, x: uint, y: uint) { +fn draw_character (w: &mut term::Writer, x: uint, y: uint) { w.move(x, y); - io::print("@"); + w.write("@"); w.move(x, y); } -fn draw_ground (w: &term::Writer, x: uint, y: uint) { +fn draw_ground (w: &mut term::Writer, x: uint, y: uint) { w.move(x, y); - io::print("."); + w.write("."); } fn main () { @@ -42,6 +42,7 @@ fn main () { let mut cursor = true; loop { draw_character(w, x, y); + w.flush(); let k = match r.read() { Some(key) => key, None => break, |