问题描述

 os.Stdout  os时,该命令非常有用.Stderr 
os.Stdoutos.Stderr

我想将输出和错误输出显示到控制台,但我也希望我的程序查看输出是什么.

I want to display the output and the error output to the console, but I also want my program to see what the output was.

然后我创建了自己的Writer类型,将其写入缓冲区并打印到终端.

I then made my own Writer type that did just that, wrote both to a buffer and printed to the terminal.

这就是问题所在:某些应用程序在检测到未写入tty时,会将其输出更改为人类无法读取的内容.因此,当我以后者的方式进行输出时,输出变得难看.(计算机更清洁,人类更难看)

Here's the problem—some applications change their output to something much less readable by humans when it detects it's not writing to a tty. So the output I get changes to something ugly when I do it in the latter way. (cleaner for computers, uglier for humans)

 os.Stdout/os.Stderr  script 
os.Stdout/os.Stderrscript

提前谢谢!

推荐答案

解决此问题的唯一实用方法是分配

The only practical way to solve this is to allocate a pseudo terminal (PTY) and make your external process use it for its output: since PTY is still a terminal, a process checking whether it's connected to a real terminal thinks it is.

 github.com/creack/pty 
github.com/creack/pty

下一步是让实现PTY的程序包实际分配它,并将PTY的另一端"连接到您的自定义编写器.

The next step is to have a package implementing a PTY actually allocate it, and connect "the other end" of a PTY to your custom writer.

 io.MultiWriter 
io.MultiWriter

这篇关于Golang获取命令tty输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!