Skip to content

Update Windows ANSI code code based on feedback #543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions cmd/tsgo/enablevtprocessing_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ import (
)

func enableVirtualTerminalProcessing() {
hStdout, err := windows.GetStdHandle(windows.STD_OUTPUT_HANDLE)
if err == nil && hStdout != windows.InvalidHandle {
h, err := windows.GetStdHandle(windows.STD_OUTPUT_HANDLE)
if err != nil || h == windows.InvalidHandle {
return
}
fileType, err := windows.GetFileType(h)
if err != nil || fileType == windows.FILE_TYPE_CHAR {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth noting that you need to do this to only enable virtual terminal processing if stdout hasn't been redirected. Otherwise, leave it as-is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or it's possible you don't even "need" to do it? It's unclear to me what the purpose is since libuv doesn't do this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth noting that you need to do this to only enable virtual terminal processing if stdout hasn't been redirected. Otherwise, leave it as-is.

The STD_OUTPUT_HANDLE is not redirected if it is a character device and GetConsoleMode succeeds. See for example dotnet's IsHandleRedirected function.

Or it's possible you don't even "need" to do it? It's unclear to me what the purpose is since libuv doesn't do this.

It is optional. I suggested this change to @jakebailey because GetConsoleMode can be slow, and GetFileType (which is much faster), helps doing the GetConsoleMode only when it is really necessary. Having said this, as enableVirtualTerminalProcessing is a one-time call, it won't make any difference.

var mode uint32
err = windows.GetConsoleMode(windows.Handle(hStdout), &mode)
if err == nil {
windows.SetConsoleMode(windows.Handle(hStdout), mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
if err := windows.GetConsoleMode(h, &mode); err != nil {
return
}
if mode&windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0 {
windows.SetConsoleMode(h, mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
}
}
}