User proper method of console identification (msvcrt).

This commit is contained in:
cheatfate 2020-09-30 16:19:51 +03:00
parent cb44a0db40
commit 4363df120f
No known key found for this signature in database
GPG Key ID: 46ADD633A7201F95

View File

@ -51,6 +51,8 @@ when defined(windows):
lpDefaultChar: pointer, lpDefaultChar: pointer,
lpUsedDefaultChar: pointer): cint {. lpUsedDefaultChar: pointer): cint {.
importc: "WideCharToMultiByte", stdcall, dynlib: "kernel32", sideEffect.} importc: "WideCharToMultiByte", stdcall, dynlib: "kernel32", sideEffect.}
proc getFileType(hFile: uint): uint32 {.
importc: "GetFileType", stdcall, dynlib: "kernel32", sideEffect.}
const const
CP_UTF8 = 65001'u32 CP_UTF8 = 65001'u32
@ -59,20 +61,17 @@ when defined(windows):
INVALID_HANDLE_VALUE = cast[uint](-1) INVALID_HANDLE_VALUE = cast[uint](-1)
ENABLE_PROCESSED_INPUT = 0x0001'u32 ENABLE_PROCESSED_INPUT = 0x0001'u32
ENABLE_ECHO_INPUT = 0x0004'u32 ENABLE_ECHO_INPUT = 0x0004'u32
ERROR_INVALID_HANDLE = 0x0006'u32 FILE_TYPE_CHAR = 0x0002'u32
proc isConsoleRedirected(hConsole: uint): bool = proc isConsoleRedirected(hConsole: uint): bool =
## Returns ``true`` if console handle was redirected. ## Returns ``true`` if console handle was redirected.
var mode: uint32 let res = getFileType(hConsole)
let res = getConsoleMode(hConsole, mode) if res == FILE_TYPE_CHAR:
if res == 0: # The specified handle is a character device, typically an LPT device or a
let errCode = ioLastError() # console.
if errCode == ERROR_INVALID_HANDLE:
true
else:
false
else:
false false
else:
true
proc readConsoleInput(maxBytes: int): IoResult[string] = proc readConsoleInput(maxBytes: int): IoResult[string] =
let hConsoleInput = let hConsoleInput =