From 4363df120fb572877512786bdb2f28e841a8886f Mon Sep 17 00:00:00 2001 From: cheatfate Date: Wed, 30 Sep 2020 16:19:51 +0300 Subject: [PATCH] User proper method of console identification (msvcrt). --- stew/conio.nim | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/stew/conio.nim b/stew/conio.nim index c8d9cf9..7f7c3ba 100644 --- a/stew/conio.nim +++ b/stew/conio.nim @@ -51,6 +51,8 @@ when defined(windows): lpDefaultChar: pointer, lpUsedDefaultChar: pointer): cint {. importc: "WideCharToMultiByte", stdcall, dynlib: "kernel32", sideEffect.} + proc getFileType(hFile: uint): uint32 {. + importc: "GetFileType", stdcall, dynlib: "kernel32", sideEffect.} const CP_UTF8 = 65001'u32 @@ -59,20 +61,17 @@ when defined(windows): INVALID_HANDLE_VALUE = cast[uint](-1) ENABLE_PROCESSED_INPUT = 0x0001'u32 ENABLE_ECHO_INPUT = 0x0004'u32 - ERROR_INVALID_HANDLE = 0x0006'u32 + FILE_TYPE_CHAR = 0x0002'u32 proc isConsoleRedirected(hConsole: uint): bool = ## Returns ``true`` if console handle was redirected. - var mode: uint32 - let res = getConsoleMode(hConsole, mode) - if res == 0: - let errCode = ioLastError() - if errCode == ERROR_INVALID_HANDLE: - true - else: - false - else: + let res = getFileType(hConsole) + if res == FILE_TYPE_CHAR: + # The specified handle is a character device, typically an LPT device or a + # console. false + else: + true proc readConsoleInput(maxBytes: int): IoResult[string] = let hConsoleInput =