github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zlog/color.go (about)

     1  package zlog
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/sohaha/zlsgo/zutil"
     9  
    10  	"github.com/sohaha/zlsgo/zstring"
    11  )
    12  
    13  // DisableColor DisableColor
    14  var DisableColor = false
    15  
    16  // Color Color
    17  type Color int
    18  
    19  // Format Format
    20  type Format int
    21  
    22  // Op Op
    23  type Op int
    24  
    25  const (
    26  	// ColorBlack black
    27  	ColorBlack Color = iota + 30
    28  	// ColorRed gules
    29  	ColorRed
    30  	// ColorGreen green
    31  	ColorGreen
    32  	// ColorYellow yellow
    33  	ColorYellow
    34  	// ColorBlue blue
    35  	ColorBlue
    36  	// ColorMagenta magenta
    37  	ColorMagenta
    38  	// ColorCyan cyan
    39  	ColorCyan
    40  	// ColorWhite white
    41  	ColorWhite
    42  )
    43  
    44  const (
    45  	// ColorLightGrey light grey
    46  	ColorLightGrey Color = iota + 90
    47  	// ColorLightRed light red
    48  	ColorLightRed
    49  	// ColorLightGreen light green
    50  	ColorLightGreen
    51  	// ColorLightYellow light yellow
    52  	ColorLightYellow
    53  	// ColorLightBlue light blue
    54  	ColorLightBlue
    55  	// ColorLightMagenta light magenta
    56  	ColorLightMagenta
    57  	// ColorLightCyan lightcyan
    58  	ColorLightCyan
    59  	// ColorLightWhite light white
    60  	ColorLightWhite
    61  	// ColorDefault ColorDefault
    62  	ColorDefault = 49
    63  )
    64  const (
    65  	// OpReset Reset All Settings
    66  	OpReset Op = iota
    67  	// OpBold Bold
    68  	OpBold
    69  	// OpFuzzy Fuzzy (not all terminal emulators support it)
    70  	OpFuzzy
    71  	// OpItalic Italic (not all terminal emulators support it)
    72  	OpItalic
    73  	// OpUnderscore Underline
    74  	OpUnderscore
    75  	// OpBlink Twinkle
    76  	OpBlink
    77  	// OpFastBlink Fast scintillation (not widely supported)
    78  	OpFastBlink
    79  	// OpReverse Reversed Exchange Background and Foreground Colors
    80  	OpReverse
    81  	// OpConcealed Concealed
    82  	OpConcealed
    83  	// OpStrikethrough Deleted lines (not widely supported)
    84  	OpStrikethrough
    85  )
    86  
    87  // OpTextWrap OpTextWrap
    88  func OpTextWrap(op Op, text string) string {
    89  	if !isSupportColor() {
    90  		return text
    91  	}
    92  	return fmt.Sprintf("\x1b[%dm%s\x1b[0m", op, text)
    93  }
    94  
    95  // ColorBackgroundWrap ColorBackgroundWrap
    96  func ColorBackgroundWrap(color Color, backgroundColor Color, text string) string {
    97  	if !isSupportColor() {
    98  		return text
    99  	}
   100  	return fmt.Sprintf("\x1b[%d;%dm%s\x1b[0m", color, backgroundColor+10, text)
   101  }
   102  
   103  // OutAllColor OutAllColor
   104  func OutAllColor() {
   105  	all := zstring.Buffer()
   106  	colors := GetAllColorText()
   107  	for k, v := range colors {
   108  		all.WriteString("\n\nBackground " + k + "\n")
   109  		for ck, cv := range colors {
   110  			if cv == v {
   111  				continue
   112  			}
   113  			all.WriteString(ColorBackgroundWrap(cv, v, ck+" => "))
   114  			all.WriteString(ColorBackgroundWrap(cv, v, OpTextWrap(OpBold, "Bold ")))
   115  			all.WriteString(ColorBackgroundWrap(cv, v, OpTextWrap(OpUnderscore, "Under")))
   116  			all.WriteString(ColorBackgroundWrap(cv, v, " | "))
   117  		}
   118  		all.WriteString("\n")
   119  	}
   120  	fmt.Println(all.String())
   121  }
   122  
   123  // GetAllColorText GetAllColorText
   124  func GetAllColorText() map[string]Color {
   125  	return map[string]Color{
   126  		"ColorBlack":        ColorBlack,
   127  		"ColorRed":          ColorRed,
   128  		"ColorGreen":        ColorGreen,
   129  		"ColorYellow":       ColorYellow,
   130  		"ColorBlue":         ColorBlue,
   131  		"ColorMagenta":      ColorMagenta,
   132  		"ColorCyan":         ColorCyan,
   133  		"ColorWhite":        ColorWhite,
   134  		"ColorLightGrey":    ColorLightGrey,
   135  		"ColorLightRed":     ColorLightRed,
   136  		"ColorLightGreen":   ColorLightGreen,
   137  		"ColorLightYellow":  ColorLightYellow,
   138  		"ColorLightBlue":    ColorLightBlue,
   139  		"ColorLightMagenta": ColorLightMagenta,
   140  		"ColorLightCyan":    ColorLightCyan,
   141  		"ColorLightWhite":   ColorLightWhite,
   142  		"ColorDefault":      ColorDefault,
   143  	}
   144  }
   145  
   146  // ColorTextWrap ColorTextWrap
   147  func ColorTextWrap(color Color, text string) string {
   148  	if !isSupportColor() {
   149  		return text
   150  	}
   151  	return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color, text)
   152  }
   153  
   154  var supportColor bool
   155  var isMsystem = os.Getenv("MSYSTEM") != ""
   156  
   157  func init() {
   158  	if zutil.IsWin() && isMsystem {
   159  		return
   160  	}
   161  	term := os.Getenv("TERM")
   162  	supportColor = strings.Contains(term, "xterm") || os.Getenv("ConEmuANSI") == "ON" || os.Getenv("ANSICON") != "" || strings.Contains(term, "256color")
   163  }
   164  
   165  func isSupportColor() bool {
   166  	return !DisableColor && IsSupportColor()
   167  }
   168  
   169  func TrimAnsi(str string) string {
   170  	str, _ = zstring.RegexReplace(`\x1b\[[0-9;]*[a-zA-Z]`, str, "")
   171  	return str
   172  }