2013-05-30 01:13:35 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
//! Simple ANSI color library
|
2011-07-11 13:50:02 -05:00
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
#[allow(missing_doc)];
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::io;
|
|
|
|
use core::os;
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2013-05-30 01:13:35 -05:00
|
|
|
use terminfo::*;
|
|
|
|
use terminfo::searcher::open;
|
|
|
|
use terminfo::parser::compiled::parse;
|
|
|
|
use terminfo::parm::{expand, Number};
|
|
|
|
|
2012-07-05 17:58:25 -05:00
|
|
|
// FIXME (#2807): Windows support.
|
2011-07-11 13:50:02 -05:00
|
|
|
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static color_black: u8 = 0u8;
|
|
|
|
pub static color_red: u8 = 1u8;
|
|
|
|
pub static color_green: u8 = 2u8;
|
|
|
|
pub static color_yellow: u8 = 3u8;
|
|
|
|
pub static color_blue: u8 = 4u8;
|
|
|
|
pub static color_magenta: u8 = 5u8;
|
|
|
|
pub static color_cyan: u8 = 6u8;
|
|
|
|
pub static color_light_gray: u8 = 7u8;
|
|
|
|
pub static color_light_grey: u8 = 7u8;
|
|
|
|
pub static color_dark_gray: u8 = 8u8;
|
|
|
|
pub static color_dark_grey: u8 = 8u8;
|
|
|
|
pub static color_bright_red: u8 = 9u8;
|
|
|
|
pub static color_bright_green: u8 = 10u8;
|
|
|
|
pub static color_bright_yellow: u8 = 11u8;
|
|
|
|
pub static color_bright_blue: u8 = 12u8;
|
|
|
|
pub static color_bright_magenta: u8 = 13u8;
|
|
|
|
pub static color_bright_cyan: u8 = 14u8;
|
|
|
|
pub static color_bright_white: u8 = 15u8;
|
2011-07-11 13:50:02 -05:00
|
|
|
|
2013-05-30 18:14:40 -05:00
|
|
|
#[cfg(not(target_os = "win32"))]
|
2013-05-30 01:13:35 -05:00
|
|
|
pub struct Terminal {
|
|
|
|
color_supported: bool,
|
|
|
|
priv out: @io::Writer,
|
|
|
|
priv ti: ~TermInfo
|
2011-07-11 13:50:02 -05:00
|
|
|
}
|
|
|
|
|
2013-05-30 18:14:40 -05:00
|
|
|
#[cfg(target_os = "win32")]
|
|
|
|
pub struct Terminal {
|
|
|
|
color_supported: bool,
|
|
|
|
priv out: @io::Writer,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "win32"))]
|
2013-06-03 18:56:35 -05:00
|
|
|
impl Terminal {
|
2013-05-30 01:13:35 -05:00
|
|
|
pub fn new(out: @io::Writer) -> Result<Terminal, ~str> {
|
|
|
|
let term = os::getenv("TERM");
|
|
|
|
if term.is_none() {
|
|
|
|
return Err(~"TERM environment variable undefined");
|
|
|
|
}
|
2011-07-11 13:50:02 -05:00
|
|
|
|
2013-05-30 01:13:35 -05:00
|
|
|
let entry = open(term.unwrap());
|
|
|
|
if entry.is_err() {
|
|
|
|
return Err(entry.get_err());
|
|
|
|
}
|
2011-07-11 13:50:02 -05:00
|
|
|
|
2013-05-30 01:13:35 -05:00
|
|
|
let ti = parse(entry.get(), false);
|
|
|
|
if ti.is_err() {
|
|
|
|
return Err(entry.get_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut inf = ti.get();
|
2013-05-31 12:37:58 -05:00
|
|
|
let cs = *inf.numbers.find_or_insert(~"colors", 0) >= 16
|
|
|
|
&& inf.strings.find(&~"setaf").is_some()
|
2013-05-30 01:19:03 -05:00
|
|
|
&& inf.strings.find_equiv(&("setab")).is_some();
|
2011-07-11 13:50:02 -05:00
|
|
|
|
2013-05-30 01:13:35 -05:00
|
|
|
return Ok(Terminal {out: out, ti: inf, color_supported: cs});
|
|
|
|
}
|
2013-06-03 18:56:35 -05:00
|
|
|
pub fn fg(&self, color: u8) {
|
2013-05-30 12:21:41 -05:00
|
|
|
if self.color_supported {
|
2013-05-30 18:14:40 -05:00
|
|
|
let s = expand(*self.ti.strings.find_equiv(&("setaf")).unwrap(),
|
|
|
|
[Number(color as int)], [], []);
|
|
|
|
if s.is_ok() {
|
|
|
|
self.out.write(s.get());
|
|
|
|
} else {
|
|
|
|
warn!(s.get_err());
|
|
|
|
}
|
2013-05-30 12:21:41 -05:00
|
|
|
}
|
2013-05-30 01:13:35 -05:00
|
|
|
}
|
2013-06-03 18:56:35 -05:00
|
|
|
pub fn bg(&self, color: u8) {
|
2013-05-30 12:21:41 -05:00
|
|
|
if self.color_supported {
|
2013-05-30 18:14:40 -05:00
|
|
|
let s = expand(*self.ti.strings.find_equiv(&("setab")).unwrap(),
|
|
|
|
[Number(color as int)], [], []);
|
|
|
|
if s.is_ok() {
|
|
|
|
self.out.write(s.get());
|
|
|
|
} else {
|
|
|
|
warn!(s.get_err());
|
|
|
|
}
|
2013-05-30 12:21:41 -05:00
|
|
|
}
|
2013-05-30 01:13:35 -05:00
|
|
|
}
|
2013-06-03 18:56:35 -05:00
|
|
|
pub fn reset(&self) {
|
2013-05-30 12:21:41 -05:00
|
|
|
if self.color_supported {
|
2013-05-30 18:14:40 -05:00
|
|
|
let s = expand(*self.ti.strings.find_equiv(&("op")).unwrap(), [], [], []);
|
|
|
|
if s.is_ok() {
|
|
|
|
self.out.write(s.get());
|
|
|
|
} else {
|
|
|
|
warn!(s.get_err());
|
|
|
|
}
|
2013-05-30 12:21:41 -05:00
|
|
|
}
|
2013-05-30 01:13:35 -05:00
|
|
|
}
|
2011-07-11 13:50:02 -05:00
|
|
|
}
|
2013-05-30 18:14:40 -05:00
|
|
|
|
|
|
|
#[cfg(target_os = "win32")]
|
2013-06-03 18:56:35 -05:00
|
|
|
impl Terminal {
|
2013-05-30 18:14:40 -05:00
|
|
|
pub fn new(out: @io::Writer) -> Result<Terminal, ~str> {
|
|
|
|
return Ok(Terminal {out: out, color_supported: false});
|
|
|
|
}
|
|
|
|
|
2013-06-03 18:56:35 -05:00
|
|
|
pub fn fg(&self, color: u8) {
|
2013-05-30 18:14:40 -05:00
|
|
|
}
|
|
|
|
|
2013-06-03 18:56:35 -05:00
|
|
|
pub fn bg(&self, color: u8) {
|
2013-05-30 18:14:40 -05:00
|
|
|
}
|
|
|
|
|
2013-06-03 18:56:35 -05:00
|
|
|
pub fn reset(&self) {
|
2013-05-30 18:14:40 -05:00
|
|
|
}
|
|
|
|
}
|