Skip to content

Commit 0ae203a

Browse files
lilyballemberian
authored andcommitted
Refactor extra::term a bit
Move all the colors into a nested mod named color instead of prefixing with "color_". Define a new type color::Color, and make this a u16 instead of a u8 (to allow for easy comparisons against num_colors, which is a u16). Remove color_supported and replace it with num_colors. Teach fg() and bg() to "dim" bright colors down to the normal intensity if num_colors isn't high enough. Remove unnecessary copies, and fix a bug where a terminfo parse failure would try to use the wrong error and end up failing.
1 parent 00b4138 commit 0ae203a

File tree

4 files changed

+82
-64
lines changed

4 files changed

+82
-64
lines changed

src/libextra/term.rs

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,38 @@ use core::io;
2424

2525
// FIXME (#2807): Windows support.
2626

27-
pub static color_black: u8 = 0u8;
28-
pub static color_red: u8 = 1u8;
29-
pub static color_green: u8 = 2u8;
30-
pub static color_yellow: u8 = 3u8;
31-
pub static color_blue: u8 = 4u8;
32-
pub static color_magenta: u8 = 5u8;
33-
pub static color_cyan: u8 = 6u8;
34-
pub static color_light_gray: u8 = 7u8;
35-
pub static color_light_grey: u8 = 7u8;
36-
pub static color_dark_gray: u8 = 8u8;
37-
pub static color_dark_grey: u8 = 8u8;
38-
pub static color_bright_red: u8 = 9u8;
39-
pub static color_bright_green: u8 = 10u8;
40-
pub static color_bright_yellow: u8 = 11u8;
41-
pub static color_bright_blue: u8 = 12u8;
42-
pub static color_bright_magenta: u8 = 13u8;
43-
pub static color_bright_cyan: u8 = 14u8;
44-
pub static color_bright_white: u8 = 15u8;
27+
pub mod color {
28+
pub type Color = u16;
29+
30+
pub static black: Color = 0u16;
31+
pub static red: Color = 1u16;
32+
pub static green: Color = 2u16;
33+
pub static yellow: Color = 3u16;
34+
pub static blue: Color = 4u16;
35+
pub static magenta: Color = 5u16;
36+
pub static cyan: Color = 6u16;
37+
pub static white: Color = 7u16;
38+
39+
pub static bright_black: Color = 8u16;
40+
pub static bright_red: Color = 9u16;
41+
pub static bright_green: Color = 10u16;
42+
pub static bright_yellow: Color = 11u16;
43+
pub static bright_blue: Color = 12u16;
44+
pub static bright_magenta: Color = 13u16;
45+
pub static bright_cyan: Color = 14u16;
46+
pub static bright_white: Color = 15u16;
47+
}
4548

4649
#[cfg(not(target_os = "win32"))]
4750
pub struct Terminal {
48-
color_supported: bool,
51+
num_colors: u16,
4952
priv out: @io::Writer,
5053
priv ti: ~TermInfo
5154
}
5255

5356
#[cfg(target_os = "win32")]
5457
pub struct Terminal {
55-
color_supported: bool,
58+
num_colors: u16,
5659
priv out: @io::Writer,
5760
}
5861

@@ -66,66 +69,81 @@ impl Terminal {
6669

6770
let entry = open(term.unwrap());
6871
if entry.is_err() {
69-
return Err(entry.get_err());
72+
return Err(entry.unwrap_err());
7073
}
7174

72-
let ti = parse(entry.get(), false);
75+
let ti = parse(entry.unwrap(), false);
7376
if ti.is_err() {
74-
return Err(entry.get_err());
77+
return Err(ti.unwrap_err());
7578
}
7679

77-
let mut inf = ti.get();
78-
let cs = *inf.numbers.find_or_insert(~"colors", 0) >= 16
79-
&& inf.strings.find(&~"setaf").is_some()
80-
&& inf.strings.find_equiv(&("setab")).is_some();
80+
let inf = ti.unwrap();
81+
let nc = if inf.strings.find_equiv(&("setaf")).is_some()
82+
&& inf.strings.find_equiv(&("setab")).is_some() {
83+
inf.numbers.find_equiv(&("colors")).map_consume_default(0, |&n| n)
84+
} else { 0 };
8185

82-
return Ok(Terminal {out: out, ti: inf, color_supported: cs});
86+
return Ok(Terminal {out: out, ti: inf, num_colors: nc});
8387
}
84-
pub fn fg(&self, color: u8) {
85-
if self.color_supported {
88+
/// Sets the foreground color to the given color.
89+
///
90+
/// If the color is a bright color, but the terminal only supports 8 colors,
91+
/// the corresponding normal color will be used instead.
92+
pub fn fg(&self, color: color::Color) {
93+
let color = self.dim_if_necessary(color);
94+
if self.num_colors > color {
8695
let s = expand(*self.ti.strings.find_equiv(&("setaf")).unwrap(),
8796
[Number(color as int)], &mut Variables::new());
8897
if s.is_ok() {
89-
self.out.write(s.get());
98+
self.out.write(s.unwrap());
9099
} else {
91-
warn!(s.get_err());
100+
warn!(s.unwrap_err());
92101
}
93102
}
94103
}
95-
pub fn bg(&self, color: u8) {
96-
if self.color_supported {
104+
/// Sets the background color to the given color.
105+
///
106+
/// If the color is a bright color, but the terminal only supports 8 colors,
107+
/// the corresponding normal color will be used instead.
108+
pub fn bg(&self, color: color::Color) {
109+
let color = self.dim_if_necessary(color);
110+
if self.num_colors > color {
97111
let s = expand(*self.ti.strings.find_equiv(&("setab")).unwrap(),
98112
[Number(color as int)], &mut Variables::new());
99113
if s.is_ok() {
100-
self.out.write(s.get());
114+
self.out.write(s.unwrap());
101115
} else {
102-
warn!(s.get_err());
116+
warn!(s.unwrap_err());
103117
}
104118
}
105119
}
106120
pub fn reset(&self) {
107-
if self.color_supported {
108-
let mut vars = Variables::new();
109-
let s = expand(*self.ti.strings.find_equiv(&("op")).unwrap(), [], &mut vars);
110-
if s.is_ok() {
111-
self.out.write(s.get());
112-
} else {
113-
warn!(s.get_err());
114-
}
121+
let mut vars = Variables::new();
122+
let s = expand(*self.ti.strings.find_equiv(&("op")).unwrap(), [], &mut vars);
123+
if s.is_ok() {
124+
self.out.write(s.unwrap());
125+
} else {
126+
warn!(s.unwrap_err());
115127
}
116128
}
129+
130+
priv fn dim_if_necessary(&self, color: color::Color) -> color::Color {
131+
if color >= self.num_colors && color >= 8 && color < 16 {
132+
color-8
133+
} else { color }
134+
}
117135
}
118136

119137
#[cfg(target_os = "win32")]
120138
impl Terminal {
121139
pub fn new(out: @io::Writer) -> Result<Terminal, ~str> {
122-
return Ok(Terminal {out: out, color_supported: false});
140+
return Ok(Terminal {out: out, num_colors: 0});
123141
}
124142

125-
pub fn fg(&self, _color: u8) {
143+
pub fn fg(&self, _color: color::Color) {
126144
}
127145

128-
pub fn bg(&self, _color: u8) {
146+
pub fn bg(&self, _color: color::Color) {
129147
}
130148

131149
pub fn reset(&self) {

src/libextra/test.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,33 +326,33 @@ pub fn run_tests_console(opts: &TestOpts,
326326
}
327327
328328
fn write_ok(out: @io::Writer, use_color: bool) {
329-
write_pretty(out, "ok", term::color_green, use_color);
329+
write_pretty(out, "ok", term::color::green, use_color);
330330
}
331331
332332
fn write_failed(out: @io::Writer, use_color: bool) {
333-
write_pretty(out, "FAILED", term::color_red, use_color);
333+
write_pretty(out, "FAILED", term::color::red, use_color);
334334
}
335335
336336
fn write_ignored(out: @io::Writer, use_color: bool) {
337-
write_pretty(out, "ignored", term::color_yellow, use_color);
337+
write_pretty(out, "ignored", term::color::yellow, use_color);
338338
}
339339
340340
fn write_bench(out: @io::Writer, use_color: bool) {
341-
write_pretty(out, "bench", term::color_cyan, use_color);
341+
write_pretty(out, "bench", term::color::cyan, use_color);
342342
}
343343
344344
fn write_pretty(out: @io::Writer,
345345
word: &str,
346-
color: u8,
346+
color: term::color::Color,
347347
use_color: bool) {
348348
let t = term::Terminal::new(out);
349349
match t {
350350
Ok(term) => {
351-
if use_color && term.color_supported {
351+
if use_color {
352352
term.fg(color);
353353
}
354354
out.write_str(word);
355-
if use_color && term.color_supported {
355+
if use_color {
356356
term.reset();
357357
}
358358
},

src/librustpkg/messages.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ use core::io;
1313
use core::result::*;
1414

1515
pub fn note(msg: &str) {
16-
pretty_message(msg, "note: ", term::color_green, io::stdout())
16+
pretty_message(msg, "note: ", term::color::green, io::stdout())
1717
}
1818

1919
pub fn warn(msg: &str) {
20-
pretty_message(msg, "warning: ", term::color_yellow, io::stdout())
20+
pretty_message(msg, "warning: ", term::color::yellow, io::stdout())
2121
}
2222

2323
pub fn error(msg: &str) {
24-
pretty_message(msg, "error: ", term::color_red, io::stdout())
24+
pretty_message(msg, "error: ", term::color::red, io::stdout())
2525
}
2626

27-
fn pretty_message<'a>(msg: &'a str, prefix: &'a str, color: u8, out: @io::Writer) {
27+
fn pretty_message<'a>(msg: &'a str, prefix: &'a str, color: term::color::Color, out: @io::Writer) {
2828
let term = term::Terminal::new(out);
2929
match term {
3030
Ok(ref t) => {

src/libsyntax/diagnostic.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,16 @@ fn diagnosticstr(lvl: level) -> ~str {
178178
}
179179
}
180180

181-
fn diagnosticcolor(lvl: level) -> u8 {
181+
fn diagnosticcolor(lvl: level) -> term::color::Color {
182182
match lvl {
183-
fatal => term::color_bright_red,
184-
error => term::color_bright_red,
185-
warning => term::color_bright_yellow,
186-
note => term::color_bright_green
183+
fatal => term::color::bright_red,
184+
error => term::color::bright_red,
185+
warning => term::color::bright_yellow,
186+
note => term::color::bright_green
187187
}
188188
}
189189

190-
fn print_maybe_colored(msg: &str, color: u8) {
190+
fn print_maybe_colored(msg: &str, color: term::color::Color) {
191191
let stderr = io::stderr();
192192

193193
let t = term::Terminal::new(stderr);

0 commit comments

Comments
 (0)