-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixes 1492: tty initial size error #1529
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,9 +16,9 @@ import ( | |
) | ||
|
||
// resizeTtyTo resizes tty to specific height and width | ||
func resizeTtyTo(ctx context.Context, client client.ContainerAPIClient, id string, height, width uint, isExec bool) { | ||
func resizeTtyTo(ctx context.Context, client client.ContainerAPIClient, id string, height, width uint, isExec bool) error { | ||
if height == 0 && width == 0 { | ||
return | ||
return nil | ||
} | ||
|
||
options := types.ResizeOptions{ | ||
|
@@ -34,19 +34,42 @@ func resizeTtyTo(ctx context.Context, client client.ContainerAPIClient, id strin | |
} | ||
|
||
if err != nil { | ||
logrus.Debugf("Error resize: %s", err) | ||
logrus.Debugf("Error resize: %s\r", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reason for adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kolyshkin Nice to see you again ^_^
When we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch! I've definitely seen it in the past. Maybe add a comment why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No code change, modify patch description now. Thanks. |
||
} | ||
return err | ||
} | ||
|
||
// MonitorTtySize updates the container tty size when the terminal tty changes size | ||
func MonitorTtySize(ctx context.Context, cli command.Cli, id string, isExec bool) error { | ||
resizeTty := func() { | ||
height, width := cli.Out().GetTtySize() | ||
resizeTtyTo(ctx, cli.Client(), id, height, width, isExec) | ||
} | ||
// resizeTty is to resize the tty with cli out's tty size | ||
func resizeTty(ctx context.Context, cli command.Cli, id string, isExec bool) error { | ||
height, width := cli.Out().GetTtySize() | ||
return resizeTtyTo(ctx, cli.Client(), id, height, width, isExec) | ||
} | ||
|
||
resizeTty() | ||
// initTtySize is to init the tty's size to the same as the window, if there is an error, it will retry 5 times. | ||
func initTtySize(ctx context.Context, cli command.Cli, id string, isExec bool, resizeTtyFunc func(ctx context.Context, cli command.Cli, id string, isExec bool) error) { | ||
rttyFunc := resizeTtyFunc | ||
if rttyFunc == nil { | ||
rttyFunc = resizeTty | ||
} | ||
if err := rttyFunc(ctx, cli, id, isExec); err != nil { | ||
go func() { | ||
var err error | ||
for retry := 0; retry < 5; retry++ { | ||
time.Sleep(10 * time.Millisecond) | ||
if err = rttyFunc(ctx, cli, id, isExec); err == nil { | ||
break | ||
} | ||
} | ||
if err != nil { | ||
fmt.Fprintln(cli.Err(), "failed to resize tty, using default size") | ||
} | ||
}() | ||
} | ||
} | ||
|
||
// MonitorTtySize updates the container tty size when the terminal tty changes size | ||
func MonitorTtySize(ctx context.Context, cli command.Cli, id string, isExec bool) error { | ||
initTtySize(ctx, cli, id, isExec, resizeTty) | ||
if runtime.GOOS == "windows" { | ||
go func() { | ||
prevH, prevW := cli.Out().GetTtySize() | ||
|
@@ -55,7 +78,7 @@ func MonitorTtySize(ctx context.Context, cli command.Cli, id string, isExec bool | |
h, w := cli.Out().GetTtySize() | ||
|
||
if prevW != w || prevH != h { | ||
resizeTty() | ||
resizeTty(ctx, cli, id, isExec) | ||
} | ||
prevH = h | ||
prevW = w | ||
|
@@ -66,7 +89,7 @@ func MonitorTtySize(ctx context.Context, cli command.Cli, id string, isExec bool | |
gosignal.Notify(sigchan, signal.SIGWINCH) | ||
go func() { | ||
for range sigchan { | ||
resizeTty() | ||
resizeTty(ctx, cli, id, isExec) | ||
} | ||
}() | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package container | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/docker/cli/cli/command" | ||
"github.com/docker/cli/internal/test" | ||
"github.com/docker/docker/api/types" | ||
"github.com/pkg/errors" | ||
"gotest.tools/assert" | ||
is "gotest.tools/assert/cmp" | ||
) | ||
|
||
func TestInitTtySizeErrors(t *testing.T) { | ||
expectedError := "failed to resize tty, using default size\n" | ||
fakeContainerExecResizeFunc := func(id string, options types.ResizeOptions) error { | ||
return errors.Errorf("Error response from daemon: no such exec") | ||
} | ||
fakeResizeTtyFunc := func(ctx context.Context, cli command.Cli, id string, isExec bool) error { | ||
height, width := uint(1024), uint(768) | ||
return resizeTtyTo(ctx, cli.Client(), id, height, width, isExec) | ||
} | ||
ctx := context.Background() | ||
cli := test.NewFakeCli(&fakeClient{containerExecResizeFunc: fakeContainerExecResizeFunc}) | ||
initTtySize(ctx, cli, "8mm8nn8tt8bb", true, fakeResizeTtyFunc) | ||
time.Sleep(100 * time.Millisecond) | ||
assert.Check(t, is.Equal(expectedError, cli.ErrBuffer().String())) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this signature did not change; perhaps you can revert this formatting change to keep the change focussed on the actual changes? Or did
gofmt
force this wrap?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because in circleci:lint, there is an error:
cli/command/container/client_test.go:18::warning: line is 201 characters (lll),
so I need to force this wrap, or else ci will get an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah! hm that's a pity (as it's not related to this change 😞)