From 368fb132f840f35ecaaa3e9654a3d2e781b4470c Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 2 Apr 2013 01:38:20 -0500 Subject: implement the rest of the attributes --- src/term.rs | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 116 insertions(+), 5 deletions(-) diff --git a/src/term.rs b/src/term.rs index 19f1ff3..2572942 100644 --- a/src/term.rs +++ b/src/term.rs @@ -88,6 +88,18 @@ impl Term { self.w.standout(enabled); } + pub fn reverse (&mut self, enabled: bool) { + self.w.reverse(enabled); + } + + pub fn bold (&mut self, enabled: bool) { + self.w.bold(enabled); + } + + pub fn blink (&mut self, enabled: bool) { + self.w.blink(enabled); + } + pub fn cursor (&mut self, enabled: bool) { self.w.cursor(enabled); } @@ -113,10 +125,35 @@ impl Term { struct Writer { priv buf: ~str, priv cleanup: bool, + priv state: AttrState, +} + +struct AttrState { + fg: Option, + bg: Option, + underline: bool, + standout: bool, + reverse: bool, + bold: bool, + blink: bool, } fn Writer (cleanup: bool) -> Writer { - Writer { buf: ~"", cleanup: cleanup } + let mut w = Writer { buf: ~"", cleanup: cleanup, state: AttrState() }; + w.reset_attributes(); + w +} + +fn AttrState () -> AttrState { + AttrState { + fg: None, + bg: None, + underline: false, + standout: false, + reverse: false, + bold: false, + blink: false, + } } impl Writer { @@ -134,18 +171,17 @@ impl Writer { } fn fg_color (&mut self, color: Color) { + self.state.fg = Some(color); self.buf.push_str(escape1("setaf", color as int)); } fn bg_color (&mut self, color: Color) { + self.state.bg = Some(color); self.buf.push_str(escape1("setab", color as int)); } - fn reset_color (&mut self) { - self.buf.push_str(escape("op")); - } - fn underline (&mut self, enabled: bool) { + self.state.underline = enabled; if enabled { self.buf.push_str(escape("smul")); } @@ -155,6 +191,7 @@ impl Writer { } fn standout (&mut self, enabled: bool) { + self.state.standout = enabled; if enabled { self.buf.push_str(escape("smso")); } @@ -163,6 +200,80 @@ impl Writer { } } + fn reverse (&mut self, enabled: bool) { + if self.state.reverse != enabled { + if self.state.reverse { + self.apply_state(); + } + else { + self.buf.push_str(escape("rev")); + } + self.state.reverse = enabled; + } + } + + fn bold (&mut self, enabled: bool) { + if self.state.bold != enabled { + if self.state.bold { + self.apply_state(); + } + else { + self.buf.push_str(escape("bold")); + } + self.state.bold = enabled; + } + } + + fn blink (&mut self, enabled: bool) { + if self.state.blink != enabled { + if self.state.blink { + self.apply_state(); + } + else { + self.buf.push_str(escape("blink")); + } + self.state.blink = enabled; + } + } + + fn reset_color (&mut self) { + self.state.fg = None; + self.state.bg = None; + self.buf.push_str(escape("op")); + } + + fn reset_attributes (&mut self) { + self.state = AttrState(); + self.apply_state(); + } + + fn apply_state (&mut self) { + self.buf.push_str(escape("sgr0")); + match self.state.fg { + Some(c) => self.fg_color(c), + None => (), + } + match self.state.bg { + Some(c) => self.bg_color(c), + None => (), + } + if self.state.underline { + self.underline(true); + } + if self.state.standout { + self.standout(true); + } + if self.state.reverse { + self.reverse(true); + } + if self.state.bold { + self.bold(true); + } + if self.state.blink { + self.blink(true); + } + } + fn cursor (&mut self, enabled: bool) { if enabled { self.buf.push_str(escape("civis")); -- cgit v1.2.3