test code: import terminal echo "press a key" let a = getch() echo a
result: E:Programsnim-1.6.10libpureterminal.nim(796) getch E:Programsnim-1.6.10libsystemassertions.nim(38) failedAssertImpl E:Programsnim-1.6.10libsystemassertions.nim(28) raiseAssert E:Programsnim-1.6.10libsystemfatal.nim(54) sysFatal Error: unhandled exception: E:Programsnim-1.6.10libpureterminal.nim(796, 15) readConsoleInput(fd, addr(keyEvent), 1, addr(numRead)) != 0 [AssertionDefect] Error: execution of an external program failed: 'E:Coding101NIMHangmanhangman1.exe '
Fairly fluent in js and php, I only started coding nim a couple of weeks ago and can't figure out where to start debugging this. I did try cut-and-paste getch from terminal, and got: undeclared identifier: 'getStdHandle'
readConsoleInput is wrapped from Windows: https://learn.microsoft.com/en-us/windows/console/readconsoleinput
If the function succeeds, the return value is nonzero. > If the function fails, the return value is zero. To get extended error information, call GetLastError.
So there was an error attempting to read the console, unfortunately instead of raising an OsError with the real error message the terminal module code just tries to assert that it ran correctly.
As for copy pasting the code, you probably need to import winlean as the original module does. Maybe to the end of the assert call you can add , $getLastError() to print the error code when it happens.
I'm not on window, so I can't test.
If you go to docs https://nim-lang.org/docs/terminal.html#getch And click in source, it opens https://github.com/nim-lang/Nim/blob/version-1-6/lib/pure/terminal.nim#L786
You can verify that doAssert to call readConsoleInput is failing because it expects return != 0
In index https://nim-lang.org/docs/theindex.html you may find readConsoleInput at https://nim-lang.org/docs/winlean.html#readConsoleInput%2CHandle%2Cpointer%2Ccint%2Cptr.cint
And clicking in source again it points to https://github.com/nim-lang/Nim/blob/version-1-6/lib/windows/winlean.nim#L1109
It uses a different system call depending on --define:useWinAnsi or --undef:useWinAnsi, maybe other compiler flag sets it to you https://nim-lang.org/docs/nimc.html . I'm not sure.
If those compile flags didn't help, you can keep digging.
Since readConsoleInput is a wrapper for importc: "ReadConsoleInputW" or importc: "ReadConsoleInputA", searching at internet, you can find https://learn.microsoft.com/en-us/windows/console/readconsoleinput that say 0 means error, and you can get error code calling GetLastError and find near readConsoleInput https://github.com/nim-lang/Nim/blob/version-1-6/lib/windows/winlean.nim#L224 a wrapper for GetLastError.
You could import std/winlean, add a try/except https://nim-lang.org/docs/tut2.html#exceptions-try-statement and call getLastError in except block, it gives you a magic number, there are two options then, search at Microsoft docs https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes#system-error-codes or use formatMessage https://github.com/nim-lang/Nim/blob/version-1-6/lib/windows/winlean.nim#L231
This code works well on my Windows10 PC.
# test.nim
import terminal
echo "press a key"
let a = getch()
echo a
This can compile and run on command line,
> nim c -r -d:release test.nim