From 885d5f20983b150f5501385122464e5514f8bbd2 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Sun, 24 Apr 2022 22:23:54 -0500 Subject: [PATCH] fix: Monitor TTY size when using SSH (#1119) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The TTY wasn't resizing properly, and reasonably so considering we weren't updating it 🤦. --- cli/ssh.go | 13 +++++++++++++ cli/ssh_other.go | 22 ++++++++++++++++++++++ cli/ssh_windows.go | 27 +++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 cli/ssh_other.go create mode 100644 cli/ssh_windows.go diff --git a/cli/ssh.go b/cli/ssh.go index 627a0a041e..6bc2da7231 100644 --- a/cli/ssh.go +++ b/cli/ssh.go @@ -134,6 +134,19 @@ func ssh() *cobra.Command { defer func() { _ = term.Restore(int(os.Stdin.Fd()), state) }() + + windowChange := listenWindowSize(cmd.Context()) + go func() { + for { + select { + case <-cmd.Context().Done(): + return + case <-windowChange: + } + width, height, _ := term.GetSize(int(stdoutFile.Fd())) + _ = sshSession.WindowChange(height, width) + } + }() } err = sshSession.RequestPty("xterm-256color", 128, 128, gossh.TerminalModes{}) diff --git a/cli/ssh_other.go b/cli/ssh_other.go new file mode 100644 index 0000000000..8799030949 --- /dev/null +++ b/cli/ssh_other.go @@ -0,0 +1,22 @@ +//go:build !windows +// +build !windows + +package cli + +import ( + "context" + "os" + "os/signal" + + "golang.org/x/sys/unix" +) + +func listenWindowSize(ctx context.Context) <-chan os.Signal { + windowSize := make(chan os.Signal, 1) + signal.Notify(windowSize, unix.SIGWINCH) + go func() { + <-ctx.Done() + signal.Stop(windowSize) + }() + return windowSize +} diff --git a/cli/ssh_windows.go b/cli/ssh_windows.go new file mode 100644 index 0000000000..2b1cbb4dd6 --- /dev/null +++ b/cli/ssh_windows.go @@ -0,0 +1,27 @@ +//go:build windows +// +build windows + +package cli + +import ( + "context" + "os" + "time" +) + +func listenWindowSize(ctx context.Context) <-chan os.Signal { + windowSize := make(chan os.Signal, 3) + ticker := time.NewTicker(time.Second) + go func() { + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + } + windowSize <- nil + } + }() + return windowSize +}