我正在使用Tabwriter,但无法使其与颜色配合使用。我正在使用“ github.com/fatih/color”包。
基本上,问题是我需要调用tabwriterw.Flush()
才能获得要渲染的颜色...如果我没有调用冲洗,则无法切换颜色。
依次调用带有Tabwriter格式的Flush螺钉。
关于如何将两者结合的任何想法?
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()
}
你不能
使用color.Color.SprintFunc()
方法获取函数,并使用此函数包装字符串。
这也不起作用,因为颜色是使用无法识别的特殊字符序列设置的tabwriter
,因此此行的长度将缩短两倍标记的长度(一个用于设置颜色,另一个用于返回)标准颜色)。
编写一个替代制表器(算法并不复杂),该制表器可以识别颜色字符序列并忽略它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句