github.com/night-codes/go-json@v0.9.15/color.go (about)

     1  package json
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/night-codes/go-json/internal/encoder"
     7  )
     8  
     9  type (
    10  	ColorFormat = encoder.ColorFormat
    11  	ColorScheme = encoder.ColorScheme
    12  )
    13  
    14  const escape = "\x1b"
    15  
    16  type colorAttr int
    17  
    18  //nolint:deadcode,varcheck
    19  const (
    20  	fgBlackColor colorAttr = iota + 30
    21  	fgRedColor
    22  	fgGreenColor
    23  	fgYellowColor
    24  	fgBlueColor
    25  	fgMagentaColor
    26  	fgCyanColor
    27  	fgWhiteColor
    28  )
    29  
    30  //nolint:deadcode,varcheck
    31  const (
    32  	fgHiBlackColor colorAttr = iota + 90
    33  	fgHiRedColor
    34  	fgHiGreenColor
    35  	fgHiYellowColor
    36  	fgHiBlueColor
    37  	fgHiMagentaColor
    38  	fgHiCyanColor
    39  	fgHiWhiteColor
    40  )
    41  
    42  func createColorFormat(attr colorAttr) ColorFormat {
    43  	return ColorFormat{
    44  		Header: wrapColor(attr),
    45  		Footer: resetColor(),
    46  	}
    47  }
    48  
    49  func wrapColor(attr colorAttr) string {
    50  	return fmt.Sprintf("%s[%dm", escape, attr)
    51  }
    52  
    53  func resetColor() string {
    54  	return wrapColor(colorAttr(0))
    55  }
    56  
    57  var (
    58  	DefaultColorScheme = &ColorScheme{
    59  		Int:       createColorFormat(fgHiMagentaColor),
    60  		Uint:      createColorFormat(fgHiMagentaColor),
    61  		Float:     createColorFormat(fgHiMagentaColor),
    62  		Bool:      createColorFormat(fgHiYellowColor),
    63  		String:    createColorFormat(fgHiGreenColor),
    64  		Binary:    createColorFormat(fgHiRedColor),
    65  		ObjectKey: createColorFormat(fgHiCyanColor),
    66  		Null:      createColorFormat(fgBlueColor),
    67  	}
    68  )