Reputation: 12732
Is it possible to get the terminal width in Go?
I tried using http://github.com/nsf/termbox-go with the code:
package main
import (
"fmt"
"github.com/nsf/termbox-go"
)
func main() {
fmt.Println(termbox.Size())
}
But it prints 0 0
.
I also tried http://github.com/buger/goterm but when I try to go get
it, I get an error:
$ go get github.com/buger/goterm
# github.com/buger/goterm
..\..\buger\goterm\terminal.go:78: undefined: syscall.SYS_IOCTL
..\..\buger\goterm\terminal.go:82: not enough arguments in call to syscall.Syscall
Any other ideas on how to get the terminal width?
Upvotes: 6
Views: 3044
Reputation: 1
You can also use the twin
package:
package main
import "github.com/walles/moar/twin"
func main() {
s, e := twin.NewScreen()
if e != nil {
panic(e)
}
w, h := s.Size()
println(w, h)
}
https://pkg.go.dev/github.com/walles/moar/twin#Screen
Upvotes: 0
Reputation: 36189
You need to call termbox.Init()
before you call termbox.Size()
, and then termbox.Close()
when you're done.
package main
import (
"fmt"
"github.com/nsf/termbox-go"
)
func main() {
if err := termbox.Init(); err != nil {
panic(err)
}
w, h := termbox.Size()
termbox.Close()
fmt.Println(w, h)
}
Upvotes: 8