开发者

How do you get the terminal size in Go?

开发者 https://www.devze.com 2022-12-11 04:56 出处:网络
How do I get the terminal size in Go. In C it would look like this: struct ttysize ts; ioctl(0, TIOCGWINSZ, &ts);

How do I get the terminal size in Go. In C it would look like this:

struct ttysize ts; 
ioctl(0, TIOCGWINSZ, &ts);

But how to i access TIOCGWIN开发者_运维百科SZ in Go


The cgo compiler can't handle variable arguments in a c function and macros in c header files at present, so you can't do a simple

// #include <sys/ioctl.h>
// typedef struct ttysize ttysize;
import "C"

func GetWinSz() {
    var ts C.ttysize;
    C.ioctl(0,C.TIOCGWINSZ,&ts)
}

To get around the macros use a constant, so

// #include <sys/ioctl.h>
// typedef struct ttysize ttysize;
import "C"

const TIOCGWINSZ C.ulong = 0x5413; // Value from Jed Smith's answer

func GetWinSz() {
    var ts C.ttysize;
    C.ioctl(0,TIOCGWINSZ,&ts)
}

However cgo will still barf on the ... in ioctl's prototype. Your best bet would be to wrap ioctl with a c function taking a specific number of arguments and link that in. As a hack you can do that in the comment above import "C"

// #include <sys/ioctl.h>
// typedef struct ttysize ttysize;
// void myioctl(int i, unsigned long l, ttysize * t){ioctl(i,l,t);}
import "C"

const TIOCGWINSZ C.ulong = 0x5413; // Value from Jed Smith's answer

func GetWinSz() {
    var ts C.ttysize;
    C.myioctl(0,TIOCGWINSZ,&ts)
}

I've not tested this, but something similar should work.


The best way to do this would be to use the syscall package. The syscall package does not define an ioctl function because it just does so many different things, but you can still call it like this:

syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(TIOCGWINSZ), uintptr(unsafe.Pointer(&ts)))

The two things left are to duplicate the winsize structure and the constant you need. The tool for this is godefs, which will generate a .go source file by looking at structures and constants in C headers. Create a termios.c file that looks like this:

#include <termios.h>

enum {
    $TIOCGWINSZ = TIOCGWINSZ
};

typedef winsize $winsize;

Now run

godefs -gpackagename termios.c > termios.go

Now you should have everything you need to get the terminal size. Setting the size is as simple as adding another constant to termios.c.


read: http://www.darkcoding.net/software/pretty-command-line-console-output-on-unix-in-python-and-go-lang/

const (
    TIOCGWINSZ     = 0x5413
    TIOCGWINSZ_OSX = 1074295912
)

type window struct {
    Row    uint16
    Col    uint16
    Xpixel uint16
    Ypixel uint16
}

func terminalWidth() (int, error) {
    w := new(window)
    tio := syscall.TIOCGWINSZ
    if runtime.GOOS == "darwin" {
        tio = TIOCGWINSZ_OSX
    }
    res, _, err := syscall.Syscall(syscall.SYS_IOCTL,
        uintptr(syscall.Stdin),
        uintptr(tio),
        uintptr(unsafe.Pointer(w)),
    )
    if int(res) == -1 {
        return 0, err
    }
    return int(w.Col), nil
}


It doesn't look like much work has been done on this yet from a casual glance of the documentation -- in fact, I can't find ioctl at all.

With a language this early in its infancy, it's safe to say that you're treading unwalked ground. TIOCGWINSZ, itself, is just a constant integer (I found its value in the Linux source code):

#define TIOCGWINSZ  0x5413

Good luck, though.

0

精彩评论

暂无评论...
验证码 换一张
取 消