美文网首页
Golang在linux系统上获取terminal终端的宽度

Golang在linux系统上获取terminal终端的宽度

作者: 大地缸 | 来源:发表于2021-02-21 06:50 被阅读0次

title: "Golang在linux系统上获取terminal终端的宽度"
date: 2021-02-09T23:31:59+08:00
draft: true
tags: ['terminal','go']
author: "dadigang"
author_cn: "大地缸"
personal: "http://www.real007.cn"


关于作者

http://www.real007.cn/about

package main

import (
    "fmt"
    "runtime"
    "syscall"
    "unsafe"
)

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
}

func main() {
    width, _ := terminalWidth()
    fmt.Print(width)
}

相关文章

网友评论

      本文标题:Golang在linux系统上获取terminal终端的宽度

      本文链接:https://www.haomeiwen.com/subject/xjypxltx.html