Debian Codegraph @

rainbow/rainbow.go

raw symbols SCIP
1 // Package rainbow makes your io.Writer much more joyful.
2 package rainbow
3
4 import (
5 "bytes"
6 "github.com/aybabtme/rgbterm"
7 "io"
8 )
9
10 // Rainbow prints all the colors of the rainbow! All 232 of them colors!
11 type Rainbow struct {
12 wrap io.Writer
13 h, s, l float64
14 }
15
16 // New creates a Rainbow writer, wrapping each byte with a new color
17 // of the rainbow. This is clearly a good idea! This is especially
18 // useful when w is something like a net.Conn or the output of a log.
19 func New(w io.Writer, r, g, b uint8) *Rainbow {
20 h, s, l := rgbterm.RGBtoHSL(r, g, b)
21 return &Rainbow{wrap: w, h: h, s: s, l: l}
22 }
23
24 // Write wraps each byte with an RGB color. Weeee!
25 func (r *Rainbow) Write(p []byte) (int, error) {
26
27 buf := bytes.NewBuffer(nil)
28 for i := range p {
29 r.h += (0.5 / 360)
30 if r.h > 1.0 {
31 r.h = 0
32 }
33
34 r, g, b := rgbterm.HSLtoRGB(r.h, r.s, r.l)
35 if p[i] != '\n' {
36 _, _ = buf.Write(rgbterm.FgByte(p[i], r, g, b))
37 } else {
38 buf.Write([]byte{p[i]})
39 }
40
41 }
42
43 _, err := buf.WriteTo(r.wrap)
44 return len(p), err
45 }