在Windows上输出转义序列

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

Ouput escaped sequences on Windows

问题

有没有类似于colorama(Python)的东西可以用于Go语言?不仅仅是为了输出颜色,还可以像这样写print("\033[1;32mMy text in green\033[0m")并获得带颜色的文本?

我从一个程序中传递一个字符串(比如git log --color=always),它输出转义序列,我希望能够以带颜色的方式输出它。

$ git log --color=always | go run prog.go

如何使转义序列起作用?(我得到这样的东西?[1;32,颜色应该被设置)。除了解析字符串并使用color之类的工具,没有更好的解决方案吗?

英文:

Is there something like colorama (python) for golang? Not just to output color, but to be able to just write print("\033[1;32mMy text in green\033[0m") and get the colored text?

I pipe a string from a program (say git log --color=always) which output escaped sequences, and I want to be able to output it with the colors too.

$ git log --color=always | go run prog.go

How can I make the escaped sequences work? (I get these kinds of thing ?[1;32 where the color was supposed to be set).

Is there no better solution than parsing the string and use color for example?

答案1

得分: 2

以下是翻译好的内容:

<https://github.com/shiena/ansicolor>满足了我的需求。几乎完美。

writer := ansicolor.NewAnsiColorWriter(os.Stdout)
fmt.Fprint(writer, "\033[1;32m我的绿色文本\033[0m")

这样可以工作!

英文:

<https://github.com/shiena/ansicolor> did what I needed. Almost perfect.

writer := ansicolor.NewAnsiColorWriter(os.Stdout)
fmt.Fprint(writer, &quot;3[1;32mMy text in green3[0m&quot;)
# that works!

huangapple
  • 本文由 发表于 2017年3月11日 05:46:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/42728244.html
匿名

发表评论

匿名网友

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

确定