github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/terminal/color.go (about)

     1  package terminal
     2  
     3  import (
     4  	"os"
     5  	"regexp"
     6  
     7  	"github.com/fatih/color"
     8  	"golang.org/x/crypto/ssh/terminal"
     9  )
    10  
    11  const (
    12  	red            color.Attribute = color.FgRed
    13  	green                          = color.FgGreen
    14  	yellow                         = color.FgYellow
    15  	magenta                        = color.FgMagenta
    16  	cyan                           = color.FgCyan
    17  	grey                           = color.FgWhite
    18  	defaultFgColor                 = 38
    19  )
    20  
    21  var (
    22  	colorize               func(message string, textColor color.Attribute, bold int) string
    23  	TerminalSupportsColors = isTerminal()
    24  	UserAskedForColors     = ""
    25  )
    26  
    27  func init() {
    28  	InitColorSupport()
    29  }
    30  
    31  func InitColorSupport() {
    32  	if colorsEnabled() {
    33  		colorize = func(message string, textColor color.Attribute, bold int) string {
    34  			colorPrinter := color.New(textColor)
    35  			if bold == 1 {
    36  				colorPrinter = colorPrinter.Add(color.Bold)
    37  			}
    38  			f := colorPrinter.SprintFunc()
    39  			return f(message)
    40  		}
    41  	} else {
    42  		colorize = func(message string, _ color.Attribute, _ int) string {
    43  			return message
    44  		}
    45  	}
    46  }
    47  
    48  func colorsEnabled() bool {
    49  	if os.Getenv("CF_COLOR") == "true" {
    50  		return true
    51  	}
    52  
    53  	if os.Getenv("CF_COLOR") == "false" {
    54  		return false
    55  	}
    56  
    57  	if UserAskedForColors == "true" {
    58  		return true
    59  	}
    60  
    61  	return UserAskedForColors != "false" && TerminalSupportsColors
    62  }
    63  
    64  func Colorize(message string, textColor color.Attribute) string {
    65  	return colorize(message, textColor, 0)
    66  }
    67  
    68  func ColorizeBold(message string, textColor color.Attribute) string {
    69  	return colorize(message, textColor, 1)
    70  }
    71  
    72  var decolorizerRegex = regexp.MustCompile(`\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]`)
    73  
    74  func Decolorize(message string) string {
    75  	return string(decolorizerRegex.ReplaceAll([]byte(message), []byte("")))
    76  }
    77  
    78  func HeaderColor(message string) string {
    79  	return ColorizeBold(message, defaultFgColor)
    80  }
    81  
    82  func CommandColor(message string) string {
    83  	return ColorizeBold(message, yellow)
    84  }
    85  
    86  func StoppedColor(message string) string {
    87  	return ColorizeBold(message, grey)
    88  }
    89  
    90  func AdvisoryColor(message string) string {
    91  	return ColorizeBold(message, yellow)
    92  }
    93  
    94  func CrashedColor(message string) string {
    95  	return ColorizeBold(message, red)
    96  }
    97  
    98  func FailureColor(message string) string {
    99  	return ColorizeBold(message, red)
   100  }
   101  
   102  func SuccessColor(message string) string {
   103  	return ColorizeBold(message, green)
   104  }
   105  
   106  func EntityNameColor(message string) string {
   107  	return ColorizeBold(message, cyan)
   108  }
   109  
   110  func PromptColor(message string) string {
   111  	return ColorizeBold(message, cyan)
   112  }
   113  
   114  func TableContentHeaderColor(message string) string {
   115  	return ColorizeBold(message, cyan)
   116  }
   117  
   118  func WarningColor(message string) string {
   119  	return ColorizeBold(message, magenta)
   120  }
   121  
   122  func LogStdoutColor(message string) string {
   123  	return message
   124  }
   125  
   126  func LogStderrColor(message string) string {
   127  	return Colorize(message, red)
   128  }
   129  
   130  func LogHealthHeaderColor(message string) string {
   131  	return Colorize(message, grey)
   132  }
   133  
   134  func LogAppHeaderColor(message string) string {
   135  	return ColorizeBold(message, yellow)
   136  }
   137  
   138  func LogSysHeaderColor(message string) string {
   139  	return ColorizeBold(message, cyan)
   140  }
   141  
   142  func isTerminal() bool {
   143  	return terminal.IsTerminal(int(os.Stdout.Fd()))
   144  }