File: init_cli_windows.go

package info (click to toggle)
gitlab-ci-multi-runner 14.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 31,248 kB
  • sloc: sh: 1,694; makefile: 384; asm: 79; ruby: 68
file content (27 lines) | stat: -rw-r--r-- 902 bytes parent folder | download
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
package cli_helpers

import (
	"github.com/sirupsen/logrus"
	"golang.org/x/sys/windows"
)

// InitCli initializes the Windows console window by activating virtual terminal features.
// Calling this function enables colored terminal output.
func InitCli() {
	setConsoleMode(windows.Stdout, windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING) // enable VT processing on standard output stream
	setConsoleMode(windows.Stderr, windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING) // enable VT processing on standard error stream
}

// setConsoleMode sets the given flags on the given
// console standard stream.
func setConsoleMode(handle windows.Handle, flags uint32) {
	var mode uint32

	// add console mode flag
	if err := windows.GetConsoleMode(handle, &mode); err == nil {
		err := windows.SetConsoleMode(handle, mode|flags)
		if err != nil {
			logrus.WithError(err).Info("Did not set console mode for cli")
		}
	}
}