Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 885d5f2

Browse files
authored
fix: Monitor TTY size when using SSH (#1119)
The TTY wasn't resizing properly, and reasonably so considering we weren't updating it 🤦.
1 parent 0c042dc commit 885d5f2

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

cli/ssh.go

+13
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,19 @@ func ssh() *cobra.Command {
134134
defer func() {
135135
_ = term.Restore(int(os.Stdin.Fd()), state)
136136
}()
137+
138+
windowChange := listenWindowSize(cmd.Context())
139+
go func() {
140+
for {
141+
select {
142+
case <-cmd.Context().Done():
143+
return
144+
case <-windowChange:
145+
}
146+
width, height, _ := term.GetSize(int(stdoutFile.Fd()))
147+
_ = sshSession.WindowChange(height, width)
148+
}
149+
}()
137150
}
138151

139152
err = sshSession.RequestPty("xterm-256color", 128, 128, gossh.TerminalModes{})

cli/ssh_other.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
package cli
5+
6+
import (
7+
"context"
8+
"os"
9+
"os/signal"
10+
11+
"golang.org/x/sys/unix"
12+
)
13+
14+
func listenWindowSize(ctx context.Context) <-chan os.Signal {
15+
windowSize := make(chan os.Signal, 1)
16+
signal.Notify(windowSize, unix.SIGWINCH)
17+
go func() {
18+
<-ctx.Done()
19+
signal.Stop(windowSize)
20+
}()
21+
return windowSize
22+
}

cli/ssh_windows.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//go:build windows
2+
// +build windows
3+
4+
package cli
5+
6+
import (
7+
"context"
8+
"os"
9+
"time"
10+
)
11+
12+
func listenWindowSize(ctx context.Context) <-chan os.Signal {
13+
windowSize := make(chan os.Signal, 3)
14+
ticker := time.NewTicker(time.Second)
15+
go func() {
16+
defer ticker.Stop()
17+
for {
18+
select {
19+
case <-ctx.Done():
20+
return
21+
case <-ticker.C:
22+
}
23+
windowSize <- nil
24+
}
25+
}()
26+
return windowSize
27+
}

0 commit comments

Comments
 (0)