在Go中获取终端大小

Ili*_*nko 24 go tty

如何用Go lang获得tty尺寸?我正在尝试执行stty size命令,但我无法正确编写代码.

package main

import (
  "os/exec"
  "fmt"
  "log"
)

func main() {
  out, err := exec.Command("stty", "size").Output()
  fmt.Printf("out: %#v\n", out)
  fmt.Printf("err: %#v\n", err)
  if err != nil {
    log.Fatal(err)
  }
}
Run Code Online (Sandbox Code Playgroud)

输出:

out: []byte{}
err: &exec.ExitError{ProcessState:(*os.ProcessState)(0xc200066520)}
2013/05/16 02:35:57 exit status 1
exit status 1
Run Code Online (Sandbox Code Playgroud)

我认为这是因为Go产生了一个与当前tty无关的进程,它正在使用它.如何将命令与当前终端关联以获得其大小?

c.g*_*.ga 27

您可以使用golang.org/x/term包(https://pkg.go.dev/golang.org/x/term

例子

package main

import "golang.org/x/term"

func main() {
    if term.IsTerminal(0) {
        println("in a term")
    } else {
        println("not in a term")
    }
    width, height, err := term.GetSize(0)
    if err != nil {
        return
    }
    println("width:", width, "height:", height)
}
Run Code Online (Sandbox Code Playgroud)

输出

in a term
width: 228 height: 27
Run Code Online (Sandbox Code Playgroud)


def*_*ode 23

我遇到了类似的问题.这就是我最终的结果.

它不使用子进程,因此在某些情况下可能需要.

import (
    "syscall"
    "unsafe"
)

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

func getWidth() uint {
    ws := &winsize{}
    retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
        uintptr(syscall.Stdin),
        uintptr(syscall.TIOCGWINSZ),
        uintptr(unsafe.Pointer(ws)))

    if int(retCode) == -1 {
        panic(errno)
    }
    return uint(ws.Col)
}
Run Code Online (Sandbox Code Playgroud)


lnm*_*nmx 17

如果您为子进程提供对父进程的访问权限,则它可以工作stdin:

package main

import (
  "os/exec"
  "fmt"
  "log"
  "os"
)

func main() {
  cmd := exec.Command("stty", "size")
  cmd.Stdin = os.Stdin
  out, err := cmd.Output()
  fmt.Printf("out: %#v\n", string(out))
  fmt.Printf("err: %#v\n", err)
  if err != nil {
    log.Fatal(err)
  }
}
Run Code Online (Sandbox Code Playgroud)

产量:

out: "36 118\n"
err: <nil>
Run Code Online (Sandbox Code Playgroud)


pec*_*rin 15

我刚才想添加一个新答案,因为我最近遇到了这个问题.终端软件包位于官方ssh软件包https://godoc.org/golang.org/x/crypto/ssh/terminal中.

该软件包提供了一种轻松获取终端大小的方法.

width, height, err := terminal.GetSize(0)

0将是您想要的大小的终端的文件描述符.要获得fd或您当前的终端,您可以随时使用int(os.Stdin.Fd())

在封面下,它使用系统调用来获取给定fd的终端大小.

  • 如果 `os.Stdin` 未连接到终端,例如从管道读取时,您可以使用 `int(os.Stdout.Fd())` 来查找尺寸。 (2认同)

Nat*_* F. 9

由于这里还没有其他人提出可以同时在WindowsUnix上运行的跨平台解决方案,因此我继续构建了一个支持这两种平台的库。

https://github.com/nathan-fiscaletti/consolesize-go

package main

import (
    "fmt"

    "github.com/nathan-fiscaletti/consolesize-go"
)

func main() {
    cols, rows := consolesize.GetConsoleSize()
    fmt.Printf("Rows: %v, Cols: %v\n", rows, cols)
}
Run Code Online (Sandbox Code Playgroud)


Way*_*rry 6

如果有人有兴趣我做了一个包,让这更容易.

https://github.com/wayneashleyberry/terminal-dimensions

package main

import (
    "fmt"

    terminal "github.com/wayneashleyberry/terminal-dimensions"
)

func main() {
    x, _ := terminal.Width()
    y, _ := terminal.Height()
    fmt.Printf("Terminal is %d wide and %d high", x, y)
}
Run Code Online (Sandbox Code Playgroud)

  • 没有 Windows 支持,这使得该包对于跨平台开发毫无用处。 (2认同)