resizeevents_windows.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. Copyright 2016 The Kubernetes Authors.
  16. Licensed under the Apache License, Version 2.0 (the "License");
  17. you may not use this file except in compliance with the License.
  18. You may obtain a copy of the License at
  19. http://www.apache.org/licenses/LICENSE-2.0
  20. Unless required by applicable law or agreed to in writing, software
  21. distributed under the License is distributed on an "AS IS" BASIS,
  22. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. See the License for the specific language governing permissions and
  24. limitations under the License.
  25. */
  26. package term
  27. import (
  28. "time"
  29. "yunion.io/x/pkg/util/runtime"
  30. "yunion.io/x/onecloud/pkg/util/pod/remotecommand"
  31. )
  32. // monitorResizeEvents spawns a goroutine that periodically gets the terminal size and tries to send
  33. // it to the resizeEvents channel if the size has changed. The goroutine stops when the stop channel
  34. // is closed.
  35. func monitorResizeEvents(fd uintptr, resizeEvents chan<- remotecommand.TerminalSize, stop chan struct{}) {
  36. go func() {
  37. defer runtime.HandleCrash()
  38. size := GetSize(fd)
  39. if size == nil {
  40. return
  41. }
  42. lastSize := *size
  43. for {
  44. // see if we need to stop running
  45. select {
  46. case <-stop:
  47. return
  48. default:
  49. }
  50. size := GetSize(fd)
  51. if size == nil {
  52. return
  53. }
  54. if size.Height != lastSize.Height || size.Width != lastSize.Width {
  55. lastSize.Height = size.Height
  56. lastSize.Width = size.Width
  57. resizeEvents <- *size
  58. }
  59. // sleep to avoid hot looping
  60. time.Sleep(250 * time.Millisecond)
  61. }
  62. }()
  63. }