2021-10-02 14:18:18 -04:00
|
|
|
//go:build windows
|
2020-01-25 01:15:29 -05:00
|
|
|
// +build windows
|
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
package tea
|
2020-01-25 01:15:29 -05:00
|
|
|
|
2020-10-12 22:36:24 -04:00
|
|
|
import (
|
2024-01-09 17:20:23 -05:00
|
|
|
"fmt"
|
2020-10-12 22:36:24 -04:00
|
|
|
"os"
|
|
|
|
|
2024-01-09 17:20:23 -05:00
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
"golang.org/x/term"
|
2020-10-12 22:36:24 -04:00
|
|
|
)
|
|
|
|
|
2024-01-09 17:20:23 -05:00
|
|
|
func (p *Program) initInput() (err error) {
|
|
|
|
// Save stdin state and enable VT input
|
|
|
|
// We enable VT processing using Termenv, but we also need to enable VT
|
|
|
|
// input here.
|
|
|
|
if f, ok := p.input.(*os.File); ok && term.IsTerminal(int(f.Fd())) {
|
|
|
|
p.tty = f
|
|
|
|
p.ttyState, err = term.MakeRaw(int(p.tty.Fd()))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable VT input
|
|
|
|
var mode uint32
|
|
|
|
if err := windows.GetConsoleMode(windows.Handle(p.tty.Fd()), &mode); err != nil {
|
|
|
|
return fmt.Errorf("error getting console mode: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := windows.SetConsoleMode(windows.Handle(p.tty.Fd()), mode|windows.ENABLE_VIRTUAL_TERMINAL_INPUT); err != nil {
|
|
|
|
return fmt.Errorf("error setting console mode: %w", err)
|
|
|
|
}
|
2021-07-29 17:59:36 -04:00
|
|
|
}
|
2021-02-26 18:38:52 -05:00
|
|
|
|
2024-01-09 17:20:23 -05:00
|
|
|
return
|
2021-02-26 18:38:52 -05:00
|
|
|
}
|
|
|
|
|
2021-07-29 16:47:13 -04:00
|
|
|
// Open the Windows equivalent of a TTY.
|
2021-02-26 18:38:52 -05:00
|
|
|
func openInputTTY() (*os.File, error) {
|
|
|
|
f, err := os.OpenFile("CONIN$", os.O_RDWR, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|