os/exec StdoutPipe 带有颜色

huangapple go评论81阅读模式
英文:

os/exec StdoutPipe with colors

问题

我正在使用Go语言为Minecraft服务器控制台编写一个包装器。它使用os/exec来运行服务器,并使用process.StdoutPipe()来获取子进程的实时输出。

但是,由于某种原因,我无法显示颜色。当我直接从终端运行服务器时,颜色是正常的,但是当我从Go中运行时,颜色完全不起作用。

更奇怪的是,当我从Go中运行时,日志文件中也没有颜色代码。但是当我直接从终端运行时,日志文件中充斥着颜色代码。我真的不明白这是怎么发生的。

有没有办法解决这个问题呢?
谢谢!

英文:

I'm making a wrapper for a minecraft server console with Go. It uses os/exec to run the server and process.StdoutPipe() for realtime output from the subprocess.

But, I can't get it to display the colors for some reason. When I run the server directly from the terminal, colors work, but it doesn't work at all when I run it from Go.

What's more is there's no color codes on the log files either when I run it from Go. But running it directly from the terminal, the log file is absolutely messed up with color codes. I really don't get how that happens.

Is there any way to fix this?
Thanks!

答案1

得分: 4

解决这个问题的方法是从Minecraft服务器强制使用颜色。这完全不是你的Go程序的问题。命令行程序通常会检测它们是否在交互式终端上运行,并在不是的情况下禁用某些功能(如颜色输出、分页等),因为它们假设正在通过脚本传递(正如在你的情况下)。如果Minecraft服务器没有这个选项,你可以尝试欺骗它以使其认为它在交互式终端上运行

英文:

The way to fix this is to force colors from the minecraft server. It's not a problem with your Go program at all. It's very common for command-line programs to detect whether they're running on an interactive terminal, and disable certain features (such as color output, paging, etc) when they are not, under the assumption that they're being passed through a script (as indeed it is, in your case).

If the minecraft server doesn't have this option, you may be able to fool it into thinking it's running on an interactive terminal.

huangapple
  • 本文由 发表于 2017年7月15日 18:36:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/45117214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定