aboutsummaryrefslogtreecommitdiff
path: root/pkg/terminal/console_windows.go
blob: 08e66cb3a0aaaa2330b284f69911fd80235c2237 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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).Debug("Failed to set console mode for cli")
	}

	return nil
}