本文摘自网络,作者,侵删。
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终端的宽度
相关阅读 >>
Golang中main中panic和后续panic处理,以及新开协程的影响
更多相关阅读请进入《Go》频道 >>

Go语言101
一个与时俱进的Go编程知识库。