github.com/neilotoole/jsoncolor@v0.7.2-0.20231115150201-1637fae69be1/jsoncolor.go (about)

     1  package jsoncolor
     2  
     3  import (
     4  	"strconv"
     5  )
     6  
     7  // Colors specifies colorization of JSON output. Each field
     8  // is a Color, which is simply the bytes of the terminal color code.
     9  type Colors struct {
    10  	// Null is the color for JSON nil.
    11  	Null Color
    12  
    13  	// Bool is the color for boolean values.
    14  	Bool Color
    15  
    16  	// Number is the color for number values.
    17  	Number Color
    18  
    19  	// String is the color for string values.
    20  	String Color
    21  
    22  	// Key is the color for JSON keys.
    23  	Key Color
    24  
    25  	// Bytes is the color for byte data.
    26  	Bytes Color
    27  
    28  	// Time is the color for datetime values.
    29  	Time Color
    30  
    31  	// Punc is the color for JSON punctuation: []{},: etc.
    32  	Punc Color
    33  
    34  	// TextMarshaler is the color for values implementing encoding.TextMarshaler.
    35  	TextMarshaler Color
    36  }
    37  
    38  // appendNull appends a colorized "null" to b.
    39  func (c *Colors) appendNull(b []byte) []byte {
    40  	if c == nil {
    41  		return append(b, "null"...)
    42  	}
    43  
    44  	b = append(b, c.Null...)
    45  	b = append(b, "null"...)
    46  	return append(b, ansiReset...)
    47  }
    48  
    49  // appendBool appends the colorized bool v to b.
    50  func (c *Colors) appendBool(b []byte, v bool) []byte {
    51  	if c == nil {
    52  		if v {
    53  			return append(b, "true"...)
    54  		}
    55  
    56  		return append(b, "false"...)
    57  	}
    58  
    59  	b = append(b, c.Bool...)
    60  	if v {
    61  		b = append(b, "true"...)
    62  	} else {
    63  		b = append(b, "false"...)
    64  	}
    65  
    66  	return append(b, ansiReset...)
    67  }
    68  
    69  // appendInt64 appends the colorized int64 v to b.
    70  func (c *Colors) appendInt64(b []byte, v int64) []byte {
    71  	if c == nil {
    72  		return strconv.AppendInt(b, v, 10)
    73  	}
    74  
    75  	b = append(b, c.Number...)
    76  	b = strconv.AppendInt(b, v, 10)
    77  	return append(b, ansiReset...)
    78  }
    79  
    80  // appendUint64 appends the colorized uint64 v to b.
    81  func (c *Colors) appendUint64(b []byte, v uint64) []byte {
    82  	if c == nil {
    83  		return strconv.AppendUint(b, v, 10)
    84  	}
    85  
    86  	b = append(b, c.Number...)
    87  	b = strconv.AppendUint(b, v, 10)
    88  	return append(b, ansiReset...)
    89  }
    90  
    91  // appendPunc appends the colorized punctuation mark v to b.
    92  func (c *Colors) appendPunc(b []byte, v byte) []byte {
    93  	if c == nil {
    94  		return append(b, v)
    95  	}
    96  
    97  	b = append(b, c.Punc...)
    98  	b = append(b, v)
    99  	return append(b, ansiReset...)
   100  }
   101  
   102  // Color is used to render terminal colors. In effect, Color is
   103  // the bytes of the ANSI prefix code. The zero value is valid (results in
   104  // no colorization). When Color is non-zero, the encoder writes the prefix,
   105  // then the actual value, then the ANSI reset code.
   106  //
   107  // Example value:
   108  //
   109  //	number := Color("\x1b[36m")
   110  type Color []byte
   111  
   112  // ansiReset is the ANSI ansiReset escape code.
   113  const ansiReset = "\x1b[0m"
   114  
   115  // DefaultColors returns the default Colors configuration.
   116  // These colors largely follow jq's default colorization,
   117  // with some deviation.
   118  func DefaultColors() *Colors {
   119  	return &Colors{
   120  		Null:          Color("\x1b[2m"),
   121  		Bool:          Color("\x1b[1m"),
   122  		Number:        Color("\x1b[36m"),
   123  		String:        Color("\x1b[32m"),
   124  		Key:           Color("\x1b[34;1m"),
   125  		Bytes:         Color("\x1b[2m"),
   126  		Time:          Color("\x1b[32;2m"),
   127  		Punc:          Color{},           // No colorization
   128  		TextMarshaler: Color("\x1b[32m"), // Same as String
   129  	}
   130  }