From 7cac1a3abf7770eb0414d4c3120dbae2be50ae2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonin=20D=C3=A9cimo?= Date: Fri, 6 Oct 2023 10:02:57 +0200 Subject: [PATCH] Open Windows console handle to retrieve dimensions (#396) If stdout is redirected on Windows, the dimensions of the terminal won't be correctly retrieved. 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 --- CHANGES.md | 2 +- src/alcotest/alcotest_stubs.c | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 1b9f3931..ec914563 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,7 +10,7 @@ - Enable terminal size reporting on macOS and Windows. Also report the terminal size even when the test is run buffered by Dune. - (#381, @MisterDA) + (#381, #396, @MisterDA) - Allow overriding the number of columns with `ALCOTEST_COLUMNS` env var. (#322, #381, @MisterDA) diff --git a/src/alcotest/alcotest_stubs.c b/src/alcotest/alcotest_stubs.c index 3df01a68..ab53369d 100644 --- a/src/alcotest/alcotest_stubs.c +++ b/src/alcotest/alcotest_stubs.c @@ -35,8 +35,16 @@ CAMLprim value ocaml_alcotest_get_terminal_dimensions(value unit) CAMLparam1(unit); CAMLlocal2(result, pair); + HANDLE console = CreateFileW(L"CONOUT$", GENERIC_READ|GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + HANDLE handle = console == INVALID_HANDLE_VALUE ? + GetStdHandle(STD_OUTPUT_HANDLE) : console; CONSOLE_SCREEN_BUFFER_INFO csbi; - int success = GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); + int success = GetConsoleScreenBufferInfo(handle, &csbi); + if (console != INVALID_HANDLE_VALUE) + CloseHandle(console); + if (success) { pair = caml_alloc_tuple(2);