Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(218)

Side by Side Diff: sdk/lib/io/stdio.dart

Issue 807833002: Revert "Make stdout/stderr async" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove console and fix some issues Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart.io; 5 part of dart.io;
6 6
7 const int _STDIO_HANDLE_TYPE_TERMINAL = 0; 7 const int _STDIO_HANDLE_TYPE_TERMINAL = 0;
8 const int _STDIO_HANDLE_TYPE_PIPE = 1; 8 const int _STDIO_HANDLE_TYPE_PIPE = 1;
9 const int _STDIO_HANDLE_TYPE_FILE = 2; 9 const int _STDIO_HANDLE_TYPE_FILE = 2;
10 const int _STDIO_HANDLE_TYPE_SOCKET = 3; 10 const int _STDIO_HANDLE_TYPE_SOCKET = 3;
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 * Synchronously read a byte from stdin. This call will block until a byte is 142 * Synchronously read a byte from stdin. This call will block until a byte is
143 * available. 143 * available.
144 * 144 *
145 * If at end of file, -1 is returned. 145 * If at end of file, -1 is returned.
146 */ 146 */
147 external int readByteSync(); 147 external int readByteSync();
148 } 148 }
149 149
150 150
151 /** 151 /**
152 * [Stdout] exposes methods to query the terminal for properties. 152 * [Stdout] represents the [IOSink] for either `stdout` or `stderr`.
153 * 153 *
154 * Use [hasTerminal] to test if there is a terminal associated to stdout. 154 * It provides a *blocking* `IOSink`, so using this to write will block until
155 * the output is written.
156 *
157 * 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.
158 * provide the same non-blocking behavior as dart:io in general exposes.
159 * Use the property [nonBlocking] to get an `IOSink` which has the non-blocking
160 * behavior.
161 *
162 * This class can also be used to check whether `stdout` or `stderr` is
163 * connected to a terminal and query some terminal properties.
155 */ 164 */
156 class Stdout extends _StdSink implements IOSink { 165 class Stdout extends _StdSink implements IOSink {
157 Stdout._(IOSink sink) : super(sink); 166 final int _fd;
167 IOSink _nonBlocking;
168
169 Stdout._(IOSink sink, this._fd) : super(sink);
158 170
159 /** 171 /**
160 * Returns true if there is a terminal attached to stdout. 172 * Returns true if there is a terminal attached to stdout.
161 */ 173 */
162 external bool get hasTerminal; 174 bool get hasTerminal => _hasTerminal(_fd);
163 175
164 /** 176 /**
165 * Get the number of columns of the terminal. 177 * Get the number of columns of the terminal.
166 * 178 *
167 * If no terminal is attached to stdout, a [StdoutException] is thrown. See 179 * If no terminal is attached to stdout, a [StdoutException] is thrown. See
168 * [hasTerminal] for more info. 180 * [hasTerminal] for more info.
169 */ 181 */
170 external int get terminalColumns; 182 int get terminalColumns => _terminalColumns(_fd);
171 183
172 /** 184 /**
173 * Get the number of lines of the terminal. 185 * Get the number of lines of the terminal.
174 * 186 *
175 * If no terminal is attached to stdout, a [StdoutException] is thrown. See 187 * If no terminal is attached to stdout, a [StdoutException] is thrown. See
176 * [hasTerminal] for more info. 188 * [hasTerminal] for more info.
177 */ 189 */
178 external int get terminalLines; 190 int get terminalLines => _terminalLines(_fd);
191
192 external bool _hasTerminal(int fd);
193 external int _terminalColumns(int fd);
194 external int _terminalLines(int fd);
195
196 /**
197 * Get a non-blocking `IOSink`.
198 */
199 IOSink get nonBlocking {
200 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.
201 if (_nonBlocking == null) {
202 _nonBlocking = new IOSink(new _FileStreamConsumer.fromStdio(_fd));
203 }
204 return _nonBlocking;
205 }
179 } 206 }
180 207
181 208
182 class StdoutException implements IOException { 209 class StdoutException implements IOException {
183 final String message; 210 final String message;
184 final OSError osError; 211 final OSError osError;
185 212
186 const StdoutException(this.message, [this.osError]); 213 const StdoutException(this.message, [this.osError]);
187 214
188 String toString() { 215 String toString() {
189 return "StdoutException: $message${osError == null ? "" : ", $osError"}"; 216 return "StdoutException: $message${osError == null ? "" : ", $osError"}";
190 } 217 }
191 } 218 }
192 219
220 class _StdConsumer implements StreamConsumer<List<int>> {
221 final _file;
222
223 _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
224
225 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
226 var completer = new Completer();
227 var sub;
228 sub = stream.listen(
229 (data) {
230 try {
231 _file.writeFromSync(data);
232 } catch (e, s) {
233 sub.cancel();
234 completer.completeError(e, s);
235 }
236 },
237 onError: completer.completeError,
238 onDone: completer.complete,
239 cancelOnError: true);
240 return completer.future;
241 }
242
243 Future close() {
244 _file.closeSync();
245 return new Future.value();
246 }
247 }
193 248
194 class _StdSink implements IOSink { 249 class _StdSink implements IOSink {
195 final IOSink _sink; 250 final IOSink _sink;
196 251
197 _StdSink(this._sink); 252 _StdSink(this._sink);
198 253
199 Encoding get encoding => _sink.encoding; 254 Encoding get encoding => _sink.encoding;
200 void set encoding(Encoding encoding) { 255 void set encoding(Encoding encoding) {
201 _sink.encoding = encoding; 256 _sink.encoding = encoding;
202 } 257 }
(...skipping 17 matching lines...) Expand all
220 static const StdioType FILE = const StdioType._("file"); 275 static const StdioType FILE = const StdioType._("file");
221 static const StdioType OTHER = const StdioType._("other"); 276 static const StdioType OTHER = const StdioType._("other");
222 final String name; 277 final String name;
223 const StdioType._(this.name); 278 const StdioType._(this.name);
224 String toString() => "StdioType: $name"; 279 String toString() => "StdioType: $name";
225 } 280 }
226 281
227 282
228 Stdin _stdin; 283 Stdin _stdin;
229 Stdout _stdout; 284 Stdout _stdout;
230 IOSink _stderr; 285 Stdout _stderr;
231 286
232 287
233 /// The standard input stream of data read by this program. 288 /// The standard input stream of data read by this program.
234 Stdin get stdin { 289 Stdin get stdin {
235 if (_stdin == null) { 290 if (_stdin == null) {
236 _stdin = _StdIOUtils._getStdioInputStream(); 291 _stdin = _StdIOUtils._getStdioInputStream();
237 } 292 }
238 return _stdin; 293 return _stdin;
239 } 294 }
240 295
241 296
242 /// The standard output stream of data written by this program. 297 /// The standard output stream of data written by this program.
243 Stdout get stdout { 298 Stdout get stdout {
244 if (_stdout == null) { 299 if (_stdout == null) {
245 _stdout = _StdIOUtils._getStdioOutputStream(1); 300 _stdout = _StdIOUtils._getStdioOutputStream(1);
246 } 301 }
247 return _stdout; 302 return _stdout;
248 } 303 }
249 304
250 305
251 /// The standard output stream of errors written by this program. 306 /// The standard output stream of errors written by this program.
252 IOSink get stderr { 307 Stdout get stderr {
253 if (_stderr == null) { 308 if (_stderr == null) {
254 _stderr = _StdIOUtils._getStdioOutputStream(2); 309 _stderr = _StdIOUtils._getStdioOutputStream(2);
255 } 310 }
256 return _stderr; 311 return _stderr;
257 } 312 }
258 313
259 314
260 /// For a stream, returns whether it is attached to a file, pipe, terminal, or 315 /// For a stream, returns whether it is attached to a file, pipe, terminal, or
261 /// something else. 316 /// something else.
262 StdioType stdioType(object) { 317 StdioType stdioType(object) {
(...skipping 28 matching lines...) Expand all
291 return StdioType.OTHER; 346 return StdioType.OTHER;
292 } 347 }
293 348
294 349
295 class _StdIOUtils { 350 class _StdIOUtils {
296 external static _getStdioOutputStream(int fd); 351 external static _getStdioOutputStream(int fd);
297 external static Stdin _getStdioInputStream(); 352 external static Stdin _getStdioInputStream();
298 external static int _socketType(nativeSocket); 353 external static int _socketType(nativeSocket);
299 external static _getStdioHandleType(int fd); 354 external static _getStdioHandleType(int fd);
300 } 355 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698