-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
terminal_unix.go
69 lines (54 loc) · 1.36 KB
/
terminal_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//go:build !windows && !excludenative
package ssh
import (
"encoding/binary"
"os"
"os/signal"
"syscall"
"golang.org/x/crypto/ssh"
"golang.org/x/term"
)
func setupTerminal() (func(), error) {
fd := int(os.Stdin.Fd())
state, err := term.MakeRaw(fd)
if err != nil {
return nil, err
}
return func() { term.Restore(fd, state) }, nil
}
func requestPty(session *ssh.Session) error {
w, h, err := term.GetSize(int(os.Stdin.Fd()))
if err != nil {
return err
}
if err := session.RequestPty(
os.Getenv("TERM"),
h, w,
ssh.TerminalModes{},
); err != nil {
return err
}
go resizeSessionOnWindowChange(session, os.Stdin.Fd())
return nil
}
// resizeSessionOnWindowChange watches for SIGWINCH and refreshes the session with the new window size
func resizeSessionOnWindowChange(session *ssh.Session, _ uintptr) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGWINCH)
for range sigs {
session.SendRequest("window-change", false, windowSizePayloadFor())
}
}
func windowSizePayloadFor() []byte {
w, h, err := term.GetSize(int(os.Stdin.Fd()))
if err != nil {
return buildWindowSizePayload(80, 24)
}
return buildWindowSizePayload(w, h)
}
func buildWindowSizePayload(width, height int) []byte {
size := make([]byte, 16)
binary.BigEndian.PutUint32(size, uint32(width))
binary.BigEndian.PutUint32(size[4:], uint32(height))
return size
}