From 5777d777476ec43815ec0effac47efa6791008e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Thu, 21 Aug 2025 12:31:55 +0200 Subject: [PATCH 1/5] feat: add support for webassembly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Léone --- term_unix.go | 4 +-- term_webassembly.go | 66 +++++++++++++++++++++++++++++++++++++++++++++ termios_nonbsd.go | 4 +-- termios_unix.go | 4 +-- 4 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 term_webassembly.go 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..d024549 --- /dev/null +++ b/term_webassembly.go @@ -0,0 +1,66 @@ +//go:build js +// +build js + +package term + +import "io" +import "os" +import "errors" + +// 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) { + return getWinsize(fd) +} + +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 { + 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 From d65280ce917689457b65467c0ff0faaad30aa429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Thu, 21 Aug 2025 14:19:34 +0200 Subject: [PATCH 2/5] return nil, nil for getWinsize MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Léone --- term_webassembly.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/term_webassembly.go b/term_webassembly.go index d024549..8ba5de8 100644 --- a/term_webassembly.go +++ b/term_webassembly.go @@ -12,7 +12,7 @@ type terminalState struct{} // GetWinsize returns the window size based on the specified file descriptor. func getWinsize(fd uintptr) (*Winsize, error) { - return getWinsize(fd) + return nil, nil } func isTerminal(fd uintptr) bool { From c92dc76669b086379e4dd57f92035510b4491d78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Thu, 21 Aug 2025 14:27:00 +0200 Subject: [PATCH 3/5] implement a getWinSize and setWinSize MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Léone --- term_webassembly.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/term_webassembly.go b/term_webassembly.go index 8ba5de8..4cdb23d 100644 --- a/term_webassembly.go +++ b/term_webassembly.go @@ -12,7 +12,10 @@ type terminalState struct{} // GetWinsize returns the window size based on the specified file descriptor. func getWinsize(fd uintptr) (*Winsize, error) { - return nil, nil + window := js.Global().Get("window") + width := window.Get("innerWidth").Int() + height := window.Get("innerHeight").Int() + return &Winsize{width: width, Height: height}, nil } func isTerminal(fd uintptr) bool { @@ -46,6 +49,10 @@ func getFdInfo(in interface{}) (uintptr, bool) { } func setWinsize(fd uintptr, ws *Winsize) error { + window := js.Global().Get("window") + window.Set("innerWidth", ws.Width) + window.Set("innerHeight", ws.Height) + return nil } From d41322b230bcb1a66f9e23dd840efefb550eaec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Thu, 21 Aug 2025 14:29:49 +0200 Subject: [PATCH 4/5] Fix imports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Léone --- term_webassembly.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/term_webassembly.go b/term_webassembly.go index 4cdb23d..0d6febc 100644 --- a/term_webassembly.go +++ b/term_webassembly.go @@ -3,9 +3,12 @@ package term -import "io" -import "os" -import "errors" +import ( + "errors" + "io" + "os" + "syscall/js" +) // terminalState holds the platform-specific state / console mode for the terminal. type terminalState struct{} From d67ea226892d9617c452fabf4c6b894b91aa64fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Thu, 21 Aug 2025 16:03:32 +0200 Subject: [PATCH 5/5] cast to uint16 --- term_webassembly.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/term_webassembly.go b/term_webassembly.go index 0d6febc..50437a7 100644 --- a/term_webassembly.go +++ b/term_webassembly.go @@ -16,9 +16,9 @@ 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 := window.Get("innerWidth").Int() - height := window.Get("innerHeight").Int() - return &Winsize{width: width, Height: height}, nil + width := uint16(window.Get("innerWidth").Int()) + height := uint16(window.Get("innerHeight").Int()) + return &Winsize{Width: width, Height: height}, nil } func isTerminal(fd uintptr) bool {