如何使用颜色属性读取命令输出? [英] How to read a command output with its color attributes?
本文介绍了如何使用颜色属性读取命令输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以读取带有颜色属性的命令输出.我的意思是,我们可以读取实际的转义序列吗.
Is it possible to read a commands output with its color attributes. I mean, can we read the actual escape sequences.
例如;命令输出为红色:
for instance; A command output is red colored:
Hello
我想把它读成:
\033[31;1;4mHello\033[0m
目前我正在阅读它:
func stat(hash string) string {
cmd := exec.Command("git", "show", "--stat", hash)
out, err := cmd.Output()
if err != nil {
return err.Error()
}
return string(out)
}
推荐答案
使用 github.com/creack/pty 库在 pty 中运行命令
Use the github.com/creack/pty library to run the command in a pty
这对我有用
转义序列在输出中可见
package main
import (
"github.com/creack/pty"
"io"
"os"
"os/exec"
)
func main() {
hash := os.Args[1]
cmd := exec.Command("git", "show", "--stat", hash)
f, err := pty.Start(cmd)
if err != nil {
panic(err)
}
io.Copy(os.Stdout, f)
}
这篇关于如何使用颜色属性读取命令输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文