about summary refs log tree commit diff
path: root/src/libsyntax/diagnostic.rs
diff options
context:
space:
mode:
authorHanno Braun <mail@hannobraun.de>2014-05-08 13:10:03 +0000
committerHanno Braun <mail@hannobraun.de>2014-05-15 13:12:53 +0000
commitb7676f2df5e1ff97e0baa3f7e70936d4bd4dacb5 (patch)
treea70df2c0d394b84d3741321d22bc91888a75d541 /src/libsyntax/diagnostic.rs
parent579e0a5f5555ed926cf9378ef61b034b0c316e76 (diff)
downloadrust-b7676f2df5e1ff97e0baa3f7e70936d4bd4dacb5.tar.gz
rust-b7676f2df5e1ff97e0baa3f7e70936d4bd4dacb5.zip
Add compiler flag to configure output coloring
This adds the flag --color, which allows the user to force coloring or
turn it off. The default behavior stays the same as before (colorize, if
output goes to tty).
Why this is beneficial is explained in issue #12881.

Please note that this commit doesn't include any regression tests. I
thought about how I'd write a test for this and it doesn't seem to be
worth the effort to me for a UI change like this.

Fixes #12881.
Diffstat (limited to 'src/libsyntax/diagnostic.rs')
-rw-r--r--src/libsyntax/diagnostic.rs22
1 files changed, 18 insertions, 4 deletions
diff --git a/src/libsyntax/diagnostic.rs b/src/libsyntax/diagnostic.rs
index 73027013090..94132988d97 100644
--- a/src/libsyntax/diagnostic.rs
+++ b/src/libsyntax/diagnostic.rs
@@ -49,6 +49,13 @@ impl RenderSpan {
     }
 }
 
+#[deriving(Clone)]
+pub enum ColorConfig {
+    Auto,
+    Always,
+    Never
+}
+
 pub trait Emitter {
     fn emit(&mut self, cmsp: Option<(&codemap::CodeMap, Span)>,
             msg: &str, lvl: Level);
@@ -176,8 +183,8 @@ pub fn mk_span_handler(handler: Handler, cm: codemap::CodeMap) -> SpanHandler {
     }
 }
 
-pub fn default_handler() -> Handler {
-    mk_handler(box EmitterWriter::stderr())
+pub fn default_handler(color_config: ColorConfig) -> Handler {
+    mk_handler(box EmitterWriter::stderr(color_config))
 }
 
 pub fn mk_handler(e: Box<Emitter:Send>) -> Handler {
@@ -257,9 +264,16 @@ enum Destination {
 }
 
 impl EmitterWriter {
-    pub fn stderr() -> EmitterWriter {
+    pub fn stderr(color_config: ColorConfig) -> EmitterWriter {
         let stderr = io::stderr();
-        if stderr.get_ref().isatty() {
+
+        let use_color = match color_config {
+            Always => true,
+            Never  => false,
+            Auto   => stderr.get_ref().isatty()
+        };
+
+        if use_color {
             let dst = match term::Terminal::new(stderr.unwrap()) {
                 Ok(t) => Terminal(t),
                 Err(..) => Raw(box io::stderr()),