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)
}
}
}
|