tc_unix.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // +build darwin freebsd linux solaris
  2. package console
  3. import (
  4. "golang.org/x/sys/unix"
  5. )
  6. func tcget(fd uintptr, p *unix.Termios) error {
  7. termios, err := unix.IoctlGetTermios(int(fd), cmdTcGet)
  8. if err != nil {
  9. return err
  10. }
  11. *p = *termios
  12. return nil
  13. }
  14. func tcset(fd uintptr, p *unix.Termios) error {
  15. return unix.IoctlSetTermios(int(fd), cmdTcSet, p)
  16. }
  17. func tcgwinsz(fd uintptr) (WinSize, error) {
  18. var ws WinSize
  19. uws, err := unix.IoctlGetWinsize(int(fd), unix.TIOCGWINSZ)
  20. if err != nil {
  21. return ws, err
  22. }
  23. // Translate from unix.Winsize to console.WinSize
  24. ws.Height = uws.Row
  25. ws.Width = uws.Col
  26. ws.x = uws.Xpixel
  27. ws.y = uws.Ypixel
  28. return ws, nil
  29. }
  30. func tcswinsz(fd uintptr, ws WinSize) error {
  31. // Translate from console.WinSize to unix.Winsize
  32. var uws unix.Winsize
  33. uws.Row = ws.Height
  34. uws.Col = ws.Width
  35. uws.Xpixel = ws.x
  36. uws.Ypixel = ws.y
  37. return unix.IoctlSetWinsize(int(fd), unix.TIOCSWINSZ, &uws)
  38. }
  39. func setONLCR(fd uintptr, enable bool) error {
  40. var termios unix.Termios
  41. if err := tcget(fd, &termios); err != nil {
  42. return err
  43. }
  44. if enable {
  45. // Set +onlcr so we can act like a real terminal
  46. termios.Oflag |= unix.ONLCR
  47. } else {
  48. // Set -onlcr so we don't have to deal with \r.
  49. termios.Oflag &^= unix.ONLCR
  50. }
  51. return tcset(fd, &termios)
  52. }
  53. func cfmakeraw(t unix.Termios) unix.Termios {
  54. t.Iflag &^= (unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON)
  55. t.Oflag &^= unix.OPOST
  56. t.Lflag &^= (unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN)
  57. t.Cflag &^= (unix.CSIZE | unix.PARENB)
  58. t.Cflag &^= unix.CS8
  59. t.Cc[unix.VMIN] = 1
  60. t.Cc[unix.VTIME] = 0
  61. return t
  62. }