File: ui_windows.go

package info (click to toggle)
golang-step-cli-utils 0.7.5%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 404 kB
  • sloc: makefile: 19
file content (51 lines) | stat: -rw-r--r-- 1,277 bytes parent folder | download | duplicates (2)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//go:build windows
// +build windows

package ui

import (
	"fmt"
	"os"

	"golang.org/x/sys/windows"
)

var inMode, outMode uint32

func init() {
	var _ = windows.GetConsoleMode(windows.Stdin, &inMode)
	var _ = windows.GetConsoleMode(windows.Stdout, &outMode)
}

func setConsoleMode() {
	in := inMode | windows.ENABLE_VIRTUAL_TERMINAL_INPUT
	out := outMode | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING

	if inMode != 0 && inMode != in {
		if err := windows.SetConsoleMode(windows.Stdin, in); err != nil {
			fmt.Fprintf(os.Stderr, "Failed to set console mode: %v\n", err)
		}
	}

	if outMode != 0 && outMode != out {
		if err := windows.SetConsoleMode(windows.Stdout, out); err != nil {
			fmt.Fprintf(os.Stderr, "Failed to set console mode: %v\n", err)
		}
	}
}

func resetConsoleMode() {
	in := inMode | windows.ENABLE_VIRTUAL_TERMINAL_INPUT
	out := outMode | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING

	if inMode != 0 && inMode != in {
		if err := windows.SetConsoleMode(windows.Stdin, inMode); err != nil {
			fmt.Fprintf(os.Stderr, "Failed to reset console mode: %v\n", err)
		}
	}
	if outMode != 0 && outMode != out {
		if err := windows.SetConsoleMode(windows.Stdout, outMode); err != nil {
			fmt.Fprintf(os.Stderr, "Failed to reset console mode: %v\n", err)
		}
	}
}