Skip to content

Commit 52e74ee

Browse files
committed
Open Windows console handle to retreive dimensions
If stdout is redirected on Windows, the dimensions of the terminal won't be correctly retreived. Open the console instead, using the special filename CONOUT$, and ask the dimensions of this handle. Keep a fallback to stdout if the console can't be opened. https://learn.microsoft.com/en-us/windows/console/console-handles
1 parent d30c362 commit 52e74ee

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

Diff for: src/alcotest/alcotest_stubs.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,16 @@ CAMLprim value ocaml_alcotest_get_terminal_dimensions(value unit)
3535
CAMLparam1(unit);
3636
CAMLlocal2(result, pair);
3737

38+
HANDLE console = CreateFileW(L"CONOUT$", GENERIC_READ|GENERIC_WRITE,
39+
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
40+
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
41+
HANDLE handle = console == INVALID_HANDLE_VALUE ?
42+
GetStdHandle(STD_OUTPUT_HANDLE) : console;
3843
CONSOLE_SCREEN_BUFFER_INFO csbi;
39-
int success = GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
44+
int success = GetConsoleScreenBufferInfo(handle, &csbi);
45+
if (console != INVALID_HANDLE_VALUE)
46+
CloseHandle(console);
47+
4048
if (success)
4149
{
4250
pair = caml_alloc_tuple(2);

0 commit comments

Comments
 (0)