diff --git a/src/libcore/io.rs b/src/libcore/io.rs index 3704f2b70da48..8485818e2d606 100644 --- a/src/libcore/io.rs +++ b/src/libcore/io.rs @@ -330,6 +330,9 @@ fn with_str_reader(s: ~str, f: fn(reader) -> T) -> T { // Writing enum fileflag { append, create, truncate, no_flag, } +// What type of writer are we? +enum writer_type { screen, file } + // FIXME (#2004): Seekable really should be orthogonal. // FIXME (#2004): eventually u64 iface writer { @@ -337,6 +340,7 @@ iface writer { fn seek(int, seek_style); fn tell() -> uint; fn flush() -> int; + fn get_type() -> writer_type; } impl of writer for {base: T, cleanup: C} { @@ -344,6 +348,7 @@ impl of writer for {base: T, cleanup: C} { fn seek(off: int, style: seek_style) { self.base.seek(off, style); } fn tell() -> uint { self.base.tell() } fn flush() -> int { self.base.flush() } + fn get_type() -> writer_type { file } } impl of writer for *libc::FILE { @@ -364,6 +369,13 @@ impl of writer for *libc::FILE { } fn tell() -> uint { libc::ftell(self) as uint } fn flush() -> int { libc::fflush(self) as int } + fn get_type() -> writer_type { + let fd = libc::fileno(self); + if libc::isatty(fd) == 0 { + ret file; + } + ret screen; + } } fn FILE_writer(f: *libc::FILE, cleanup: bool) -> writer { @@ -399,6 +411,9 @@ impl of writer for fd_t { fail; } fn flush() -> int { 0 } + fn get_type() -> writer_type { + if libc::isatty(self) == 0 { file } else { screen } + } } class fd_res { @@ -646,6 +661,7 @@ impl of writer for mem_buffer { } fn tell() -> uint { self.pos } fn flush() -> int { 0 } + fn get_type() -> writer_type { ret file } } fn mem_buffer() -> mem_buffer { diff --git a/src/libstd/net_tcp.rs b/src/libstd/net_tcp.rs index 4826bc760c237..438faa0c4faff 100644 --- a/src/libstd/net_tcp.rs +++ b/src/libstd/net_tcp.rs @@ -840,6 +840,9 @@ impl tcp_socket_buf of io::writer for @tcp_socket_buf { fn flush() -> int { 0 } + fn get_type() -> io::writer_type { + io::file + } } // INTERNAL API diff --git a/src/libsyntax/diagnostic.rs b/src/libsyntax/diagnostic.rs index cda49de8dd076..6178dfb616e5a 100644 --- a/src/libsyntax/diagnostic.rs +++ b/src/libsyntax/diagnostic.rs @@ -163,14 +163,16 @@ fn diagnosticcolor(lvl: level) -> u8 { } fn print_diagnostic(topic: ~str, lvl: level, msg: ~str) { + let use_color = term::color_supported() && + io::stderr().get_type() == io::screen; if str::is_not_empty(topic) { io::stderr().write_str(#fmt["%s ", topic]); } - if term::color_supported() { + if use_color { term::fg(io::stderr(), diagnosticcolor(lvl)); } io::stderr().write_str(#fmt["%s:", diagnosticstr(lvl)]); - if term::color_supported() { + if use_color { term::reset(io::stderr()); } io::stderr().write_str(#fmt[" %s\n", msg]); diff --git a/src/test/bench/shootout-mandelbrot.rs b/src/test/bench/shootout-mandelbrot.rs index 7385313d53a9a..6cf51513c77fb 100644 --- a/src/test/bench/shootout-mandelbrot.rs +++ b/src/test/bench/shootout-mandelbrot.rs @@ -88,6 +88,7 @@ impl of io::writer for devnull { fn seek(_i: int, _s: io::seek_style) {} fn tell() -> uint {0_u} fn flush() -> int {0} + fn get_type() -> io::writer_type { io::file } } fn writer(path: ~str, writech: comm::chan>, size: uint)