resize.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. Copyright 2015 The Kubernetes 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 container
  14. import (
  15. "k8s.io/kubernetes/pkg/util/runtime"
  16. "k8s.io/kubernetes/pkg/util/term"
  17. )
  18. // handleResizing spawns a goroutine that processes the resize channel, calling resizeFunc for each
  19. // term.Size received from the channel. The resize channel must be closed elsewhere to stop the
  20. // goroutine.
  21. func HandleResizing(resize <-chan term.Size, resizeFunc func(size term.Size)) {
  22. if resize == nil {
  23. return
  24. }
  25. go func() {
  26. defer runtime.HandleCrash()
  27. for {
  28. size, ok := <-resize
  29. if !ok {
  30. return
  31. }
  32. if size.Height < 1 || size.Width < 1 {
  33. continue
  34. }
  35. resizeFunc(size)
  36. }
  37. }()
  38. }