From 1cb4cdbaea0ad4e5b63e381f3b9cfac6c558b655 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 25 Aug 2023 13:00:26 +0530 Subject: [PATCH] Make the Go example for getting window size complete --- docs/graphics-protocol.rst | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/docs/graphics-protocol.rst b/docs/graphics-protocol.rst index 49b151fc4..fa03c101a 100644 --- a/docs/graphics-protocol.rst +++ b/docs/graphics-protocol.rst @@ -97,10 +97,28 @@ code to demonstrate its use .. code-block:: go - import "golang.org/x/sys/unix" - fd, err := unix.Open(fd, unix.O_NOCTTY|unix.O_CLOEXEC|unix.O_NDELAY|unix.O_RDWR, 0666) - sz, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ) - fmt.Println("rows: %v columns: %v width: %v height %v", sz.Row, sz.Col, sz.Xpixel, sz.Ypixel) + package main + + import ( + "fmt" + "os" + + "golang.org/x/sys/unix" + ) + + func main() { + var err error + var f *os.File + if f, err = os.OpenFile("/dev/tty", unix.O_NOCTTY|unix.O_CLOEXEC|unix.O_NDELAY|unix.O_RDWR, 0666); err == nil { + var sz *unix.Winsize + if sz, err = unix.IoctlGetWinsize(int(f.Fd()), unix.TIOCGWINSZ); err == nil { + fmt.Printf("rows: %v columns: %v width: %v height %v\n", sz.Row, sz.Col, sz.Xpixel, sz.Ypixel) + return + } + } + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } .. tab:: Bash