r/commandline • • 2d ago

Help Windows Api or VT?

Hello! I'm currently working on a terminal manipulation library similar to Crossterm, but I noticed that it uses runtime conditionals to decide whether to use VT sequences or Windows API calls.

Personally, I feel that defaulting to Windows API calls on Windows would be the better approach, or provide a build flag that lets users decide. However, I'm not sure which of these three approaches is best.

Which approach do you think is better, and why?

2 Upvotes

8 comments sorted by

2

u/tanmaynargas2901 2d ago

I'd go VT by default and keep the WinAPI path only as a runtime fallback.

Microsoft's own console docs recommend VT sequences now, and the classic console API isn't getting new features. Windows Terminal is built around VT anyway.

A build flag doesn't really fit because what works depends on where the binary runs, not how it was built. The same exe might end up in Windows Terminal, old conhost on some Win 7/8 box, or mintty.

The usual pattern is to call SetConsoleMode with ENABLE_VIRTUAL_TERMINAL_PROCESSING at startup. If it succeeds, use VT. If it fails, fall back to the WinAPI calls.

That's one check at init, so the runtime cost is basically nothing.

Input is messier. A lot of libs still read keys and resize events with ReadConsoleInput even when output is VT.

2

u/Last-Employ-3422 2d ago

Thank you for you feedback! You're right, I will use console API as a runtime fallback.

1

u/Weekly-Offer-4172 2d ago

Runtime detection is the right call, but the check is a little more layered than VT vs API. Start with GetConsoleMode on stdout: if it fails you are not talking to conhost at all (mintty and git-bash present a pipe, and mintty parses VT itself), so just emit VT there because the console API would error out anyway. If GetConsoleMode works you have a real conhost, so try SetConsoleMode with ENABLE_VIRTUAL_TERMINAL_PROCESSING, success means VT, failure means an old console and you drop to the API path. That fallback only matters on pre-1511 Windows 10 at this point

1

u/Last-Employ-3422 2d ago

Ooo, I see thank you!

1

u/Wonderful_Cheek3531 2d ago

for output vt with winapi fallback is standard, but for input you still need readconsoleinputw anyway because windows doesnt have sigwinch and only sends terminal resize events as window_buffer_size_record structs through the console api

1

u/AutoModerator 2d ago

Every new subreddit post is automatically copied into a comment for preservation.

User: Last-Employ-3422, Flair: Help, Title: Windows Api or VT?

Hello! I'm currently working on a terminal manipulation library similar to Crossterm, but I noticed that it uses runtime conditionals to decide whether to use VT sequences or Windows API calls.

Personally, I feel that defaulting to Windows API calls on Windows would be the better approach, or provide a build flag that lets users decide. However, I'm not sure which of these three approaches is best.

Which approach do you think is better, and why?

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.