Skip to content

Instantly share code, notes, and snippets.

@enrichman
Created December 15, 2016 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enrichman/4c1f6a47111d089c1fe7d2f5c4caf409 to your computer and use it in GitHub Desktop.
Save enrichman/4c1f6a47111d089c1fe7d2f5c4caf409 to your computer and use it in GitHub Desktop.
get width
// ref: https://github.com/buger/goterm
package main
import (
"fmt"
"os"
"runtime"
"syscall"
"time"
"unsafe"
)
type winsize struct {
Row uint16
Col uint16
Xpixel uint16
Ypixel uint16
}
func main() {
for {
for i := 0; i < getWidth(); i++ {
fmt.Print("#")
}
fmt.Print("\r")
time.Sleep(500 * time.Millisecond)
}
}
func getWidth() (w int) {
ws := new(winsize)
var _TIOCGWINSZ int64
switch runtime.GOOS {
case "linux":
_TIOCGWINSZ = 0x5413
case "darwin":
_TIOCGWINSZ = 1074295912
}
r1, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdin),
uintptr(_TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)),
)
if int(r1) == -1 {
fmt.Println("Error:", os.NewSyscallError("GetWinsize", errno))
}
return int(ws.Col)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment