Skip to content

Commit 43cd698

Browse files
author
3code
committed
fix Windows getchr hang: drain termPeeked before _getch
On Windows getchr was a direct _getch import, so it never drained termPeeked (unlike the POSIX branch). When the "getchr drains termPeeked" test set termPeeked='Z' and called getchr(), _getch ignored the peeked byte and blocked waiting for a real keypress, hanging the Windows test run for the full timeout. Wrap _getch in a getchr that returns the peeked byte first, matching POSIX.
1 parent 891d0de commit 43cd698

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

‎src/threecode/minline.nim‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,18 @@ var pollStdinNowHook*: proc(): bool {.closure.}
7171
when defined(windows):
7272
proc putchr*(c: cint): cint {.discardable, header: "<conio.h>", importc: "_putch".}
7373
## Prints an ASCII character to stdout.
74-
proc getchr*(): cint {.header: "<conio.h>", importc: "_getch".}
75-
## Retrieves an ASCII character from stdin.
74+
proc rawGetch(): cint {.header: "<conio.h>", importc: "_getch".}
75+
## Raw blocking key read; wrapped by `getchr` below.
76+
77+
proc getchr*(): cint =
78+
## Retrieves an ASCII character from stdin. Drains `termPeeked` first
79+
## (a byte stashed by `terminalHasPendingInput` that was not an escape
80+
## tail) so it is not lost; mirrors the POSIX branch's contract.
81+
if termPeeked >= 0:
82+
result = termPeeked.cint
83+
termPeeked = -1
84+
return result
85+
rawGetch()
7686
else:
7787
proc putchr*(c: cint) {.header: "stdio.h", importc: "putchar"} =
7888
## Prints an ASCII character to stdout.

0 commit comments

Comments
 (0)