github.com/lonnblad/godog@v0.7.14-0.20200306004719-1b0cb3259847/colors/writer.go (about)

     1  // Copyright 2014 shiena Authors. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package colors
     6  
     7  import "io"
     8  
     9  type outputMode int
    10  
    11  // DiscardNonColorEscSeq supports the divided color escape sequence.
    12  // But non-color escape sequence is not output.
    13  // Please use the OutputNonColorEscSeq If you want to output a non-color
    14  // escape sequences such as ncurses. However, it does not support the divided
    15  // color escape sequence.
    16  const (
    17  	_ outputMode = iota
    18  	discardNonColorEscSeq
    19  	outputNonColorEscSeq // unused
    20  )
    21  
    22  // Colored creates and initializes a new ansiColorWriter
    23  // using io.Writer w as its initial contents.
    24  // In the console of Windows, which change the foreground and background
    25  // colors of the text by the escape sequence.
    26  // In the console of other systems, which writes to w all text.
    27  func Colored(w io.Writer) io.Writer {
    28  	return createModeAnsiColorWriter(w, discardNonColorEscSeq)
    29  }
    30  
    31  // NewModeAnsiColorWriter create and initializes a new ansiColorWriter
    32  // by specifying the outputMode.
    33  func createModeAnsiColorWriter(w io.Writer, mode outputMode) io.Writer {
    34  	if _, ok := w.(*ansiColorWriter); !ok {
    35  		return &ansiColorWriter{
    36  			w:    w,
    37  			mode: mode,
    38  		}
    39  	}
    40  	return w
    41  }