/
tty.go
58 lines (47 loc) · 1.19 KB
/
tty.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
package shell
import (
"os"
gosignal "os/signal"
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/moby/sys/signal"
"github.com/skiffos/skiff-core/util/execcmd"
)
// resizeTtyTo resizes tty to specific height and width
func resizeTtyTo(ctx context.Context, client client.ContainerAPIClient, id string, height, width uint, isExec bool) {
if height == 0 && width == 0 {
return
}
options := types.ResizeOptions{
Height: height,
Width: width,
}
var err error
if isExec {
err = client.ContainerExecResize(ctx, id, options)
} else {
err = client.ContainerResize(ctx, id, options)
}
_ = err // Ignore this error for now.
/*
if err != nil {
}
*/
}
// MonitorTtySize updates the container tty size when the terminal tty changes size
func MonitorTtySize(ctx context.Context, client client.APIClient, out *execcmd.OutStream, id string, isExec bool) error {
resizeTty := func() {
height, width := out.GetTtySize()
resizeTtyTo(ctx, client, id, height, width, isExec)
}
resizeTty()
sigchan := make(chan os.Signal, 1)
gosignal.Notify(sigchan, signal.SIGWINCH)
go func() {
for range sigchan {
resizeTty()
}
}()
return nil
}