13
enableQuickEditMode = 64
14
enableExtendedFlags = 128
15
enableProcessedOutput = 1
16
enableWrapAtEolOutput = 2
17
enableAutoPosition = 256 // Cursor position is not affected by writing data to the console.
18
enableEchoInput = 4 // Characters are written to the console as they're read.
19
enableProcessedInput = 1 // Enables input processing (like recognizing Ctrl+C).
22
var kernel32 = syscall.NewLazyDLL("kernel32.dll")
25
procGetConsoleMode = kernel32.NewProc("GetConsoleMode")
26
procSetConsoleMode = kernel32.NewProc("SetConsoleMode")
33
// IsTerminal checks if the given file descriptor is associated with a terminal
34
func IsTerminal(fd int) bool {
36
r, _, e := syscall.SyscallN(procGetConsoleMode.Addr(), uintptr(fd), uintptr(unsafe.Pointer(&st)), 0)
37
// if the call succeeds and doesn't produce an error, it's a terminal
38
return r != 0 && e == 0
41
func SetRawMode(fd int) (*State, error) {
43
// retrieve the current mode of the terminal
44
_, _, e := syscall.SyscallN(procGetConsoleMode.Addr(), uintptr(fd), uintptr(unsafe.Pointer(&st)), 0)
48
// modify the mode to set it to raw
49
raw := st &^ (enableEchoInput | enableProcessedInput | enableLineInput | enableProcessedOutput)
50
// apply the new mode to the terminal
51
_, _, e = syscall.SyscallN(procSetConsoleMode.Addr(), uintptr(fd), uintptr(raw), 0)
55
// return the original state so that it can be restored later
56
return &State{st}, nil
59
func UnsetRawMode(fd int, state any) error {
61
_, _, err := syscall.SyscallN(procSetConsoleMode.Addr(), uintptr(fd), uintptr(s.mode), 0)