Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/libcore/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,20 +330,25 @@ fn with_str_reader<T>(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 {
fn write(v: &[const u8]);
fn seek(int, seek_style);
fn tell() -> uint;
fn flush() -> int;
fn get_type() -> writer_type;
}

impl <T: writer, C> of writer for {base: T, cleanup: C} {
fn write(bs: &[const u8]) { self.base.write(bs); }
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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/libstd/net_tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/libsyntax/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
1 change: 1 addition & 0 deletions src/test/bench/shootout-mandelbrot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<comm::chan<line>>, size: uint)
Expand Down