1 Star 0 Fork 0

亚运/tinygo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
colorwriter.go 819 Bytes
一键复制 编辑 原始数据 按行查看 历史
package main
import (
"io"
)
// ANSI escape codes for terminal colors.
const (
TermColorReset = "\x1b[0m"
TermColorYellow = "\x1b[93m"
)
// ColorWriter wraps an io.Writer but adds a prefix and a terminal color.
type ColorWriter struct {
Out io.Writer
Color string
Prefix string
line []byte
}
// Write implements io.Writer, but with an added prefix and terminal color.
func (w *ColorWriter) Write(p []byte) (n int, err error) {
for _, c := range p {
if c == '\n' {
w.line = append(w.line, []byte(TermColorReset)...)
w.line = append(w.line, '\n')
// Write this line.
_, err := w.Out.Write(w.line)
w.line = w.line[:0]
w.line = append(w.line, []byte(w.Color+w.Prefix)...)
if err != nil {
return 0, err
}
} else {
w.line = append(w.line, c)
}
}
return len(p), nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/heyayun/tinygo.git
git@gitee.com:heyayun/tinygo.git
heyayun
tinygo
tinygo
master

搜索帮助