main
Raw Download raw file
 1//go:build windows
 2// +build windows
 3
 4package tea
 5
 6import (
 7	"fmt"
 8	"os"
 9
10	"github.com/charmbracelet/x/term"
11	"golang.org/x/sys/windows"
12)
13
14func (p *Program) initInput() (err error) {
15	// Save stdin state and enable VT input
16	// We also need to enable VT
17	// input here.
18	if f, ok := p.input.(term.File); ok && term.IsTerminal(f.Fd()) {
19		p.ttyInput = f
20		p.previousTtyInputState, err = term.MakeRaw(p.ttyInput.Fd())
21		if err != nil {
22			return err
23		}
24
25		// Enable VT input
26		var mode uint32
27		if err := windows.GetConsoleMode(windows.Handle(p.ttyInput.Fd()), &mode); err != nil {
28			return fmt.Errorf("error getting console mode: %w", err)
29		}
30
31		if err := windows.SetConsoleMode(windows.Handle(p.ttyInput.Fd()), mode|windows.ENABLE_VIRTUAL_TERMINAL_INPUT); err != nil {
32			return fmt.Errorf("error setting console mode: %w", err)
33		}
34	}
35
36	// Save output screen buffer state and enable VT processing.
37	if f, ok := p.output.(term.File); ok && term.IsTerminal(f.Fd()) {
38		p.ttyOutput = f
39		p.previousOutputState, err = term.GetState(f.Fd())
40		if err != nil {
41			return err
42		}
43
44		var mode uint32
45		if err := windows.GetConsoleMode(windows.Handle(p.ttyOutput.Fd()), &mode); err != nil {
46			return fmt.Errorf("error getting console mode: %w", err)
47		}
48
49		if err := windows.SetConsoleMode(windows.Handle(p.ttyOutput.Fd()), mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING); err != nil {
50			return fmt.Errorf("error setting console mode: %w", err)
51		}
52	}
53
54	return
55}
56
57// Open the Windows equivalent of a TTY.
58func openInputTTY() (*os.File, error) {
59	f, err := os.OpenFile("CONIN$", os.O_RDWR, 0o644)
60	if err != nil {
61		return nil, err
62	}
63	return f, nil
64}
65
66const suspendSupported = false
67
68func suspendProcess() {}