英文:
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, "3[1;32mMy text in green3[0m")
# that works!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论