怎样读取windows和linux终端宽度?

leavesdrift · 2017-12-10 09:29:12 · 1473 次点击

@polaris 最后windows下是用mode con | find "Columns"拿到的,linux下应该也能echo $COLUMNS拿到

package main

import (
    "fmt"
    "os/exec"
    "bytes"
    "regexp"
    "os"
    "strconv"
)

var ch = make(chan int)
var out bytes.Buffer

func main() {
    go GetConWidth()
    fmt.Printf("%v", <-ch)
}

func GetConWidth(){
    cmd := exec.Command("mode", "con")
    cmd.Stdout = &out
    err := cmd.Run()
    if err != nil {
        fmt.Fprintf(os.Stderr, "ls: error reading console width %s", err.Error())
    }
    re := regexp.MustCompile(`\d+`)
    rs := re.FindAllString(out.String(), -1)
    i, err := strconv.Atoi(rs[1])
    if err != nil {
        fmt.Fprintf(os.Stderr, "ls: error transfering string to int %s", err.Error())
    }
    ch <-i
}
#3
更多评论
polaris
社区,需要你我一同完善!

Linux 下通过 $LINES$COLUMNS 获取高宽;

Windows 下不太清楚。

#1

文件中怎么获取呢?$COLUMNS 不在 env 中

#2