File tree Expand file tree Collapse file tree 1 file changed +25
-4
lines changed Expand file tree Collapse file tree 1 file changed +25
-4
lines changed Original file line number Diff line number Diff line change 12
12
//
13
13
//===----------------------------------------------------------------------===//
14
14
15
+ import Foundation
16
+
17
+ private var isColorSupported : Bool {
18
+ let env = ProcessInfo . processInfo. environment
19
+ if env [ " NO_COLOR " ] != nil {
20
+ return false
21
+ }
22
+ if let term = env [ " TERM " ] , term. contains ( " color " ) || env [ " COLORTERM " ] != nil {
23
+ return true
24
+ }
25
+ return false
26
+ }
27
+
15
28
package enum Rainbow : String {
16
29
case black = " \u{001B} [0;30m "
17
30
case red = " \u{001B} [0;31m "
@@ -139,13 +152,17 @@ package extension String {
139
152
self
140
153
}
141
154
}
142
-
155
+
143
156
var `default` : String {
144
157
self . colored ( as: . default)
145
158
}
146
159
147
160
func colored( as color: Rainbow ) -> String {
148
- " \( color. rawValue) \( self ) \( Rainbow . default. rawValue) "
161
+ return if isColorSupported {
162
+ " \( color. rawValue) \( self ) \( Rainbow . default. rawValue) "
163
+ } else {
164
+ self
165
+ }
149
166
}
150
167
}
151
168
@@ -185,12 +202,16 @@ package extension Substring {
185
202
var bold : String {
186
203
self . colored ( as: . bold)
187
204
}
188
-
205
+
189
206
var `default` : String {
190
207
self . colored ( as: . default)
191
208
}
192
209
193
210
func colored( as color: Rainbow ) -> String {
194
- " \( color. rawValue) \( self ) \( Rainbow . default. rawValue) "
211
+ return if isColorSupported {
212
+ " \( color. rawValue) \( self ) \( Rainbow . default. rawValue) "
213
+ } else {
214
+ String ( self )
215
+ }
195
216
}
196
217
}
You can’t perform that action at this time.
0 commit comments