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


本文摘自网络,作者,侵删。


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终端的宽度

相关阅读 >>

asta:是什么终结了我的鸽王生涯?Go 1.13 发布!

Go语言怎么删除数组元素

Golang pprof 性能分析工具

手撸Golang 结构型设计模式 组合模式

Go语言入门教程06 常量

Go高级进阶:Goroutine的创建、休眠与恢复

手撸Golang 基本数据结构与算法 栈

如何实现一个更全面的Golang版本的布谷鸟过滤器

session、cookie等相关基本概念

手撸Golang 行为型设计模式 委派模式

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...