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