new method to check if valid to create Terminal;

This commit is contained in:
Adam D. Ruppe 2019-03-01 12:29:43 -05:00
parent 2fa8f7cfa9
commit 3bcce8b478
1 changed files with 19 additions and 0 deletions

View File

@ -434,6 +434,25 @@ struct Terminal {
@disable this(this);
private ConsoleOutputType type;
/++
Terminal is only valid to use on an actual console device or terminal
handle. You should not attempt to construct a Terminal instance if this
returns false;
+/
static bool stdoutIsTerminal() {
version(Posix) {
import core.sys.posix.unistd;
return cast(bool) isatty(1);
} else version(Windows) {
auto hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO originalSbi;
if(GetConsoleScreenBufferInfo(hConsole, &originalSbi) == 0)
return false;
else
return true;
} else static assert(0);
}
version(Posix) {
private int fdOut;
private int fdIn;