resize.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "os"
  17. gosignal "os/signal"
  18. "runtime"
  19. "time"
  20. "github.com/buger/goterm"
  21. moby "github.com/docker/docker/api/types"
  22. "github.com/docker/docker/pkg/signal"
  23. )
  24. func (s *composeService) monitorTTySize(ctx context.Context, container string, resize func(context.Context, string, moby.ResizeOptions) error) {
  25. err := resize(ctx, container, moby.ResizeOptions{ // nolint:errcheck
  26. Height: uint(goterm.Height()),
  27. Width: uint(goterm.Width()),
  28. })
  29. if err != nil {
  30. return
  31. }
  32. sigchan := make(chan os.Signal, 1)
  33. gosignal.Notify(sigchan, signal.SIGWINCH)
  34. if runtime.GOOS == "windows" {
  35. // Windows has no SIGWINCH support, so we have to poll tty size ¯\_(ツ)_/¯
  36. go func() {
  37. prevH := goterm.Height()
  38. prevW := goterm.Width()
  39. for {
  40. time.Sleep(time.Millisecond * 250)
  41. h := goterm.Height()
  42. w := goterm.Width()
  43. if prevW != w || prevH != h {
  44. sigchan <- signal.SIGWINCH
  45. }
  46. prevH = h
  47. prevW = w
  48. }
  49. }()
  50. }
  51. go func() {
  52. for {
  53. select {
  54. case <-sigchan:
  55. resize(ctx, container, moby.ResizeOptions{ // nolint:errcheck
  56. Height: uint(goterm.Height()),
  57. Width: uint(goterm.Width()),
  58. })
  59. case <-ctx.Done():
  60. return
  61. }
  62. }
  63. }()
  64. }