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 (
|
2020-12-30 22:08:38 -05:00
|
|
|
"io"
|
2020-10-12 22:36:24 -04:00
|
|
|
"os"
|
|
|
|
|
2021-02-26 18:38:52 -05:00
|
|
|
"github.com/containerd/console"
|
2020-10-12 22:36:24 -04:00
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
)
|
|
|
|
|
2021-02-26 18:38:52 -05:00
|
|
|
func (p *Program) initInput() error {
|
2021-07-29 17:59:36 -04:00
|
|
|
// If input's a file, use console to manage it
|
|
|
|
if f, ok := p.input.(*os.File); ok {
|
2021-02-26 18:38:52 -05:00
|
|
|
// Save a reference to the current stdin then replace stdin with our
|
|
|
|
// input. We do this so we can hand input off to containerd/console to
|
|
|
|
// set raw mode, and do it in this fashion because the method
|
|
|
|
// console.ConsoleFromFile isn't supported on Windows.
|
|
|
|
p.windowsStdin = os.Stdin
|
|
|
|
os.Stdin = f
|
|
|
|
|
2021-07-29 17:59:36 -04:00
|
|
|
// Note: this will panic if it fails.
|
|
|
|
c := console.Current()
|
|
|
|
p.console = c
|
|
|
|
}
|
2021-02-26 18:38:52 -05:00
|
|
|
|
2021-07-29 17:36:29 -04:00
|
|
|
enableAnsiColors(p.output)
|
|
|
|
|
2021-02-26 18:38:52 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// restoreInput restores stdout in the event that we placed it aside to handle
|
|
|
|
// input with CONIN$, above.
|
|
|
|
func (p *Program) restoreInput() error {
|
|
|
|
if p.windowsStdin != nil {
|
|
|
|
os.Stdin = p.windowsStdin
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-12 22:36:24 -04:00
|
|
|
// enableAnsiColors enables support for ANSI color sequences in Windows
|
|
|
|
// default console. Note that this only works with Windows 10.
|
2020-12-30 22:08:38 -05:00
|
|
|
func enableAnsiColors(w io.Writer) {
|
|
|
|
f, ok := w.(*os.File)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout := windows.Handle(f.Fd())
|
2020-10-12 22:36:24 -04:00
|
|
|
var originalMode uint32
|
|
|
|
|
2021-02-26 18:38:52 -05:00
|
|
|
_ = windows.GetConsoleMode(stdout, &originalMode)
|
|
|
|
_ = windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
|
2020-10-12 22:36:24 -04:00
|
|
|
}
|