about summary refs log tree commit diff
path: root/src/libextra/term.rs
diff options
context:
space:
mode:
authorCorey Richardson <corey@octayn.net>2013-05-30 02:13:35 -0400
committerCorey Richardson <corey@octayn.net>2013-05-31 20:02:49 -0400
commitcf64324e19efd159fe0411f55a608f4f747e298d (patch)
tree8daee41b13b1a418501c4c8b1a34e7e0ee8b21d1 /src/libextra/term.rs
parent55c23bc55706d71e2168c0eef42f59f20e06b75f (diff)
extra::term overhaul
Diffstat (limited to 'src/libextra/term.rs')
-rw-r--r--src/libextra/term.rs73
1 files changed, 39 insertions, 34 deletions
diff --git a/src/libextra/term.rs b/src/libextra/term.rs
index a76852dc661..14d29d22a92 100644
--- a/src/libextra/term.rs
+++ b/src/libextra/term.rs
@@ -1,4 +1,4 @@
-// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
 // file at the top-level directory of this distribution and at
 // http://rust-lang.org/COPYRIGHT.
 //
@@ -15,9 +15,13 @@
 use core::prelude::*;
 
 use core::io;
-use core::option;
 use core::os;
 
+use terminfo::*;
+use terminfo::searcher::open;
+use terminfo::parser::compiled::parse;
+use terminfo::parm::{expand, Number};
+
 // FIXME (#2807): Windows support.
 
 pub static color_black: u8 = 0u8;
@@ -41,41 +45,42 @@ pub static color_bright_white: u8 = 15u8;
 
 pub fn esc(writer: @io::Writer) { writer.write([0x1bu8, '[' as u8]); }
 
-/// Reset the foreground and background colors to default
-pub fn reset(writer: @io::Writer) {
-    esc(writer);
-    writer.write(['0' as u8, 'm' as u8]);
+pub struct Terminal {
+    color_supported: bool,
+    priv out: @io::Writer,
+    priv ti: ~TermInfo
 }
 
-/// Returns true if the terminal supports color
-pub fn color_supported() -> bool {
-    let supported_terms = ~[~"xterm-color", ~"xterm",
-                           ~"screen-bce", ~"xterm-256color"];
-    return match os::getenv("TERM") {
-          option::Some(ref env) => {
-            for supported_terms.each |term| {
-                if *term == *env { return true; }
-            }
-            false
-          }
-          option::None => false
-        };
-}
+pub impl Terminal {
+    pub fn new(out: @io::Writer) -> Result<Terminal, ~str> {
+        let term = os::getenv("TERM");
+        if term.is_none() {
+            return Err(~"TERM environment variable undefined");
+        }
 
-pub fn set_color(writer: @io::Writer, first_char: u8, color: u8) {
-    assert!((color < 16u8));
-    esc(writer);
-    let mut color = color;
-    if color >= 8u8 { writer.write(['1' as u8, ';' as u8]); color -= 8u8; }
-    writer.write([first_char, ('0' as u8) + color, 'm' as u8]);
-}
+        let entry = open(term.unwrap());
+        if entry.is_err() {
+            return Err(entry.get_err());
+        }
 
-/// Set the foreground color
-pub fn fg(writer: @io::Writer, color: u8) {
-    return set_color(writer, '3' as u8, color);
-}
+        let ti = parse(entry.get(), false);
+        if ti.is_err() {
+            return Err(entry.get_err());
+        }
+
+        let mut inf = ti.get();
+        let cs = *inf.numbers.find_or_insert(~"colors", 0) >= 16 && inf.strings.find(&~"setaf").is_some()
+            && inf.strings.find(&~"setab").is_some();
 
-/// Set the background color
-pub fn bg(writer: @io::Writer, color: u8) {
-    return set_color(writer, '4' as u8, color);
+        return Ok(Terminal {out: out, ti: inf, color_supported: cs});
+    }
+    fn fg(&self, color: u8) {
+        self.out.write(expand(*self.ti.strings.find(&~"setaf").unwrap(), [Number(color as int)], [], []));
+    }
+    fn bg(&self, color: u8) {
+        self.out.write(expand(*self.ti.strings.find(&~"setab").unwrap(), [Number(color as int)], [], []));
+    }
+    fn reset(&self) {
+        self.out.write(expand(*self.ti.strings.find(&~"op").unwrap(), [], [], []));
+    }
 }