Chromium Code Reviews| Index: sdk/lib/io/stdio.dart |
| diff --git a/sdk/lib/io/stdio.dart b/sdk/lib/io/stdio.dart |
| index e4bdf01ff05c7bb0b6d68ba8c53d1a743d319d0c..8cfb090a2560a044b66c366cb1503a2c3b575297 100644 |
| --- a/sdk/lib/io/stdio.dart |
| +++ b/sdk/lib/io/stdio.dart |
| @@ -149,17 +149,29 @@ class Stdin extends _StdStream implements Stream<List<int>> { |
| /** |
| - * [Stdout] exposes methods to query the terminal for properties. |
| + * [Stdout] represents the [IOSink] for either `stdout` or `stderr`. |
| * |
| - * Use [hasTerminal] to test if there is a terminal associated to stdout. |
| + * It provides a *blocking* `IOSink`, so using this to write will block until |
| + * the output is written. |
| + * |
| + * In some situations this blocking behavior is undesirable as it does no |
|
Anders Johnsen
2015/01/09 12:35:11
no -> not
Søren Gjesse
2015/01/14 10:12:21
Done.
|
| + * provide the same non-blocking behavior as dart:io in general exposes. |
| + * Use the property [nonBlocking] to get an `IOSink` which has the non-blocking |
| + * behavior. |
| + * |
| + * This class can also be used to check whether `stdout` or `stderr` is |
| + * connected to a terminal and query some terminal properties. |
| */ |
| class Stdout extends _StdSink implements IOSink { |
| - Stdout._(IOSink sink) : super(sink); |
| + final int _fd; |
| + IOSink _nonBlocking; |
| + |
| + Stdout._(IOSink sink, this._fd) : super(sink); |
| /** |
| * Returns true if there is a terminal attached to stdout. |
| */ |
| - external bool get hasTerminal; |
| + bool get hasTerminal => _hasTerminal(_fd); |
| /** |
| * Get the number of columns of the terminal. |
| @@ -167,7 +179,7 @@ class Stdout extends _StdSink implements IOSink { |
| * If no terminal is attached to stdout, a [StdoutException] is thrown. See |
| * [hasTerminal] for more info. |
| */ |
| - external int get terminalColumns; |
| + int get terminalColumns => _terminalColumns(_fd); |
| /** |
| * Get the number of lines of the terminal. |
| @@ -175,7 +187,22 @@ class Stdout extends _StdSink implements IOSink { |
| * If no terminal is attached to stdout, a [StdoutException] is thrown. See |
| * [hasTerminal] for more info. |
| */ |
| - external int get terminalLines; |
| + int get terminalLines => _terminalLines(_fd); |
| + |
| + external bool _hasTerminal(int fd); |
| + external int _terminalColumns(int fd); |
| + external int _terminalLines(int fd); |
| + |
| + /** |
| + * Get a non-blocking `IOSink`. |
| + */ |
| + IOSink get nonBlocking { |
| + if (_fd == null) return _sink; |
|
Anders Johnsen
2015/01/09 12:35:11
when is _fd null?
Søren Gjesse
2015/01/14 10:12:21
Should never be. Removed.
|
| + if (_nonBlocking == null) { |
| + _nonBlocking = new IOSink(new _FileStreamConsumer.fromStdio(_fd)); |
| + } |
| + return _nonBlocking; |
| + } |
| } |
| @@ -190,6 +217,34 @@ class StdoutException implements IOException { |
| } |
| } |
| +class _StdConsumer implements StreamConsumer<List<int>> { |
| + final _file; |
| + |
| + _StdConsumer(int fd) : _file = _File._openStdioSync(fd); |
|
Anders Johnsen
2015/01/09 12:35:11
Perhaps '_open*' is the wrong name, as the fd is a
Søren Gjesse
2015/01/14 10:12:21
Sure, but these are the names we already used. I c
|
| + |
| + Future addStream(Stream<List<int>> stream) { |
|
Anders Johnsen
2015/01/09 12:35:11
This makes me wonder if
void main() {
stdout.wr
Søren Gjesse
2015/01/14 10:12:21
It prints
foo
bar
This is basically the code whi
|
| + var completer = new Completer(); |
| + var sub; |
| + sub = stream.listen( |
| + (data) { |
| + try { |
| + _file.writeFromSync(data); |
| + } catch (e, s) { |
| + sub.cancel(); |
| + completer.completeError(e, s); |
| + } |
| + }, |
| + onError: completer.completeError, |
| + onDone: completer.complete, |
| + cancelOnError: true); |
| + return completer.future; |
| + } |
| + |
| + Future close() { |
| + _file.closeSync(); |
| + return new Future.value(); |
| + } |
| +} |
| class _StdSink implements IOSink { |
| final IOSink _sink; |
| @@ -227,7 +282,7 @@ class StdioType { |
| Stdin _stdin; |
| Stdout _stdout; |
| -IOSink _stderr; |
| +Stdout _stderr; |
| /// The standard input stream of data read by this program. |
| @@ -249,7 +304,7 @@ Stdout get stdout { |
| /// The standard output stream of errors written by this program. |
| -IOSink get stderr { |
| +Stdout get stderr { |
| if (_stderr == null) { |
| _stderr = _StdIOUtils._getStdioOutputStream(2); |
| } |