Skip to content

Commit cdd052f

Browse files
dgryskicatamorphism
authored andcommitted
core::io::writer : add get_type() method
The get_type() method can hint to users what kind of item might be under the hood.
1 parent 872ef0f commit cdd052f

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/libcore/io.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,20 +330,25 @@ fn with_str_reader<T>(s: ~str, f: fn(reader) -> T) -> T {
330330
// Writing
331331
enum fileflag { append, create, truncate, no_flag, }
332332

333+
// What type of writer are we?
334+
enum writer_type { screen, file }
335+
333336
// FIXME (#2004): Seekable really should be orthogonal.
334337
// FIXME (#2004): eventually u64
335338
iface writer {
336339
fn write(v: &[const u8]);
337340
fn seek(int, seek_style);
338341
fn tell() -> uint;
339342
fn flush() -> int;
343+
fn get_type() -> writer_type;
340344
}
341345

342346
impl <T: writer, C> of writer for {base: T, cleanup: C} {
343347
fn write(bs: &[const u8]) { self.base.write(bs); }
344348
fn seek(off: int, style: seek_style) { self.base.seek(off, style); }
345349
fn tell() -> uint { self.base.tell() }
346350
fn flush() -> int { self.base.flush() }
351+
fn get_type() -> writer_type { file }
347352
}
348353

349354
impl of writer for *libc::FILE {
@@ -364,6 +369,13 @@ impl of writer for *libc::FILE {
364369
}
365370
fn tell() -> uint { libc::ftell(self) as uint }
366371
fn flush() -> int { libc::fflush(self) as int }
372+
fn get_type() -> writer_type {
373+
let fd = libc::fileno(self);
374+
if libc::isatty(fd) == 0 {
375+
ret file;
376+
}
377+
ret screen;
378+
}
367379
}
368380

369381
fn FILE_writer(f: *libc::FILE, cleanup: bool) -> writer {
@@ -399,6 +411,9 @@ impl of writer for fd_t {
399411
fail;
400412
}
401413
fn flush() -> int { 0 }
414+
fn get_type() -> writer_type {
415+
if libc::isatty(self) == 0 { file } else { screen }
416+
}
402417
}
403418

404419
class fd_res {
@@ -646,6 +661,7 @@ impl of writer for mem_buffer {
646661
}
647662
fn tell() -> uint { self.pos }
648663
fn flush() -> int { 0 }
664+
fn get_type() -> writer_type { ret file }
649665
}
650666
651667
fn mem_buffer() -> mem_buffer {

src/libstd/net_tcp.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,9 @@ impl tcp_socket_buf of io::writer for @tcp_socket_buf {
840840
fn flush() -> int {
841841
0
842842
}
843+
fn get_type() -> io::writer_type {
844+
io::file
845+
}
843846
}
844847

845848
// INTERNAL API

0 commit comments

Comments
 (0)