r/commandline • • 1h 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

3 comments sorted by

1

u/AutoModerator 1h 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.

1

u/tanmaynargas2901 1h 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.

1

u/Last-Employ-3422 1h ago

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