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

     1  package main
     2  
     3  // NOTE: This is copied over from tendermint/tendermint/libs/common/colors.go
     4  // Do not edit this file without modifying the other.
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  )
    10  
    11  const (
    12  	ANSIReset = "\x1b[0m"
    13  
    14  	ANSIFgRed    = "\x1b[31m"
    15  	ANSIFgGreen  = "\x1b[32m"
    16  	ANSIFgYellow = "\x1b[33m"
    17  	ANSIFgBlue   = "\x1b[34m"
    18  	ANSIFgCyan   = "\x1b[36m"
    19  )
    20  
    21  // color the string s with color 'color'
    22  // unless s is already colored
    23  func treat(s string, color string) string {
    24  	if len(s) > 2 && s[:2] == "\x1b[" {
    25  		return s
    26  	}
    27  	return color + s + ANSIReset
    28  }
    29  
    30  func treatAll(color string, args ...interface{}) string {
    31  	parts := make([]string, len(args))
    32  	for _, arg := range args {
    33  		parts = append(parts, treat(fmt.Sprintf("%v", arg), color))
    34  	}
    35  	return strings.Join(parts, "")
    36  }
    37  
    38  func Red(args ...interface{}) string {
    39  	return treatAll(ANSIFgRed, args...)
    40  }
    41  
    42  func Green(args ...interface{}) string {
    43  	return treatAll(ANSIFgGreen, args...)
    44  }
    45  
    46  func Yellow(args ...interface{}) string {
    47  	return treatAll(ANSIFgYellow, args...)
    48  }
    49  
    50  func Blue(args ...interface{}) string {
    51  	return treatAll(ANSIFgBlue, args...)
    52  }
    53  
    54  func Cyan(args ...interface{}) string {
    55  	return treatAll(ANSIFgCyan, args...)
    56  }
    57  
    58  func ColoredBytes(data []byte, textColor, bytesColor func(...interface{}) string) string {
    59  	s := ""
    60  	for _, b := range data {
    61  		if 0x21 <= b && b < 0x7F {
    62  			s += textColor(string(b))
    63  		} else {
    64  			s += bytesColor(fmt.Sprintf("%02X", b))
    65  		}
    66  	}
    67  	return s
    68  }