diff --git a/term_unix.go b/term_unix.go index 579ce55..44c6205 100644 --- a/term_unix.go +++ b/term_unix.go @@ -1,5 +1,5 @@ -//go:build !windows -// +build !windows +//go:build !js && !windows +// +build !js,!windows package term diff --git a/term_webassembly.go b/term_webassembly.go new file mode 100644 index 0000000..50437a7 --- /dev/null +++ b/term_webassembly.go @@ -0,0 +1,76 @@ +//go:build js +// +build js + +package term + +import ( + "errors" + "io" + "os" + "syscall/js" +) + +// terminalState holds the platform-specific state / console mode for the terminal. +type terminalState struct{} + +// GetWinsize returns the window size based on the specified file descriptor. +func getWinsize(fd uintptr) (*Winsize, error) { + window := js.Global().Get("window") + width := uint16(window.Get("innerWidth").Int()) + height := uint16(window.Get("innerHeight").Int()) + return &Winsize{Width: width, Height: height}, nil +} + +func isTerminal(fd uintptr) bool { + return true +} + +func saveState(fd uintptr) (*State, error) { + return &State{}, nil +} + +func stdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) { + return os.Stdin, os.Stdout, os.Stderr +} + +func disableEcho(fd uintptr, state *State) error { + return nil +} + +func setRawTerminal(fd uintptr) (*State, error) { + return makeRaw(fd) +} + +func getFdInfo(in interface{}) (uintptr, bool) { + var inFd uintptr + var isTerminalIn bool + if file, ok := in.(*os.File); ok { + inFd = file.Fd() + isTerminalIn = isTerminal(inFd) + } + return inFd, isTerminalIn +} + +func setWinsize(fd uintptr, ws *Winsize) error { + window := js.Global().Get("window") + window.Set("innerWidth", ws.Width) + window.Set("innerHeight", ws.Height) + + return nil +} + +func makeRaw(fd uintptr) (*State, error) { + oldState := State{} + return &oldState, nil +} + +func setRawTerminalOutput(fd uintptr) (*State, error) { + return nil, nil +} + +func restoreTerminal(fd uintptr, state *State) error { + if state == nil { + return errors.New("invalid terminal state") + } + return nil +} diff --git a/termios_nonbsd.go b/termios_nonbsd.go index 88b7b21..6257d04 100644 --- a/termios_nonbsd.go +++ b/termios_nonbsd.go @@ -1,5 +1,5 @@ -//go:build !darwin && !freebsd && !netbsd && !openbsd && !windows -// +build !darwin,!freebsd,!netbsd,!openbsd,!windows +//go:build !darwin && !freebsd && !netbsd && !openbsd && !js && !windows +// +build !darwin,!freebsd,!netbsd,!openbsd,!js,!windows package term diff --git a/termios_unix.go b/termios_unix.go index 60c8237..4df05b4 100644 --- a/termios_unix.go +++ b/termios_unix.go @@ -1,5 +1,5 @@ -//go:build !windows -// +build !windows +//go:build !js && !windows +// +build !js,!windows package term