...

Source file src/github.com/mattn/go-colorable/noncolorable.go

Documentation: github.com/mattn/go-colorable

     1  package colorable
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  )
     7  
     8  // NonColorable holds writer but removes escape sequence.
     9  type NonColorable struct {
    10  	out io.Writer
    11  }
    12  
    13  // NewNonColorable returns new instance of Writer which removes escape sequence from Writer.
    14  func NewNonColorable(w io.Writer) io.Writer {
    15  	return &NonColorable{out: w}
    16  }
    17  
    18  // Write writes data on console
    19  func (w *NonColorable) Write(data []byte) (n int, err error) {
    20  	er := bytes.NewReader(data)
    21  	var plaintext bytes.Buffer
    22  loop:
    23  	for {
    24  		c1, err := er.ReadByte()
    25  		if err != nil {
    26  			plaintext.WriteTo(w.out)
    27  			break loop
    28  		}
    29  		if c1 != 0x1b {
    30  			plaintext.WriteByte(c1)
    31  			continue
    32  		}
    33  		_, err = plaintext.WriteTo(w.out)
    34  		if err != nil {
    35  			break loop
    36  		}
    37  		c2, err := er.ReadByte()
    38  		if err != nil {
    39  			break loop
    40  		}
    41  		if c2 != 0x5b {
    42  			continue
    43  		}
    44  
    45  		for {
    46  			c, err := er.ReadByte()
    47  			if err != nil {
    48  				break loop
    49  			}
    50  			if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '@' {
    51  				break
    52  			}
    53  		}
    54  	}
    55  
    56  	return len(data), nil
    57  }
    58  

View as plain text