Skip to content

Commit 7cac1a3

Browse files
authored
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
1 parent 112e866 commit 7cac1a3

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
- Enable terminal size reporting on macOS and Windows. Also report the
1212
terminal size even when the test is run buffered by Dune.
13-
(#381, @MisterDA)
13+
(#381, #396, @MisterDA)
1414

1515
- Allow overriding the number of columns with `ALCOTEST_COLUMNS` env
1616
var. (#322, #381, @MisterDA)

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)