"vscode:/vscode.git/clone" did not exist on "b973563be9e3ad0d452def9df49a6860a0a528a4"
term_windows.go 1.06 KB
Newer Older
mashun1's avatar
v1  
mashun1 committed
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
package readline

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

type State struct {
	mode uint32
}

// IsTerminal checks if the given file descriptor is associated with a terminal
func IsTerminal(fd int) bool {
	var st uint32
	err := windows.GetConsoleMode(windows.Handle(fd), &st)
	return err == nil
}

func SetRawMode(fd int) (*State, error) {
	var st uint32
	if err := windows.GetConsoleMode(windows.Handle(fd), &st); err != nil {
		return nil, err
	}

	// this enables raw mode by turning off various flags in the console mode: https://pkg.go.dev/golang.org/x/sys/windows#pkg-constants
	raw := st &^ (windows.ENABLE_ECHO_INPUT | windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_LINE_INPUT | windows.ENABLE_PROCESSED_OUTPUT)

	// turn on ENABLE_VIRTUAL_TERMINAL_INPUT to enable escape sequences
	raw |= windows.ENABLE_VIRTUAL_TERMINAL_INPUT
	if err := windows.SetConsoleMode(windows.Handle(fd), raw); err != nil {
		return nil, err
	}
	return &State{st}, nil
}

func UnsetRawMode(fd int, state any) error {
	s := state.(*State)
	return windows.SetConsoleMode(windows.Handle(fd), s.mode)
}