Created
December 15, 2016 23:41
-
-
Save enrichman/4c1f6a47111d089c1fe7d2f5c4caf409 to your computer and use it in GitHub Desktop.
get width
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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