[dart:io] Adds Stdin.hasTerminal to mirror Stdout.hasTerminal

fixes #29083

Change-Id: I5f4d7ac2a5df9600fd3ad12abc2dd6068d9980af
Reviewed-on: https://dart-review.googlesource.com/23145
Reviewed-by: Ryan Macnak <rmacnak@google.com>
This commit is contained in:
Zachary Anderson 2017-11-22 22:41:37 +00:00 committed by Zach Anderson
parent 501872471c
commit 616215df1b
3 changed files with 44 additions and 1 deletions

View file

@ -54,7 +54,7 @@
`ascii`, `base64`, `base64Uri`, `json`, `latin1` and `utf8`.
* Renamed the `HtmlEscapeMode` constants `UNKNOWN`, `ATTRIBUTE`,
`SQ_ATTRIBUTE` and `ELEMENT` to `unknown`, `attribute`, `sqAttribute` and
`elements.
`elements`.
* `dart:developer`
* `Timeline.startSync` and `Timeline.timeSync` now accept an optional
@ -69,6 +69,8 @@
methods are now supported on iOS and OSX.
* Deprecated `SecurityContext.alpnSupported` as ALPN is now supported on all
platforms.
* Added `withTrustedRoots` named optional parameter to `SecurityContext`
constructor, which defaults to false.
* Added a `timeout` parameter to `Socket.connect`, `RawSocket.connect`,
`SecureSocket.connect` and `RawSecureSocket.connect`. If a connection attempt
takes longer than the duration specified in `timeout`, a `SocketException`
@ -81,6 +83,7 @@
decompression routines.
* Added `IOOverrides` and `HttpOverrides` to aid in writing tests that wish to
mock varios `dart:io` objects.
# Added `Stdin.hasTerminal`.
* `dart.math`
* Renamed `E`, `LN10`, `LN`, `LOG2E`, `LOG10E`, `PI`, `SQRT1_2` and `SQRT2`

View file

@ -167,6 +167,20 @@ class Stdin extends _StdStream implements Stream<List<int>> {
* If at end of file, -1 is returned.
*/
external int readByteSync();
/**
* Returns true if there is a terminal attached to stdin.
*/
bool get hasTerminal {
try {
return stdioType(this) == StdioType.TERMINAL;
} on FileSystemException catch (_) {
// If stdioType throws a FileSystemException, then it is not hooked up to
// a terminal, probably because it is closed, but let other exception
// types bubble up.
return false;
}
}
}
/**

View file

@ -0,0 +1,26 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "dart:io";
import "package:expect/expect.dart";
void fiddleWithEchoMode() {
final bool echoMode = stdin.echoMode;
stdin.echoMode = false;
stdin.echoMode = true;
stdin.echoMode = echoMode;
}
void main() {
Expect.isNotNull(stdin.hasTerminal);
Expect.isTrue(stdin.hasTerminal is bool);
if (stdin.hasTerminal) {
fiddleWithEchoMode();
} else {
Expect.throws(() {
fiddleWithEchoMode();
}, (e) => e is StdinException);
}
}