From a15bbbad39bf35adeb7eaa31db2c5ad6b4402414 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 2 Apr 2013 20:03:18 -0500 Subject: make the Term object always be for full-screen terminal apps i'll add functionality for coloring normal output separately --- src/term.rs | 65 ++++++++++++++++++++++++++----------------------------------- test/rl.rs | 31 ++++++++++++++++------------- 2 files changed, 45 insertions(+), 51 deletions(-) diff --git a/src/term.rs b/src/term.rs index 8da623a..ee7859e 100644 --- a/src/term.rs +++ b/src/term.rs @@ -51,21 +51,22 @@ struct Term { priv w: Writer, } -pub fn Term (cleanup: bool) -> Term { +pub fn Term () -> Term { info::init(); - Term { r: Reader(cleanup), w: Writer(cleanup) } + + ios::cbreak(); + ios::echo(false); + + print(escape("smkx")); + print(escape("smcup")); + print(escape("sgr0")); + print(escape("cnorm")); + print(escape("clear")); + + Term { r: Reader(), w: Writer() } } impl Term { - pub fn init_term_app (&mut self) { - cbreak(); - echo(false); - self.write(escape("smkx")); - self.alternate_screen(true); - self.clear(); - self.flush(); - } - pub fn clear (&mut self) { self.w.clear(); } @@ -128,9 +129,20 @@ impl Term { } } +impl Drop for Term { + fn finalize (&self) { + print(escape("rmkx")); + print(escape("rmcup")); + print(escape("sgr0")); + print(escape("cnorm")); + + // XXX should really restore the previous termios mode... + ios::cooked(); + } +} + struct Writer { priv buf: ~str, - priv cleanup: bool, priv state: AttrState, } @@ -144,10 +156,8 @@ struct AttrState { blink: bool, } -fn Writer (cleanup: bool) -> Writer { - let mut w = Writer { buf: ~"", cleanup: cleanup, state: AttrState() }; - w.reset_attributes(); - w +fn Writer () -> Writer { + Writer { buf: ~"", state: AttrState() } } fn AttrState () -> AttrState { @@ -323,24 +333,13 @@ impl Writer { } } -impl Drop for Writer { - fn finalize (&self) { - if self.cleanup { - print(escape("rmcup")); - print(escape("sgr0")); - print(escape("cnorm")); - } - } -} - struct Reader { priv escapes: Trie, priv buf: ~str, - priv cleanup: bool, } -pub fn Reader (cleanup: bool) -> Reader { - Reader { escapes: build_escapes_trie(), buf: ~"", cleanup: cleanup } +pub fn Reader () -> Reader { + Reader { escapes: build_escapes_trie(), buf: ~"" } } impl Reader { @@ -408,14 +407,6 @@ impl Reader { } } -impl Drop for Reader { - fn finalize (&self) { - if self.cleanup { - print(escape("rmkx")); - } - } -} - // XXX this whole thing needs to be able to deal with caps that don't exist fn build_escapes_trie () -> Trie { let mut trie = Trie(); diff --git a/test/rl.rs b/test/rl.rs index 9355f8d..c0dee03 100644 --- a/test/rl.rs +++ b/test/rl.rs @@ -2,14 +2,6 @@ extern mod term; use term::{KeyCharacter,KeyEscape,KeyUp,KeyDown,KeyLeft,KeyRight,KeyF}; use term::{Color,ColorRed}; -fn term_app (body: &fn (r: &mut term::Term)) { - do term::ios::preserve { - let mut term = term::Term(true); - term.init_term_app(); - body(&mut term); - } -} - fn draw_map (term: &mut term::Term, color: Option, rows: uint, cols: uint) { match color { @@ -46,20 +38,22 @@ fn draw_ground (term: &mut term::Term, color: Option, fn main () { let (cols, rows) = term::size(); - do term_app |term| { + { + let mut term = term::Term(); + let mut (x, y) = (0u, 0u); let mut cursor = true; let mut color = None; - draw_map(term, color, rows, cols); + draw_map(&mut term, color, rows, cols); loop { - draw_character(term, None, x, y); + draw_character(&mut term, None, x, y); let k = match term.read() { Some(key) => key, None => break, }; - draw_ground(term, color, x, y); + draw_ground(&mut term, color, x, y); match k { KeyCharacter('q') | KeyEscape => { break } @@ -71,11 +65,11 @@ fn main () { KeyF(1) => { color = Some(ColorRed); - draw_map(term, color, rows, cols); + draw_map(&mut term, color, rows, cols); } KeyF(6) => { color = None; - draw_map(term, color, rows, cols); + draw_map(&mut term, color, rows, cols); } KeyCharacter(' ') => { term.cursor(cursor); cursor = !cursor } @@ -84,4 +78,13 @@ fn main () { } } } + + // XXX this is here mostly to work around a really weird bug where any + // non-escape key quits the program. removing one of the KeyF branches + // in the above match statement fixes it, as does adding a print + // statement basically anywhere, or changing the return value of + // term::Term::read from "self.w.read()" to "let k = self.w.read(); k" + // i have basically no way to debug this, and it really doesn't sound + // like my fault, so i'm going to ignore it for now. + println("Be seeing you..."); } -- cgit v1.2.3-54-g00ecf