resizeevents_windows.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. Copyright 2016 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 term
  14. import (
  15. "time"
  16. "k8s.io/apimachinery/pkg/util/runtime"
  17. "k8s.io/client-go/tools/remotecommand"
  18. )
  19. // monitorResizeEvents spawns a goroutine that periodically gets the terminal size and tries to send
  20. // it to the resizeEvents channel if the size has changed. The goroutine stops when the stop channel
  21. // is closed.
  22. func monitorResizeEvents(fd uintptr, resizeEvents chan<- remotecommand.TerminalSize, stop chan struct{}) {
  23. go func() {
  24. defer runtime.HandleCrash()
  25. size := GetSize(fd)
  26. if size == nil {
  27. return
  28. }
  29. lastSize := *size
  30. for {
  31. // see if we need to stop running
  32. select {
  33. case <-stop:
  34. return
  35. default:
  36. }
  37. size := GetSize(fd)
  38. if size == nil {
  39. return
  40. }
  41. if size.Height != lastSize.Height || size.Width != lastSize.Width {
  42. lastSize.Height = size.Height
  43. lastSize.Width = size.Width
  44. resizeEvents <- *size
  45. }
  46. // sleep to avoid hot looping
  47. time.Sleep(250 * time.Millisecond)
  48. }
  49. }()
  50. }