-
-
Notifications
You must be signed in to change notification settings - Fork 516
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
cmd/age: fix terminal escape sequences on Windows #475
Open
BuriedInTheGround
wants to merge
1
commit into
FiloSottile:main
Choose a base branch
from
BuriedInTheGround:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2022 The age Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"syscall" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
// Some instances of the Windows Console (e.g., cmd.exe and Windows PowerShell) | ||
// do not have the virtual terminal processing enabled, which is necessary to | ||
// make terminal escape sequences work. For this reason the clearLine function | ||
// may not properly work. Here we enable the virtual terminal processing, if | ||
// possible. | ||
// | ||
// See https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences. | ||
func init() { | ||
const ( | ||
ENABLE_PROCESSED_OUTPUT uint32 = 0x1 | ||
ENABLE_VIRTUAL_TERMINAL_PROCESSING uint32 = 0x4 | ||
) | ||
|
||
kernel32DLL := windows.NewLazySystemDLL("Kernel32.dll") | ||
setConsoleMode := kernel32DLL.NewProc("SetConsoleMode") | ||
|
||
var mode uint32 | ||
err := syscall.GetConsoleMode(syscall.Stdout, &mode) | ||
if err != nil { | ||
// Terminal escape sequences may work at this point, but we can't know. | ||
avoidTerminalEscapeSequences = true | ||
return | ||
} | ||
|
||
mode |= ENABLE_PROCESSED_OUTPUT | ||
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | ||
ret, _, _ := setConsoleMode.Call(uintptr(syscall.Stdout), uintptr(mode)) | ||
// If the SetConsoleMode function fails, the return value is zero. | ||
// See https://learn.microsoft.com/en-us/windows/console/setconsolemode#return-value. | ||
if ret == 0 { | ||
avoidTerminalEscapeSequences = true | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this and sorry about the review lag!
I made a couple small style changes, but then realized this shouldn't be unconditionally be stdout, as we might have opened
CONOUT$
as a terminal instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking the time to review!
Shouldn't this work anyway by using
syscall.Stdout
? Go initializessyscall.Stdout
withGetStdHandle(STD_OUTPUT_HANDLE)
(see here), andSTD_OUTPUT_HANDLE
defaults toCONOUT$
, as described in the Windows Console docs.Maybe I'm confusing or missing something. If so, could you please elaborate on the situation?