From a966a2757cb9bd69cd6b202744aba733c66f3e0a Mon Sep 17 00:00:00 2001 From: FlUxIuS Date: Thu, 19 Sep 2024 11:08:13 +0200 Subject: [PATCH] TTY resize for Windows --- go/rfswift/dock/rfdock.go | 46 +++++++++++++++++++++-------- go/rfswift/dock/terminal_darwin.go | 6 ++++ go/rfswift/dock/terminal_linux.go | 6 ++++ go/rfswift/dock/terminal_windows.go | 5 ++++ 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/go/rfswift/dock/rfdock.go b/go/rfswift/dock/rfdock.go index b3f8f3c..5341e84 100644 --- a/go/rfswift/dock/rfdock.go +++ b/go/rfswift/dock/rfdock.go @@ -11,7 +11,7 @@ import ( "strings" "time" "os/signal" - "syscall" + "runtime" "context" "github.com/docker/docker/api/types" @@ -630,17 +630,39 @@ func DockerExec(containerIdentifier string, WorkingDir string) { // Handle terminal resize go func() { - sigchan := make(chan os.Signal, 1) - signal.Notify(sigchan, syscall.SIGWINCH) - defer signal.Stop(sigchan) - - for range sigchan { - if outIsTerminal { - if size, err := term.GetWinsize(outFd); err == nil { - cli.ContainerExecResize(ctx, execID.ID, container.ResizeOptions{ - Height: uint(size.Height), - Width: uint(size.Width), - }) + switch runtime.GOOS { + case "linux", "darwin": + sigchan := make(chan os.Signal, 1) + signal.Notify(sigchan, syscallsigwin()) + defer signal.Stop(sigchan) + + for range sigchan { + if outIsTerminal { + if size, err := term.GetWinsize(outFd); err == nil { + cli.ContainerExecResize(ctx, execID.ID, container.ResizeOptions{ + Height: uint(size.Height), + Width: uint(size.Width), + }) + } + } + } + case "windows": + ticker := time.NewTicker(500 * time.Millisecond) + defer ticker.Stop() + + var lastHeight, lastWidth uint16 + for range ticker.C { + if outIsTerminal { + if size, err := term.GetWinsize(outFd); err == nil { + if size.Height != lastHeight || size.Width != lastWidth { + cli.ContainerExecResize(ctx, execID.ID, container.ResizeOptions{ + Height: uint(size.Height), + Width: uint(size.Width), + }) + lastHeight = size.Height + lastWidth = size.Width + } + } } } } diff --git a/go/rfswift/dock/terminal_darwin.go b/go/rfswift/dock/terminal_darwin.go index 4c7344d..6139c25 100644 --- a/go/rfswift/dock/terminal_darwin.go +++ b/go/rfswift/dock/terminal_darwin.go @@ -1,9 +1,15 @@ package dock import ( + "syscall" + "os" "golang.org/x/crypto/ssh/terminal" ) func getTerminalSize(fd int) (int, int, error) { return terminal.GetSize(fd) } + +func syscallsigwin() (os.Signal) { + return syscall.SIGWINCH +} \ No newline at end of file diff --git a/go/rfswift/dock/terminal_linux.go b/go/rfswift/dock/terminal_linux.go index 4c7344d..6139c25 100644 --- a/go/rfswift/dock/terminal_linux.go +++ b/go/rfswift/dock/terminal_linux.go @@ -1,9 +1,15 @@ package dock import ( + "syscall" + "os" "golang.org/x/crypto/ssh/terminal" ) func getTerminalSize(fd int) (int, int, error) { return terminal.GetSize(fd) } + +func syscallsigwin() (os.Signal) { + return syscall.SIGWINCH +} \ No newline at end of file diff --git a/go/rfswift/dock/terminal_windows.go b/go/rfswift/dock/terminal_windows.go index fc8dd89..f389a11 100644 --- a/go/rfswift/dock/terminal_windows.go +++ b/go/rfswift/dock/terminal_windows.go @@ -1,6 +1,7 @@ package dock import ( + "os" "golang.org/x/sys/windows" ) @@ -20,3 +21,7 @@ func getTerminalSize(fd int) (int, int, error) { height := int(info.Window.Bottom - info.Window.Top + 1) return width, height, nil } + +func syscallsigwin() os.Signal { + return nil // No signal equivalent for Windows +} \ No newline at end of file