Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get terminal width in Windows using golang

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?

like image 271
kolrie Avatar asked Feb 09 '15 20:02

kolrie


Video Answer


1 Answers

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)
}
like image 125
Ainar-G Avatar answered Nov 12 '22 22:11

Ainar-G