David K. Bainbridge | 215e024 | 2017-09-05 23:18:24 -0700 | [diff] [blame] | 1 | // Copyright 2016 Claudemiro Alves Feitosa Neto. All rights reserved. |
| 2 | // Use of this source code is governed by a MIT-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package banner |
| 6 | |
| 7 | import ( |
| 8 | "os" |
| 9 | |
| 10 | "github.com/mattn/go-isatty" |
| 11 | ) |
| 12 | |
| 13 | const ( |
| 14 | encodeStart = "\033[" |
| 15 | encodeEnd = "m" |
| 16 | encodeReset = "0;39" |
| 17 | ) |
| 18 | |
| 19 | type ansiBackground struct { |
| 20 | isColorEnabled bool |
| 21 | } |
| 22 | |
| 23 | func (a ansiBackground) Default() string { |
| 24 | return outputANSI(a.isColorEnabled, "49") |
| 25 | } |
| 26 | |
| 27 | func (a ansiBackground) Black() string { |
| 28 | return outputANSI(a.isColorEnabled, "40") |
| 29 | } |
| 30 | |
| 31 | func (a ansiBackground) Red() string { |
| 32 | return outputANSI(a.isColorEnabled, "41") |
| 33 | } |
| 34 | |
| 35 | func (a ansiBackground) Green() string { |
| 36 | return outputANSI(a.isColorEnabled, "42") |
| 37 | } |
| 38 | |
| 39 | func (a ansiBackground) Yellow() string { |
| 40 | return outputANSI(a.isColorEnabled, "43") |
| 41 | } |
| 42 | |
| 43 | func (a ansiBackground) Blue() string { |
| 44 | return outputANSI(a.isColorEnabled, "44") |
| 45 | } |
| 46 | |
| 47 | func (a ansiBackground) Magenta() string { |
| 48 | return outputANSI(a.isColorEnabled, "45") |
| 49 | } |
| 50 | |
| 51 | func (a ansiBackground) Cyan() string { |
| 52 | return outputANSI(a.isColorEnabled, "46") |
| 53 | } |
| 54 | |
| 55 | func (a ansiBackground) White() string { |
| 56 | return outputANSI(a.isColorEnabled, "47") |
| 57 | } |
| 58 | |
| 59 | func (a ansiBackground) BrightBlack() string { |
| 60 | return outputANSI(a.isColorEnabled, "100") |
| 61 | } |
| 62 | |
| 63 | func (a ansiBackground) BrightRed() string { |
| 64 | return outputANSI(a.isColorEnabled, "101") |
| 65 | } |
| 66 | |
| 67 | func (a ansiBackground) BrightGreen() string { |
| 68 | return outputANSI(a.isColorEnabled, "102") |
| 69 | } |
| 70 | |
| 71 | func (a ansiBackground) BrightYellow() string { |
| 72 | return outputANSI(a.isColorEnabled, "103") |
| 73 | } |
| 74 | |
| 75 | func (a ansiBackground) BrightBlue() string { |
| 76 | return outputANSI(a.isColorEnabled, "104") |
| 77 | } |
| 78 | |
| 79 | func (a ansiBackground) BrightMagenta() string { |
| 80 | return outputANSI(a.isColorEnabled, "105") |
| 81 | } |
| 82 | |
| 83 | func (a ansiBackground) BrightCyan() string { |
| 84 | return outputANSI(a.isColorEnabled, "106") |
| 85 | } |
| 86 | |
| 87 | func (a ansiBackground) BrightWhite() string { |
| 88 | return outputANSI(a.isColorEnabled, "107") |
| 89 | } |
| 90 | |
| 91 | type ansiColor struct { |
| 92 | isColorEnabled bool |
| 93 | } |
| 94 | |
| 95 | func (a ansiColor) Default() string { |
| 96 | return outputANSI(a.isColorEnabled, "39") |
| 97 | } |
| 98 | |
| 99 | func (a ansiColor) Black() string { |
| 100 | return outputANSI(a.isColorEnabled, "30") |
| 101 | } |
| 102 | |
| 103 | func (a ansiColor) Red() string { |
| 104 | return outputANSI(a.isColorEnabled, "31") |
| 105 | } |
| 106 | |
| 107 | func (a ansiColor) Green() string { |
| 108 | return outputANSI(a.isColorEnabled, "32") |
| 109 | } |
| 110 | |
| 111 | func (a ansiColor) Yellow() string { |
| 112 | return outputANSI(a.isColorEnabled, "33") |
| 113 | } |
| 114 | |
| 115 | func (a ansiColor) Blue() string { |
| 116 | return outputANSI(a.isColorEnabled, "34") |
| 117 | } |
| 118 | |
| 119 | func (a ansiColor) Magenta() string { |
| 120 | return outputANSI(a.isColorEnabled, "35") |
| 121 | } |
| 122 | |
| 123 | func (a ansiColor) Cyan() string { |
| 124 | return outputANSI(a.isColorEnabled, "36") |
| 125 | } |
| 126 | |
| 127 | func (a ansiColor) White() string { |
| 128 | return outputANSI(a.isColorEnabled, "37") |
| 129 | } |
| 130 | |
| 131 | func (a ansiColor) BrightBlack() string { |
| 132 | return outputANSI(a.isColorEnabled, "90") |
| 133 | } |
| 134 | |
| 135 | func (a ansiColor) BrightRed() string { |
| 136 | return outputANSI(a.isColorEnabled, "91") |
| 137 | } |
| 138 | |
| 139 | func (a ansiColor) BrightGreen() string { |
| 140 | return outputANSI(a.isColorEnabled, "92") |
| 141 | } |
| 142 | |
| 143 | func (a ansiColor) BrightYellow() string { |
| 144 | return outputANSI(a.isColorEnabled, "93") |
| 145 | } |
| 146 | |
| 147 | func (a ansiColor) BrightBlue() string { |
| 148 | return outputANSI(a.isColorEnabled, "94") |
| 149 | } |
| 150 | |
| 151 | func (a ansiColor) BrightMagenta() string { |
| 152 | return outputANSI(a.isColorEnabled, "95") |
| 153 | } |
| 154 | |
| 155 | func (a ansiColor) BrightCyan() string { |
| 156 | return outputANSI(a.isColorEnabled, "96") |
| 157 | } |
| 158 | |
| 159 | func (a ansiColor) BrightWhite() string { |
| 160 | return outputANSI(a.isColorEnabled, "97") |
| 161 | } |
| 162 | |
| 163 | func (a ansiBackground) Reset() string { |
| 164 | return outputANSI(a.isColorEnabled, encodeReset) |
| 165 | } |
| 166 | |
| 167 | func outputANSI(isColorEnabled bool, code string) string { |
| 168 | if isColorEnabled && isANSIEnabled() { |
| 169 | return encodeStart + code + encodeEnd |
| 170 | } |
| 171 | |
| 172 | return "" |
| 173 | |
| 174 | } |
| 175 | |
| 176 | func isANSIEnabled() bool { |
| 177 | return isatty.IsTerminal(os.Stdout.Fd()) |
| 178 | } |