pb_win.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // +build windows
  2. package pb
  3. import (
  4. "os"
  5. "syscall"
  6. "unsafe"
  7. )
  8. var tty = os.Stdin
  9. var (
  10. kernel32 = syscall.NewLazyDLL("kernel32.dll")
  11. // GetConsoleScreenBufferInfo retrieves information about the
  12. // specified console screen buffer.
  13. // http://msdn.microsoft.com/en-us/library/windows/desktop/ms683171(v=vs.85).aspx
  14. procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
  15. )
  16. type (
  17. // Defines the coordinates of the upper left and lower right corners
  18. // of a rectangle.
  19. // See
  20. // http://msdn.microsoft.com/en-us/library/windows/desktop/ms686311(v=vs.85).aspx
  21. smallRect struct {
  22. Left, Top, Right, Bottom int16
  23. }
  24. // Defines the coordinates of a character cell in a console screen
  25. // buffer. The origin of the coordinate system (0,0) is at the top, left cell
  26. // of the buffer.
  27. // See
  28. // http://msdn.microsoft.com/en-us/library/windows/desktop/ms682119(v=vs.85).aspx
  29. coordinates struct {
  30. X, Y int16
  31. }
  32. word int16
  33. // Contains information about a console screen buffer.
  34. // http://msdn.microsoft.com/en-us/library/windows/desktop/ms682093(v=vs.85).aspx
  35. consoleScreenBufferInfo struct {
  36. dwSize coordinates
  37. dwCursorPosition coordinates
  38. wAttributes word
  39. srWindow smallRect
  40. dwMaximumWindowSize coordinates
  41. }
  42. )
  43. // terminalWidth returns width of the terminal.
  44. func terminalWidth() (width int, err error) {
  45. var info consoleScreenBufferInfo
  46. _, _, e := syscall.Syscall(procGetConsoleScreenBufferInfo.Addr(), 2, uintptr(syscall.Stdout), uintptr(unsafe.Pointer(&info)), 0)
  47. if e != 0 {
  48. return 0, error(e)
  49. }
  50. return int(info.dwSize.X), nil
  51. }