如何在golang的tabwriter中使用颜色?

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

How do I get colors to work with golang tabwriter?

问题

我正在使用tabwriter,但无法与颜色一起使用。我正在使用"github.com/fatih/color"包。

基本上问题是,我需要调用tabwriter的w.Flush()才能使颜色渲染...如果我没有调用flush,就无法切换颜色。

而调用Flush会影响tabwriter的格式。

有什么办法可以将这两者结合起来吗?

package main

import "fmt"
import "text/tabwriter"
import "os"
import "github.com/fatih/color"

func main() {
    w := new(tabwriter.Writer)
    w.Init(os.Stderr, 0, 8, 0, '\t', 0)
    color.Set(color.FgGreen)
    fmt.Fprintln(w, "ID\tNAME\tSIZE\tFIELD1\tSTATUS\tSTATE")
    // -------> 在这里调用w.Flush()会导致问题。
    color.Set(color.FgYellow)
    fmt.Fprintln(w, "8617833164795356724\tfoo1\t1.1 Gb\t3\tsome_status\tsome_state")
    fmt.Fprintln(w) 
    w.Flush()
}
英文:

I am using the tabwriter and I cannot get it to work with colors. I am using the "github.com/fatih/color" package.

Basically the problem is that I need to call tabwriter's w.Flush() in order to get the colors to render... I cannot switch colors if I have not called a flush.

Calling Flush in turn screws with the tabwriter formatting.

Any ideas on how to combine the two?

    package main
    
    import "fmt"
    import "text/tabwriter"
    import "os"
    import "github.com/fatih/color"
    
    func main() {
        w := new(tabwriter.Writer)
        w.Init(os.Stderr, 0, 8, 0, '\t', 0)
        color.Set(color.FgGreen)
        fmt.Fprintln(w, "ID\tNAME\tSIZE\tFIELD1\tSTATUS\tSTATE")
        // ------> Calling w.Flush() here cases problems.
        color.Set(color.FgYellow)
        fmt.Fprintln(w, "8617833164795356724\tfoo1\t1.1 Gb\t3\tsome_status\tsome_state")
        fmt.Fprintln(w) 
        w.Flush()
    }

答案1

得分: 9

尽管接受的答案说不行,但是是有可能的,你只需要非常小心地处理字段长度。

用颜色+重置代码包裹每个“字段”(即特定的行和列)。如果所有代码的字符串长度相同,tabwriter 就会给出一个很好的结果。

我在这里有一个简单的演示:https://play.golang.org/p/r6GNeV1gbH

如何在golang的tabwriter中使用颜色?

我在演示中没有这样做,但你也应该添加背景代码(可以简单地将它们相加,如 RedText + YellowBackground),提供一个默认的背景。这样,所有内容的长度都将相等,并且你还将拥有背景支持。

请注意,我是一个初学者的 Go 程序员。我不声称我的代码有多好。

英文:

Despite what the accepted answer says, it is possible, you just have to be very careful about field length.

Wrap each "field" (i.e. a specific row and column) with a color+reset code. If all codes are of the same string length, tabwriter will give you a good result.

I have a crude demonstration here: https://play.golang.org/p/r6GNeV1gbH

如何在golang的tabwriter中使用颜色?

I didn't do so in my demo, but you should also add the background codes as well (you can simply add them together as in RedText + YellowBackground), providing a default background. In this way, everything will be of equal length and you'll have background support as well.

Please note that I am a beginner Go programmer. I don't claim that my code is any good.

答案2

得分: 4

简短回答

你不能这样做。

幼稚的回答

使用color.Color.SprintFunc()方法获取一个函数,并使用该函数包装你的字符串。

真实回答

这种方法也行不通,因为颜色是使用一种特殊的字符序列设置的,而这个字符序列在tabwriter中无法识别,所以这一行的长度将比标记的两倍要短(一个用于设置颜色,一个用于恢复到标准颜色)。

解决方案

编写一个替代的tabwriter(算法甚至不复杂),它可以识别颜色字符序列并忽略它。

英文:

Short answer

You can't.

Naive answer

Use the color.Color.SprintFunc() method to get a function and wrap your strigns using this function.

Real answer

That won't work either, because the color is set using a special character sequence that isn't recognized by the tabwriter, so this row will be shorter by the length of twice the marker (one to set the color and one to get back to the standard color).

Solution

Write an alternative tabwriter (the algoerithm isn't even complex) that recognized the color character sequence and ignore it.

huangapple
  • 本文由 发表于 2016年2月15日 05:56:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/35398497.html
匿名

发表评论

匿名网友

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

确定