1
1
mirror of https://github.com/walles/moar.git synced 2024-11-26 04:21:11 +03:00
moar/twin/screen-setup-windows.go

81 lines
2.2 KiB
Go
Raw Normal View History

//go:build windows
// +build windows
package twin
import (
2023-08-14 20:47:42 +03:00
"fmt"
"os"
"syscall"
"golang.org/x/sys/windows"
"golang.org/x/term"
)
func (screen *UnixScreen) setupSigwinchNotification() {
screen.sigwinch = make(chan int, 1)
screen.sigwinch <- 0 // Trigger initial screen size query
// No SIGWINCH handling on Windows for now, contributions welcome, see
// sigwinch.go for inspiration.
}
func (screen *UnixScreen) setupTtyInTtyOut() error {
in, err := syscall.Open("CONIN$", syscall.O_RDWR, 0)
if err != nil {
2023-08-14 20:47:42 +03:00
return fmt.Errorf("failed to open CONIN$: %w", err)
}
screen.ttyIn = os.NewFile(uintptr(in), "/dev/tty")
// Set input stream to raw mode
2021-04-17 08:25:58 +03:00
stdin := windows.Handle(screen.ttyIn.Fd())
2021-04-17 08:44:24 +03:00
err = windows.GetConsoleMode(stdin, &screen.oldTtyInMode)
2021-04-17 08:25:58 +03:00
if err != nil {
2023-08-14 20:47:42 +03:00
return fmt.Errorf("failed to get stdin console mode: %w", err)
2021-04-17 08:25:58 +03:00
}
2021-04-17 08:44:24 +03:00
err = windows.SetConsoleMode(stdin, screen.oldTtyInMode|windows.ENABLE_VIRTUAL_TERMINAL_INPUT)
2021-04-17 08:25:58 +03:00
if err != nil {
2023-08-14 20:47:42 +03:00
return fmt.Errorf("failed to set stdin console mode: %w", err)
2021-04-17 08:25:58 +03:00
}
screen.oldTerminalState, err = term.MakeRaw(int(screen.ttyIn.Fd()))
2021-04-15 21:14:06 +03:00
if err != nil {
screen.restoreTtyInTtyOut() // Error intentionally ignored, report the first one only
2023-08-14 20:47:42 +03:00
return fmt.Errorf("failed to set raw mode: %w", err)
2021-04-15 21:14:06 +03:00
}
screen.ttyOut = os.Stdout
// Enable console colors, from: https://stackoverflow.com/a/52579002
stdout := windows.Handle(screen.ttyOut.Fd())
2021-04-17 08:44:24 +03:00
err = windows.GetConsoleMode(stdout, &screen.oldTtyOutMode)
2021-04-17 08:25:58 +03:00
if err != nil {
screen.restoreTtyInTtyOut() // Error intentionally ignored, report the first one only
2023-08-14 20:47:42 +03:00
return fmt.Errorf("failed to get stdout console mode: %w", err)
2021-04-17 08:25:58 +03:00
}
2021-04-17 08:44:24 +03:00
err = windows.SetConsoleMode(stdout, screen.oldTtyOutMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
if err != nil {
screen.restoreTtyInTtyOut() // Error intentionally ignored, report the first one only
2023-08-14 20:47:42 +03:00
return fmt.Errorf("failed to set stdout console mode: %w", err)
2021-04-17 08:44:24 +03:00
}
return nil
2021-04-17 08:44:24 +03:00
}
func (screen *UnixScreen) restoreTtyInTtyOut() error {
2021-04-17 08:44:24 +03:00
stdin := windows.Handle(screen.ttyIn.Fd())
err := windows.SetConsoleMode(stdin, screen.oldTtyInMode)
if err != nil {
return err
2021-04-17 08:44:24 +03:00
}
stdout := windows.Handle(screen.ttyOut.Fd())
err = windows.SetConsoleMode(stdout, screen.oldTtyOutMode)
2021-04-17 08:25:58 +03:00
if err != nil {
return err
2021-04-17 08:25:58 +03:00
}
return nil
}