github.com/lonnblad/godog@v0.7.14-0.20200306004719-1b0cb3259847/colors/no_colors.go (about) 1 package colors 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 ) 8 9 type noColors struct { 10 out io.Writer 11 lastbuf bytes.Buffer 12 } 13 14 func Uncolored(w io.Writer) io.Writer { 15 return &noColors{out: w} 16 } 17 18 func (w *noColors) Write(data []byte) (n int, err error) { 19 er := bytes.NewBuffer(data) 20 loop: 21 for { 22 c1, _, err := er.ReadRune() 23 if err != nil { 24 break loop 25 } 26 if c1 != 0x1b { 27 fmt.Fprint(w.out, string(c1)) 28 continue 29 } 30 c2, _, err := er.ReadRune() 31 if err != nil { 32 w.lastbuf.WriteRune(c1) 33 break loop 34 } 35 if c2 != 0x5b { 36 w.lastbuf.WriteRune(c1) 37 w.lastbuf.WriteRune(c2) 38 continue 39 } 40 41 var buf bytes.Buffer 42 for { 43 c, _, err := er.ReadRune() 44 if err != nil { 45 w.lastbuf.WriteRune(c1) 46 w.lastbuf.WriteRune(c2) 47 w.lastbuf.Write(buf.Bytes()) 48 break loop 49 } 50 if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '@' { 51 break 52 } 53 buf.Write([]byte(string(c))) 54 } 55 } 56 return len(data) - w.lastbuf.Len(), nil 57 }