pb_x.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // +build linux darwin freebsd netbsd openbsd solaris dragonfly
  2. // +build !appengine
  3. package pb
  4. import (
  5. "errors"
  6. "fmt"
  7. "os"
  8. "os/signal"
  9. "runtime"
  10. "sync"
  11. "syscall"
  12. "unsafe"
  13. )
  14. const (
  15. TIOCGWINSZ = 0x5413
  16. TIOCGWINSZ_OSX = 1074295912
  17. )
  18. var tty *os.File
  19. var ErrPoolWasStarted = errors.New("Bar pool was started")
  20. var echoLocked bool
  21. var echoLockMutex sync.Mutex
  22. func init() {
  23. var err error
  24. tty, err = os.Open("/dev/tty")
  25. if err != nil {
  26. tty = os.Stdin
  27. }
  28. }
  29. // terminalWidth returns width of the terminal.
  30. func terminalWidth() (int, error) {
  31. w := new(window)
  32. tio := syscall.TIOCGWINSZ
  33. if runtime.GOOS == "darwin" {
  34. tio = TIOCGWINSZ_OSX
  35. }
  36. res, _, err := syscall.Syscall(sysIoctl,
  37. tty.Fd(),
  38. uintptr(tio),
  39. uintptr(unsafe.Pointer(w)),
  40. )
  41. if int(res) == -1 {
  42. return 0, err
  43. }
  44. return int(w.Col), nil
  45. }
  46. var oldState syscall.Termios
  47. func lockEcho() (quit chan int, err error) {
  48. echoLockMutex.Lock()
  49. defer echoLockMutex.Unlock()
  50. if echoLocked {
  51. err = ErrPoolWasStarted
  52. return
  53. }
  54. echoLocked = true
  55. fd := tty.Fd()
  56. if _, _, e := syscall.Syscall6(sysIoctl, fd, ioctlReadTermios, uintptr(unsafe.Pointer(&oldState)), 0, 0, 0); e != 0 {
  57. err = fmt.Errorf("Can't get terminal settings: %v", e)
  58. return
  59. }
  60. newState := oldState
  61. newState.Lflag &^= syscall.ECHO
  62. newState.Lflag |= syscall.ICANON | syscall.ISIG
  63. newState.Iflag |= syscall.ICRNL
  64. if _, _, e := syscall.Syscall6(sysIoctl, fd, ioctlWriteTermios, uintptr(unsafe.Pointer(&newState)), 0, 0, 0); e != 0 {
  65. err = fmt.Errorf("Can't set terminal settings: %v", e)
  66. return
  67. }
  68. quit = make(chan int, 1)
  69. go catchTerminate(quit)
  70. return
  71. }
  72. func unlockEcho() (err error) {
  73. echoLockMutex.Lock()
  74. defer echoLockMutex.Unlock()
  75. if !echoLocked {
  76. return
  77. }
  78. echoLocked = false
  79. fd := tty.Fd()
  80. if _, _, e := syscall.Syscall6(sysIoctl, fd, ioctlWriteTermios, uintptr(unsafe.Pointer(&oldState)), 0, 0, 0); e != 0 {
  81. err = fmt.Errorf("Can't set terminal settings")
  82. }
  83. return
  84. }
  85. // listen exit signals and restore terminal state
  86. func catchTerminate(quit chan int) {
  87. sig := make(chan os.Signal, 1)
  88. signal.Notify(sig, os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGKILL)
  89. defer signal.Stop(sig)
  90. select {
  91. case <-quit:
  92. unlockEcho()
  93. case <-sig:
  94. unlockEcho()
  95. }
  96. }