如何在Go中获取终端大小?

huangapple go评论118阅读模式
英文:

How do you get the terminal size in Go?

问题

在Go语言中,如何获取终端大小?在C语言中,可以这样写:

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

但是在Go语言中,如何访问TIOCGWINSZ呢?

英文:

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

得分: 3

cgo编译器目前无法处理C函数中的可变参数和C头文件中的宏,因此您无法简单地执行以下操作:

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

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

为了解决宏的问题,您可以使用一个常量,例如:

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

const TIOCGWINSZ C.ulong = 0x5413; // 来自Jed Smith的答案中的值

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

然而,cgo仍然无法处理ioctl原型中的...。您最好的选择是使用一个接受特定数量参数的C函数来包装ioctl,并将其链接进来。作为一种hack,您可以在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; // 来自Jed Smith的答案中的值

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

我没有测试过这个,但类似的方法应该可以工作。

英文:

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 &lt;sys/ioctl.h&gt;
// typedef struct ttysize ttysize;
import &quot;C&quot;

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

To get around the macros use a constant, so

// #include &lt;sys/ioctl.h&gt;
// typedef struct ttysize ttysize;
import &quot;C&quot;

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

func GetWinSz() {
    var ts C.ttysize;
    C.ioctl(0,TIOCGWINSZ,&amp;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 &lt;sys/ioctl.h&gt;
// typedef struct ttysize ttysize;
// void myioctl(int i, unsigned long l, ttysize * t){ioctl(i,l,t);}
import &quot;C&quot;

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

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

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

答案2

得分: 1

最好的方法是使用syscall包。syscall包没有定义ioctl函数,因为它可以执行许多不同的操作,但你仍然可以像这样调用它:

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

剩下的两件事是复制winsize结构和所需的常量。用于此的工具是godefs,它将根据C头文件中的结构和常量生成一个.go源文件。创建一个termios.c文件,内容如下:

#include <termios.h>

enum {
    $TIOCGWINSZ = TIOCGWINSZ
};

typedef winsize $winsize;

然后运行

godefs -gpackagename termios.c > termios.go

现在你应该拥有获取终端大小所需的一切。设置大小只需在termios.c中添加另一个常量即可。

英文:

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(&amp;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 &lt;termios.h&gt;

enum {
    $TIOCGWINSZ = TIOCGWINSZ
};

typedef winsize $winsize;

Now run

godefs -gpackagename termios.c &gt; 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

得分: 1

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
}
英文:

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 == &quot;darwin&quot; {
		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

得分: 0

从文档上看,似乎还没有做太多的工作 - 实际上,我根本找不到ioctl

对于这样一个刚刚起步的语言来说,可以说你正在踏上未曾涉足的领域。TIOCGWINSZ本身只是一个常量整数(我在Linux源代码中找到了它的值):

#define TIOCGWINSZ	0x5413

祝你好运。

英文:

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.

huangapple
  • 本文由 发表于 2009年11月14日 11:38:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/1733155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定