Skip to content

Commit

Permalink
Actively catching terminal resize.
Browse files Browse the repository at this point in the history
  • Loading branch information
Edu Sr. Popo authored and edupo committed Apr 8, 2018
1 parent af829d3 commit ade4ccb
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
"github.com/docker/docker/client"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/net/context"
pb "gopkg.in/cheggaaa/pb.v1"
"gopkg.in/cheggaaa/pb.v1"
"syscall"
)

// Event holds the json structure for Docker API events
Expand Down Expand Up @@ -266,9 +267,8 @@ func (c *DockerClient) StartContainer(rm bool, name string) (string, error) {
}
}()

tw, th, _ := terminal.GetSize(fd)

if err := c.Cli.ContainerResize(context.Background(), resp.ID, types.ResizeOptions{Height: uint(th), Width: uint(tw)}); err != nil {
err = c.autoResizeContainer(resp.ID)
if err != nil {
return resp.ID, fmt.Errorf("Failed to start container: %s", err)
}

Expand Down Expand Up @@ -409,3 +409,38 @@ func (c *DockerClient) PullImage(image string) error {
fmt.Print("\n")
return nil
}

func (c *DockerClient) autoResizeContainer(id string) error {

// Initial resize
err:= c.resizeContainer(id)
if err != nil {
return err
}

ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGWINCH)

// goroutine to check for the SIGWINCH
go func() {
for err != nil {
<-ch

err = c.resizeContainer(id)
}
log.WithField("container", id).Debug("Finished auto-resize")
}()

return nil
}

func (c *DockerClient) resizeContainer(id string) error {
fd := int(os.Stdin.Fd())
tw, th, _ := terminal.GetSize(fd)

var err error
if err = c.Cli.ContainerResize(context.Background(), id, types.ResizeOptions{Height: uint(th), Width: uint(tw)}); err != nil {
return fmt.Errorf("Failed to resize container: %s", err)
}
return nil
}

0 comments on commit ade4ccb

Please sign in to comment.