2012-07-04 22:53:12 +01:00
|
|
|
//! Simple ANSI color library
|
2011-07-11 11:50:02 -07:00
|
|
|
|
2011-12-13 16:25:51 -08:00
|
|
|
import core::option;
|
|
|
|
|
2012-07-05 15:58:25 -07:00
|
|
|
// FIXME (#2807): Windows support.
|
2011-07-11 11:50:02 -07:00
|
|
|
|
2011-10-27 14:54:18 -07:00
|
|
|
const color_black: u8 = 0u8;
|
2011-07-27 14:19:39 +02:00
|
|
|
const color_red: u8 = 1u8;
|
|
|
|
const color_green: u8 = 2u8;
|
|
|
|
const color_yellow: u8 = 3u8;
|
|
|
|
const color_blue: u8 = 4u8;
|
|
|
|
const color_magenta: u8 = 5u8;
|
|
|
|
const color_cyan: u8 = 6u8;
|
|
|
|
const color_light_gray: u8 = 7u8;
|
|
|
|
const color_light_grey: u8 = 7u8;
|
|
|
|
const color_dark_gray: u8 = 8u8;
|
|
|
|
const color_dark_grey: u8 = 8u8;
|
|
|
|
const color_bright_red: u8 = 9u8;
|
|
|
|
const color_bright_green: u8 = 10u8;
|
|
|
|
const color_bright_yellow: u8 = 11u8;
|
|
|
|
const color_bright_blue: u8 = 12u8;
|
|
|
|
const color_bright_magenta: u8 = 13u8;
|
|
|
|
const color_bright_cyan: u8 = 14u8;
|
|
|
|
const color_bright_white: u8 = 15u8;
|
2011-07-11 11:50:02 -07:00
|
|
|
|
2012-08-14 13:38:35 -07:00
|
|
|
fn esc(writer: io::Writer) { writer.write(~[0x1bu8, '[' as u8]); }
|
2011-07-11 11:50:02 -07:00
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/// Reset the foreground and background colors to default
|
2012-08-14 13:38:35 -07:00
|
|
|
fn reset(writer: io::Writer) {
|
2011-07-11 11:50:02 -07:00
|
|
|
esc(writer);
|
2012-06-29 16:26:56 -07:00
|
|
|
writer.write(~['0' as u8, 'm' as u8]);
|
2011-07-11 11:50:02 -07:00
|
|
|
}
|
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/// Returns true if the terminal supports color
|
2011-07-11 11:50:02 -07:00
|
|
|
fn color_supported() -> bool {
|
2012-07-13 22:57:48 -07:00
|
|
|
let supported_terms = ~[~"xterm-color", ~"xterm",
|
|
|
|
~"screen-bce", ~"xterm-256color"];
|
2012-08-06 12:34:08 -07:00
|
|
|
return match os::getenv(~"TERM") {
|
2012-08-03 19:59:04 -07:00
|
|
|
option::some(env) => {
|
2012-06-30 16:19:07 -07:00
|
|
|
for vec::each(supported_terms) |term| {
|
2012-08-02 15:42:56 -07:00
|
|
|
if term == env { return true; }
|
2011-07-12 13:58:48 -07:00
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
option::none => false
|
2011-07-11 11:50:02 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-08-14 13:38:35 -07:00
|
|
|
fn set_color(writer: io::Writer, first_char: u8, color: u8) {
|
2011-07-11 11:50:02 -07:00
|
|
|
assert (color < 16u8);
|
|
|
|
esc(writer);
|
2012-03-14 14:03:56 -04:00
|
|
|
let mut color = color;
|
2012-06-29 16:26:56 -07: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 11:50:02 -07:00
|
|
|
}
|
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/// Set the foreground color
|
2012-08-14 13:38:35 -07:00
|
|
|
fn fg(writer: io::Writer, color: u8) {
|
2012-08-01 17:30:05 -07:00
|
|
|
return set_color(writer, '3' as u8, color);
|
2011-07-11 11:50:02 -07:00
|
|
|
}
|
|
|
|
|
2012-07-04 22:53:12 +01:00
|
|
|
/// Set the background color
|
2012-08-14 13:38:35 -07:00
|
|
|
fn bg(writer: io::Writer, color: u8) {
|
2012-08-01 17:30:05 -07:00
|
|
|
return set_color(writer, '4' as u8, color);
|
2011-07-11 11:50:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|