forked from containers/podman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Windows terminal handling is different than darwin and linux. It needs to have the terminal mode set to enable virtual terminal processing. This allows colors and other things to work. Signed-off-by: Brent Baude <bbaude@redhat.com> <MH: Tweaked imports to compile> Signed-off-by: Matt Heon <matthew.heon@pm.me>
- Loading branch information
Showing
3 changed files
with
51 additions
and
0 deletions.
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,8 @@ | ||
// +build !windows | ||
|
||
package terminal | ||
|
||
// SetConsole for non-windows environments is a no-op | ||
func SetConsole() error { | ||
return nil | ||
} |
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,37 @@ | ||
// +build windows | ||
|
||
package terminal | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
"golang.org/x/sys/windows" | ||
) | ||
|
||
// SetConsole switches the windows terminal mode to be able to handle colors, etc | ||
func SetConsole() error { | ||
if err := setConsoleMode(windows.Stdout, windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING); err != nil { | ||
return err | ||
} | ||
if err := setConsoleMode(windows.Stderr, windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING); err != nil { | ||
return err | ||
} | ||
if err := setConsoleMode(windows.Stdin, windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func setConsoleMode(handle windows.Handle, flags uint32) error { | ||
var mode uint32 | ||
err := windows.GetConsoleMode(handle, &mode) | ||
if err != nil { | ||
return err | ||
} | ||
if err := windows.SetConsoleMode(handle, mode|flags); err != nil { | ||
// In similar code, it is not considered an error if we cannot set the | ||
// console mode. Following same line of thinking here. | ||
logrus.WithError(err).Error("Failed to set console mode for cli") | ||
} | ||
|
||
return nil | ||
} |