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