Skip to content

Commit

Permalink
TTY resize for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
FlUxIuS committed Sep 19, 2024
1 parent 3d2179a commit a966a27
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
46 changes: 34 additions & 12 deletions go/rfswift/dock/rfdock.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"time"
"os/signal"
"syscall"
"runtime"

"context"
"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -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
}
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions go/rfswift/dock/terminal_darwin.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions go/rfswift/dock/terminal_linux.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions go/rfswift/dock/terminal_windows.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dock

import (
"os"
"golang.org/x/sys/windows"
)

Expand All @@ -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
}

0 comments on commit a966a27

Please sign in to comment.