0001 // 0002 // String+Rainbow.swift 0003 // Rainbow 0004 // 0005 // Created by Wei Wang on 15/12/23. 0006 // 0007 // Copyright (c) 2015 Wei Wang <onevcat@gmail.com> 0008 // 0009 // Permission is hereby granted, free of charge, to any person obtaining a copy 0010 // of this software and associated documentation files (the "Software"), to deal 0011 // in the Software without restriction, including without limitation the rights 0012 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 0013 // copies of the Software, and to permit persons to whom the Software is 0014 // furnished to do so, subject to the following conditions: 0015 // 0016 // The above copyright notice and this permission notice shall be included in 0017 // all copies or substantial portions of the Software. 0018 // 0019 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0020 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0021 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0022 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0023 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0024 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 0025 // THE SOFTWARE. 0026 0027 // MARK: - Worker methods 0028 extension String { 0029 /** 0030 Apply a text color to current string. 0031 0032 - parameter color: The color to apply. 0033 0034 - returns: The colorized string based on current content. 0035 */ 0036 public func stringByApplyingColor(color: Color) -> String { 0037 return stringByApplying(color) 0038 } 0039 0040 /** 0041 Remove a color from current string. 0042 0043 - Note: This method will return the string itself if there is no color component in it. 0044 Otherwise, a string without color component will be returned and other components will remain untouched.. 0045 0046 - returns: A string without color. 0047 */ 0048 public func stringByRemovingColor
String+Rainbow.swift:52 return stringByApplyingColor(.Default)String+Rainbow.swift:192 public var black: String { return stringByApplyingColor(.Black) }String+Rainbow.swift:194 public var red: String { return stringByApplyingColor(.Red) }String+Rainbow.swift:196 public var green: String { return stringByApplyingColor(.Green) }String+Rainbow.swift:198 public var yellow: String { return stringByApplyingColor(.Yellow) }String+Rainbow.swift:200 public var blue: String { return stringByApplyingColor(.Blue) }String+Rainbow.swift:202 public var magenta: String { return stringByApplyingColor(.Magenta) }String+Rainbow.swift:204 public var cyan: String { return stringByApplyingColor(.Cyan) }String+Rainbow.swift:206 public var white: String { return stringByApplyingColor(.White) }String+Rainbow.swift:208 public var lightBlack: String { return stringByApplyingColor(.LightBlack) }String+Rainbow.swift:210 public var lightRed: String { return stringByApplyingColor(.LightRed) }String+Rainbow.swift:212 public var lightGreen: String { return stringByApplyingColor(.LightGreen) }String+Rainbow.swift:214 public var lightYellow: String { return stringByApplyingColor(.LightYellow) }String+Rainbow.swift:216 public var lightBlue: String { return stringByApplyingColor(.LightBlue) }String+Rainbow.swift:218 public var lightMagenta: String { return stringByApplyingColor(.LightMagenta) }String+Rainbow.swift:220 public var lightCyan: String { return stringByApplyingColor(.LightCyan) }String+Rainbow.swift:222 public var lightWhite: String { return stringByApplyingColor(.LightWhite) }() -> String { 0049 guard let _ = Rainbow.extractModesForString(self).color else { 0050 return self 0051 } 0052 return stringByApplyingColor(.Default) 0053 } 0054 0055 /** 0056 Apply a background color to current string. 0057 0058 - parameter color: The background color to apply. 0059 0060 - returns: The background colorized string based on current content. 0061 */ 0062 public func stringByApplyingBackgroundColor
String+Rainbow.swift:264 public var clearColor: String { return stringByRemovingColor() }(color: BackgroundColor) -> String { 0063 return stringByApplying(color) 0064 } 0065 0066 /** 0067 Remove a background color from current string. 0068 0069 - Note: This method will return the string itself if there is no background color component in it. 0070 Otherwise, a string without background color component will be returned and other components will remain untouched. 0071 0072 - returns: A string without color. 0073 */ 0074 public func stringByRemovingBackgroundColor
String+Rainbow.swift:79 return stringByApplyingBackgroundColor(.Default)String+Rainbow.swift:228 public var onBlack: String { return stringByApplyingBackgroundColor(.Black) }String+Rainbow.swift:230 public var onRed: String { return stringByApplyingBackgroundColor(.Red) }String+Rainbow.swift:232 public var onGreen: String { return stringByApplyingBackgroundColor(.Green) }String+Rainbow.swift:234 public var onYellow: String { return stringByApplyingBackgroundColor(.Yellow) }String+Rainbow.swift:236 public var onBlue: String { return stringByApplyingBackgroundColor(.Blue) }String+Rainbow.swift:238 public var onMagenta: String { return stringByApplyingBackgroundColor(.Magenta) }String+Rainbow.swift:240 public var onCyan: String { return stringByApplyingBackgroundColor(.Cyan) }String+Rainbow.swift:242 public var onWhite: String { return stringByApplyingBackgroundColor(.White) }() -> String { 0075 guard let _ = Rainbow.extractModesForString(self).backgroundColor else { 0076 return self 0077 } 0078 0079 return stringByApplyingBackgroundColor(.Default) 0080 } 0081 0082 /** 0083 Apply a style to current string. 0084 0085 - parameter style: The style to apply. 0086 0087 - returns: A string with specified style applied. 0088 */ 0089 public func stringByApplyingStyle
String+Rainbow.swift:266 public var clearBackgroundColor: String { return stringByRemovingBackgroundColor() }(style: Style) -> String { 0090 return stringByApplying(style) 0091 } 0092 0093 /** 0094 Remove a style from current string. 0095 0096 - parameter style: The style to remove. 0097 0098 - returns: A string with specified style removed. 0099 */ 0100 public func stringByRemovingStyle(style: Style) -> String { 0101 0102 guard Rainbow.enabled else { 0103 return self 0104 } 0105 0106 let current = Rainbow.extractModesForString(self) 0107 if let styles = current.styles { 0108 var s = styles 0109 var index = s.indexOf(style) 0110 while index != nil { 0111 s.removeAtIndex(index!) 0112 index = s.indexOf(style) 0113 } 0114 return Rainbow.generateStringForColor( 0115 current.color, 0116 backgroundColor: current.backgroundColor, 0117 styles: s, 0118 text: current.text 0119 ) 0120 } else { 0121 return self 0122 } 0123 } 0124 0125 /** 0126 Remove all styles from current string. 0127 0128 - Note: This method will return the string itself if there is no style components in it. 0129 Otherwise, a string without stlye components will be returned and other color components will remain untouched. 0130 0131 - returns: A string without style components. 0132 */ 0133 public func stringByRemovingAllStyles
String+Rainbow.swift:248 public var bold: String { return stringByApplyingStyle(.Bold) }String+Rainbow.swift:250 public var dim: String { return stringByApplyingStyle(.Dim) }String+Rainbow.swift:252 public var italic: String { return stringByApplyingStyle(.Italic) }String+Rainbow.swift:254 public var underline: String { return stringByApplyingStyle(.Underline) }String+Rainbow.swift:256 public var blink: String { return stringByApplyingStyle(.Blink) }String+Rainbow.swift:258 public var swap: String { return stringByApplyingStyle(.Swap) }() -> String { 0134 0135 guard Rainbow.enabled else { 0136 return self 0137 } 0138 0139 let current = Rainbow.extractModesForString(self) 0140 return Rainbow.generateStringForColor( 0141 current.color, 0142 backgroundColor: current.backgroundColor, 0143 styles: nil, 0144 text: current.text 0145 ) 0146 } 0147 0148 /** 0149 Apply a series of modes to the string. 0150 0151 - parameter codes: Component mode code to apply to the string. 0152 0153 - returns: A string with specified modes applied. 0154 */ 0155 public func stringByApplying
String+Rainbow.swift:268 public var clearStyles: String { return stringByRemovingAllStyles() }(codes: ModeCode...) -> String { 0156 0157 guard Rainbow.enabled else { 0158 return self 0159 } 0160 0161 let current = Rainbow.extractModesForString(self) 0162 let input = ConsoleCodesParser().parseModeCodes( codes.map{ $0.value } ) 0163 0164 let color = input.color ?? current.color 0165 let backgroundColor = input.backgroundColor ?? current.backgroundColor 0166 var styles = [Style]() 0167 0168 if let s = current.styles { 0169 styles += s 0170 } 0171 0172 if let s = input.styles { 0173 styles += s 0174 } 0175 0176 if codes.isEmpty { 0177 return self 0178 } else { 0179 return Rainbow.generateStringForColor( 0180 color, 0181 backgroundColor: backgroundColor, 0182 styles: styles.isEmpty ? nil : styles, 0183 text: current.text 0184 ) 0185 } 0186 } 0187 } 0188 0189 // MARK: - Colors Shorthand 0190 extension String { 0191 /// String with black text. 0192 public var black: String { return stringByApplyingColor(.Black) } 0193 /// String with red text. 0194 public var red: String { return stringByApplyingColor(.Red) } 0195 /// String with green text. 0196 public var green: String { return stringByApplyingColor(.Green) } 0197 /// String with yellow text. 0198 public var yellow: String { return stringByApplyingColor(.Yellow) } 0199 /// String with blue text. 0200 public var blue: String { return stringByApplyingColor(.Blue) } 0201 /// String with magenta text. 0202 public var magenta: String { return stringByApplyingColor(.Magenta) } 0203 /// String with cyan text. 0204 public var cyan: String { return stringByApplyingColor(.Cyan) } 0205 /// String with white text. 0206 public var white: String { return stringByApplyingColor(.White) } 0207 /// String with light black text. Generally speaking, it means dark grey in some consoles. 0208 public var lightBlack: String { return stringByApplyingColor(.LightBlack) } 0209 /// String with light red text. 0210 public var lightRed: String { return stringByApplyingColor(.LightRed) } 0211 /// String with light green text. 0212 public var lightGreen: String { return stringByApplyingColor(.LightGreen) } 0213 /// String with light yellow text. 0214 public var lightYellow: String { return stringByApplyingColor(.LightYellow) } 0215 /// String with light blue text. 0216 public var lightBlue: String { return stringByApplyingColor(.LightBlue) } 0217 /// String with light magenta text. 0218 public var lightMagenta: String { return stringByApplyingColor(.LightMagenta) } 0219 /// String with light cyan text. 0220 public var lightCyan: String { return stringByApplyingColor(.LightCyan) } 0221 /// String with light white text. Generally speaking, it means light grey in some consoles. 0222 public var lightWhite: String { return stringByApplyingColor(.LightWhite) } 0223 } 0224 0225 // MARK: - Background Colors Shorthand 0226 extension String { 0227 /// String with black background. 0228 public var onBlack: String { return stringByApplyingBackgroundColor(.Black) } 0229 /// String with red background. 0230 public var onRed: String { return stringByApplyingBackgroundColor(.Red) } 0231 /// String with green background. 0232 public var onGreen: String { return stringByApplyingBackgroundColor(.Green) } 0233 /// String with yellow background. 0234 public var onYellow: String { return stringByApplyingBackgroundColor(.Yellow) } 0235 /// String with blue background. 0236 public var onBlue: String { return stringByApplyingBackgroundColor(.Blue) } 0237 /// String with magenta background. 0238 public var onMagenta: String { return stringByApplyingBackgroundColor(.Magenta) } 0239 /// String with cyan background. 0240 public var onCyan: String { return stringByApplyingBackgroundColor(.Cyan) } 0241 /// String with white background. 0242 public var onWhite: String { return stringByApplyingBackgroundColor(.White) } 0243 } 0244 0245 // MARK: - Styles Shorthand 0246 extension String { 0247 /// String with bold style. 0248 public var bold: String { return stringByApplyingStyle(.Bold) } 0249 /// String with dim style. This is not widely supported in all terminals. Use it carefully. 0250 public var dim: String { return stringByApplyingStyle(.Dim) } 0251 /// String with italic style. This depends on whether a italic existing for the font family of terminals. 0252 public var italic: String { return stringByApplyingStyle(.Italic) } 0253 /// String with underline style. 0254 public var underline: String { return stringByApplyingStyle(.Underline) } 0255 /// String with blink style. This is not widely supported in all terminals, or need additional setting. Use it carefully. 0256 public var blink: String { return stringByApplyingStyle(.Blink) } 0257 /// String with text color and background color swapped. 0258 public var swap: String { return stringByApplyingStyle(.Swap) } 0259 } 0260 0261 // MARK: - Clear Modes Shorthand 0262 extension String { 0263 /// Clear color component from string. 0264 public var clearColor: String { return stringByRemovingColor() } 0265 /// Clear background color component from string. 0266 public var clearBackgroundColor: String { return stringByRemovingBackgroundColor() } 0267 /// Clear styles components from string. 0268 public var clearStyles: String { return stringByRemovingAllStyles() } 0269 } 0270 0271
String+Rainbow.swift:37 return stringByApplying(color)String+Rainbow.swift:63 return stringByApplying(color)String+Rainbow.swift:90 return stringByApplying(style)