# console.nim
when defined(windows):
import system/widestrs
proc setMode(fd: int, mode: int): int
{.importc: "_setmode", header: "io.h".}
proc fgetws(str: WideCString, numChars: int, stream: File): bool
{.importc, header: "stdio.h".}
discard setMode(getFileHandle(stdin), 0x00020000) # _O_U16TEXT
proc consoleReadLine*(line: var string): bool =
let buffer = newWideCString("", 256)
result = fgetws(buffer, 256, stdin)
let length = buffer.len
if length > 0 and buffer[length - 1].int16 == 10: # discard '\n'
buffer[length - 1] = Utf16Char(0)
# discard extra '\n' in waiting:
let buffer2 = newWideCString("", 2)
discard fgetws(buffer2, 2, stdin)
line = $buffer
proc consoleReadLine*(): string =
discard consoleReadLine(result)
else:
proc consoleReadLine*(line: var string): bool =
result = stdin.readLine(line)
proc consoleReadLine*(): string =
result = stdin.readLine()
Run
^ when using "nim c"
^ when using "nim c --gc:arc"
hmm.... it outputs (bytes: 1026, data: input) instead of string itself when
using --gc:arc