|
| 1 | +package mage |
| 2 | + |
| 3 | +//nolint:revive // these are named this way because we also use this code in the generated output and we don't want the imports to potentially conflict with globals in user code. |
| 4 | +import ( |
| 5 | + _fmt "fmt" |
| 6 | + _os "os" |
| 7 | + _strconv "strconv" |
| 8 | + _strings "strings" |
| 9 | +) |
| 10 | + |
| 11 | +var printName = func(str string) string { |
| 12 | + // color is ANSI color type |
| 13 | + type color int |
| 14 | + |
| 15 | + // If you add/change/remove any items in this constant, |
| 16 | + // you will need to run "stringer -type=color" in this directory again. |
| 17 | + // NOTE: Please keep the list in an alphabetical order. |
| 18 | + const ( |
| 19 | + black color = iota |
| 20 | + red |
| 21 | + green |
| 22 | + yellow |
| 23 | + blue |
| 24 | + magenta |
| 25 | + cyan |
| 26 | + white |
| 27 | + brightblack |
| 28 | + brightred |
| 29 | + brightgreen |
| 30 | + brightyellow |
| 31 | + brightblue |
| 32 | + brightmagenta |
| 33 | + brightcyan |
| 34 | + brightwhite |
| 35 | + ) |
| 36 | + |
| 37 | + // AnsiColor are ANSI color codes for supported terminal colors. |
| 38 | + var ansiColor = map[color]string{ |
| 39 | + black: "\u001b[30m", |
| 40 | + red: "\u001b[31m", |
| 41 | + green: "\u001b[32m", |
| 42 | + yellow: "\u001b[33m", |
| 43 | + blue: "\u001b[34m", |
| 44 | + magenta: "\u001b[35m", |
| 45 | + cyan: "\u001b[36m", |
| 46 | + white: "\u001b[37m", |
| 47 | + brightblack: "\u001b[30;1m", |
| 48 | + brightred: "\u001b[31;1m", |
| 49 | + brightgreen: "\u001b[32;1m", |
| 50 | + brightyellow: "\u001b[33;1m", |
| 51 | + brightblue: "\u001b[34;1m", |
| 52 | + brightmagenta: "\u001b[35;1m", |
| 53 | + brightcyan: "\u001b[36;1m", |
| 54 | + brightwhite: "\u001b[37;1m", |
| 55 | + } |
| 56 | + |
| 57 | + const colorName = "blackredgreenyellowbluemagentacyanwhitebrightblackbrightredbrightgreenbrightyellowbrightbluebrightmagentabrightcyanbrightwhite" |
| 58 | + |
| 59 | + var colorIndex = [...]uint8{0, 5, 8, 13, 19, 23, 30, 34, 39, 50, 59, 70, 82, 92, 105, 115, 126} |
| 60 | + |
| 61 | + colorToLowerString := func(i color) string { |
| 62 | + if i < 0 || i >= color(len(colorIndex)-1) { |
| 63 | + return "color(" + _strconv.FormatInt(int64(i), 10) + ")" |
| 64 | + } |
| 65 | + return colorName[colorIndex[i]:colorIndex[i+1]] |
| 66 | + } |
| 67 | + |
| 68 | + // ansiColorReset is an ANSI color code to reset the terminal color. |
| 69 | + const ansiColorReset = "\033[0m" |
| 70 | + |
| 71 | + // defaultTargetAnsiColor is a default ANSI color for colorizing targets. |
| 72 | + // It is set to Cyan as an arbitrary color, because it has a neutral meaning |
| 73 | + var defaultTargetAnsiColor = ansiColor[cyan] |
| 74 | + |
| 75 | + getAnsiColor := func(color string) (string, bool) { |
| 76 | + colorLower := _strings.ToLower(color) |
| 77 | + for k, v := range ansiColor { |
| 78 | + colorConstLower := colorToLowerString(k) |
| 79 | + if colorConstLower == colorLower { |
| 80 | + return v, true |
| 81 | + } |
| 82 | + } |
| 83 | + return "", false |
| 84 | + } |
| 85 | + |
| 86 | + // Terminals which don't support color: |
| 87 | + // |
| 88 | + // TERM=vt100 |
| 89 | + // TERM=cygwin |
| 90 | + // TERM=xterm-mono |
| 91 | + var noColorTerms = map[string]bool{ |
| 92 | + "vt100": false, |
| 93 | + "cygwin": false, |
| 94 | + "xterm-mono": false, |
| 95 | + } |
| 96 | + |
| 97 | + // terminalSupportsColor checks if the current console supports color output |
| 98 | + // |
| 99 | + // Supported: |
| 100 | + // |
| 101 | + // linux, mac, or windows's ConEmu, Cmder, putty, git-bash.exe, pwsh.exe |
| 102 | + // |
| 103 | + // Not supported: |
| 104 | + // |
| 105 | + // windows cmd.exe, powerShell.exe |
| 106 | + terminalSupportsColor := func() bool { |
| 107 | + envTerm := _os.Getenv("TERM") |
| 108 | + if _, ok := noColorTerms[envTerm]; ok { |
| 109 | + return false |
| 110 | + } |
| 111 | + return true |
| 112 | + } |
| 113 | + |
| 114 | + // enableColor reports whether the user has requested to enable a color output. |
| 115 | + enableColor := func() bool { |
| 116 | + b, _ := _strconv.ParseBool(_os.Getenv("MAGEFILE_ENABLE_COLOR")) |
| 117 | + return b |
| 118 | + } |
| 119 | + |
| 120 | + // targetColor returns the ANSI color which should be used to colorize targets. |
| 121 | + targetColor := func() string { |
| 122 | + s, exists := _os.LookupEnv("MAGEFILE_TARGET_COLOR") |
| 123 | + if exists { |
| 124 | + if c, ok := getAnsiColor(s); ok { |
| 125 | + return c |
| 126 | + } |
| 127 | + } |
| 128 | + return defaultTargetAnsiColor |
| 129 | + } |
| 130 | + |
| 131 | + // store the color terminal variables, so that the detection isn't repeated for each target |
| 132 | + var enableColorValue = enableColor() && terminalSupportsColor() |
| 133 | + var targetColorValue = targetColor() |
| 134 | + |
| 135 | + if enableColorValue { |
| 136 | + return _fmt.Sprintf("%s%s%s", targetColorValue, str, ansiColorReset) |
| 137 | + } |
| 138 | + |
| 139 | + return str |
| 140 | +} |
0 commit comments