github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/colors/colors.go (about)

     1  package colors
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type Color func(args ...interface{}) string
     9  
    10  const (
    11  	ANSIReset      = "\x1b[0m"
    12  	ANSIBright     = "\x1b[1m"
    13  	ANSIDim        = "\x1b[2m"
    14  	ANSIUnderscore = "\x1b[4m"
    15  	ANSIBlink      = "\x1b[5m"
    16  	ANSIReverse    = "\x1b[7m"
    17  	ANSIHidden     = "\x1b[8m"
    18  
    19  	ANSIFgBlack   = "\x1b[30m"
    20  	ANSIFgRed     = "\x1b[31m"
    21  	ANSIFgGreen   = "\x1b[32m"
    22  	ANSIFgYellow  = "\x1b[33m"
    23  	ANSIFgBlue    = "\x1b[34m"
    24  	ANSIFgMagenta = "\x1b[35m"
    25  	ANSIFgCyan    = "\x1b[36m"
    26  	ANSIFgWhite   = "\x1b[37m"
    27  
    28  	ANSIFgGray = "\x1b[90m" // bright black
    29  
    30  	ANSIBgBlack   = "\x1b[40m"
    31  	ANSIBgRed     = "\x1b[41m"
    32  	ANSIBgGreen   = "\x1b[42m"
    33  	ANSIBgYellow  = "\x1b[43m"
    34  	ANSIBgBlue    = "\x1b[44m"
    35  	ANSIBgMagenta = "\x1b[45m"
    36  	ANSIBgCyan    = "\x1b[46m"
    37  	ANSIBgWhite   = "\x1b[47m"
    38  )
    39  
    40  // color the string s with color 'color'
    41  // unless s is already colored
    42  func treat(s string, color string) string {
    43  	if len(s) > 2 && s[:2] == "\x1b[" {
    44  		return s
    45  	}
    46  	return color + s + ANSIReset
    47  }
    48  
    49  func treatAll(color string, args ...interface{}) string {
    50  	parts := make([]string, len(args))
    51  
    52  	for i, arg := range args {
    53  		parts[i] = treat(fmt.Sprintf("%v", arg), color)
    54  	}
    55  
    56  	return strings.Join(parts, "")
    57  }
    58  
    59  func None(args ...interface{}) string {
    60  	return treatAll(ANSIReset, args...)
    61  }
    62  
    63  func Black(args ...interface{}) string {
    64  	return treatAll(ANSIFgBlack, args...)
    65  }
    66  
    67  func Red(args ...interface{}) string {
    68  	return treatAll(ANSIFgRed, args...)
    69  }
    70  
    71  func Green(args ...interface{}) string {
    72  	return treatAll(ANSIFgGreen, args...)
    73  }
    74  
    75  func Yellow(args ...interface{}) string {
    76  	return treatAll(ANSIFgYellow, args...)
    77  }
    78  
    79  func Blue(args ...interface{}) string {
    80  	return treatAll(ANSIFgBlue, args...)
    81  }
    82  
    83  func Magenta(args ...interface{}) string {
    84  	return treatAll(ANSIFgMagenta, args...)
    85  }
    86  
    87  func Cyan(args ...interface{}) string {
    88  	return treatAll(ANSIFgCyan, args...)
    89  }
    90  
    91  func White(args ...interface{}) string {
    92  	return treatAll(ANSIFgWhite, args...)
    93  }
    94  
    95  func Gray(args ...interface{}) string {
    96  	return treatAll(ANSIFgGray, args...)
    97  }
    98  
    99  func ColoredBytes(data []byte, textColor, bytesColor func(...interface{}) string) string {
   100  	s := ""
   101  	for _, b := range data {
   102  		if 0x21 <= b && b < 0x7F {
   103  			s += textColor(string(b))
   104  		} else {
   105  			s += bytesColor(fmt.Sprintf("%02X", b))
   106  		}
   107  	}
   108  	return s
   109  }