需要在文件中根据终端宽度计算输出宽度
<a href="/user/polaris" title="@polaris">@polaris</a> 最后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
更多评论