How to set the terminal's size?

為{幸葍}努か 提交于 2019-12-22 08:15:19

问题


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 TIOCGWINSZ in Go


回答1:


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.




回答2:


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.




回答3:


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
}



回答4:


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.



来源:https://stackoverflow.com/questions/1733155/how-to-set-the-terminals-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!