2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::io;
|
|
|
|
use core::option;
|
|
|
|
use core::os;
|
|
|
|
use core::vec;
|
2011-12-13 18:25:51 -06:00
|
|
|
|
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-03-12 15:00:50 -05:00
|
|
|
pub fn esc(writer: @io::Writer) { writer.write(~[0x1bu8, '[' as u8]); }
|
2011-07-11 13:50:02 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Reset the foreground and background colors to default
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn reset(writer: @io::Writer) {
|
2011-07-11 13:50:02 -05:00
|
|
|
esc(writer);
|
2012-06-29 18:26:56 -05:00
|
|
|
writer.write(~['0' as u8, 'm' as u8]);
|
2011-07-11 13:50:02 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns true if the terminal supports color
|
2012-09-27 20:03:00 -05:00
|
|
|
pub fn color_supported() -> bool {
|
2012-07-14 00:57:48 -05:00
|
|
|
let supported_terms = ~[~"xterm-color", ~"xterm",
|
|
|
|
~"screen-bce", ~"xterm-256color"];
|
2012-08-06 14:34:08 -05:00
|
|
|
return match os::getenv(~"TERM") {
|
2012-09-28 02:22:18 -05:00
|
|
|
option::Some(ref env) => {
|
2012-09-18 23:41:13 -05:00
|
|
|
for vec::each(supported_terms) |term| {
|
2012-09-28 02:22:18 -05:00
|
|
|
if *term == *env { return true; }
|
2011-07-12 15:58:48 -05:00
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
option::None => false
|
2011-07-11 13:50:02 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn set_color(writer: @io::Writer, first_char: u8, color: u8) {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((color < 16u8));
|
2011-07-11 13:50:02 -05:00
|
|
|
esc(writer);
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut color = color;
|
2012-06-29 18:26:56 -05:00
|
|
|
if color >= 8u8 { writer.write(~['1' as u8, ';' as u8]); color -= 8u8; }
|
|
|
|
writer.write(~[first_char, ('0' as u8) + color, 'm' as u8]);
|
2011-07-11 13:50:02 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Set the foreground color
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn fg(writer: @io::Writer, color: u8) {
|
2012-08-01 19:30:05 -05:00
|
|
|
return set_color(writer, '3' as u8, color);
|
2011-07-11 13:50:02 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Set the background color
|
2013-03-12 15:00:50 -05:00
|
|
|
pub fn bg(writer: @io::Writer, color: u8) {
|
2012-08-01 19:30:05 -05:00
|
|
|
return set_color(writer, '4' as u8, color);
|
2011-07-11 13:50:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|