Skip to content

Commit

Permalink
Open Windows console handle to retrieve dimensions (#396)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MisterDA authored Oct 6, 2023
1 parent 112e866 commit 7cac1a3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion src/alcotest/alcotest_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7cac1a3

Please sign in to comment.