readline
readline copied to clipboard
readline.GetSize doesn't work on Windows
Hello,
I wrote a pretty simple Go script and tried to run it on different OS:
package main
import (
"fmt"
"github.com/chzyer/readline"
)
func main() {
rl, err := readline.New("> ")
if err != nil {
panic(err)
}
defer rl.Close()
_, maxRows, err := readline.GetSize(0)
if err != nil {
fmt.Println(err)
}
fmt.Println("Current dimentions is:", maxRows)
for {
line, err := rl.Readline()
if err != nil {
fmt.Println(err)
break
}
fmt.Println(line)
}
}
And it work fine on Linux system (Centos 6.5):
$: go run test.go
Current dimentions is: 65
> Hello
Hello
But fails on Windows (10):
C:\>go run test.go
The handle is invalid.
Actually, I'm getting the same error The handle is invalid. when calling another readline functions, like readline.MakeRaw(0). I feel that I missed something or use the lib in incorrect way for Windows.
Could you please assist me with it?
The issue I think is with your code:
readline.GetSize(0)
GetSize requires the fd of the TTY, which in some cases will be zero (eg if it's your first console on Linux) but in many cases wont be. Assuming readline is outputting to the default stdout, you can find the value with:
os.Stdout.Fd()
So your code would read something like:
fd := int(os.Stdout.Fd())
_, maxRows, err := readline.GetSize(fd)