github.com/ipfans/trojan-go@v0.11.0/log/golog/colorful/colorful.go (about)

     1  // The color engine for the go-log library
     2  // Copyright (c) 2017 Fadhli Dzil Ikram
     3  
     4  package colorful
     5  
     6  import (
     7  	"runtime"
     8  
     9  	"github.com/ipfans/trojan-go/log/golog/buffer"
    10  )
    11  
    12  // ColorBuffer add color option to buffer append
    13  type ColorBuffer struct {
    14  	buffer.Buffer
    15  }
    16  
    17  // color palette map
    18  var (
    19  	colorOff    = []byte("\033[0m")
    20  	colorRed    = []byte("\033[0;31m")
    21  	colorGreen  = []byte("\033[0;32m")
    22  	colorOrange = []byte("\033[0;33m")
    23  	colorBlue   = []byte("\033[0;34m")
    24  	colorPurple = []byte("\033[0;35m")
    25  	colorCyan   = []byte("\033[0;36m")
    26  	colorGray   = []byte("\033[0;37m")
    27  )
    28  
    29  func init() {
    30  	if runtime.GOOS != "linux" {
    31  		colorOff = []byte("")
    32  		colorRed = []byte("")
    33  		colorGreen = []byte("")
    34  		colorOrange = []byte("")
    35  		colorBlue = []byte("")
    36  		colorPurple = []byte("")
    37  		colorCyan = []byte("")
    38  		colorGray = []byte("")
    39  	}
    40  }
    41  
    42  // Off apply no color to the data
    43  func (cb *ColorBuffer) Off() {
    44  	cb.Append(colorOff)
    45  }
    46  
    47  // Red apply red color to the data
    48  func (cb *ColorBuffer) Red() {
    49  	cb.Append(colorRed)
    50  }
    51  
    52  // Green apply green color to the data
    53  func (cb *ColorBuffer) Green() {
    54  	cb.Append(colorGreen)
    55  }
    56  
    57  // Orange apply orange color to the data
    58  func (cb *ColorBuffer) Orange() {
    59  	cb.Append(colorOrange)
    60  }
    61  
    62  // Blue apply blue color to the data
    63  func (cb *ColorBuffer) Blue() {
    64  	cb.Append(colorBlue)
    65  }
    66  
    67  // Purple apply purple color to the data
    68  func (cb *ColorBuffer) Purple() {
    69  	cb.Append(colorPurple)
    70  }
    71  
    72  // Cyan apply cyan color to the data
    73  func (cb *ColorBuffer) Cyan() {
    74  	cb.Append(colorCyan)
    75  }
    76  
    77  // Gray apply gray color to the data
    78  func (cb *ColorBuffer) Gray() {
    79  	cb.Append(colorGray)
    80  }
    81  
    82  // mixer mix the color on and off byte with the actual data
    83  func mixer(data []byte, color []byte) []byte {
    84  	var result []byte
    85  	return append(append(append(result, color...), data...), colorOff...)
    86  }
    87  
    88  // Red apply red color to the data
    89  func Red(data []byte) []byte {
    90  	return mixer(data, colorRed)
    91  }
    92  
    93  // Green apply green color to the data
    94  func Green(data []byte) []byte {
    95  	return mixer(data, colorGreen)
    96  }
    97  
    98  // Orange apply orange color to the data
    99  func Orange(data []byte) []byte {
   100  	return mixer(data, colorOrange)
   101  }
   102  
   103  // Blue apply blue color to the data
   104  func Blue(data []byte) []byte {
   105  	return mixer(data, colorBlue)
   106  }
   107  
   108  // Purple apply purple color to the data
   109  func Purple(data []byte) []byte {
   110  	return mixer(data, colorPurple)
   111  }
   112  
   113  // Cyan apply cyan color to the data
   114  func Cyan(data []byte) []byte {
   115  	return mixer(data, colorCyan)
   116  }
   117  
   118  // Gray apply gray color to the data
   119  func Gray(data []byte) []byte {
   120  	return mixer(data, colorGray)
   121  }