0001 // 0002 // Rainbow.swift 0003 // Rainbow 0004 // 0005 // Created by Wei Wang on 15/12/22. 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 import Foundation 0028 0029 /** 0030 A Mode code represnet a component for colorizing the string. 0031 It could be a `Color`, a `BackgroundColor` or a `Style` 0032 */ 0033 public protocol ModeCode{ 0034 var value
BackgroundColor.swift:28 public enum BackgroundColor: UInt8, ModeCode {Color.swift:28 public enum Color: UInt8, ModeCode {String+Rainbow.swift:155 public func stringByApplying(codes: ModeCode...) -> String {Style.swift:28 public enum Style: UInt8, ModeCode {: UInt8 { get } 0035 } 0036 0037 /** 0038 Setting for `Rainbow`. 0039 */ 0040 public struct Rainbow
String+Rainbow.swift:162 let input = ConsoleCodesParser().parseModeCodes( codes.map{ $0.value } ){ 0041 0042 /// Output target for `Rainbow`. `Rainbow` should detect correct target itself, so you rarely need to set it. 0043 /// However, if you want the colorized string to be different, or the detection is not correct, you can set it manually. 0044 public static var outputTarget
String+Rainbow.swift:49 guard let _ = Rainbow.extractModesForString(self).color else {String+Rainbow.swift:75 guard let _ = Rainbow.extractModesForString(self).backgroundColor else {String+Rainbow.swift:102 guard Rainbow.enabled else {String+Rainbow.swift:106 let current = Rainbow.extractModesForString(self)String+Rainbow.swift:114 return Rainbow.generateStringForColor(String+Rainbow.swift:135 guard Rainbow.enabled else {String+Rainbow.swift:139 let current = Rainbow.extractModesForString(self)String+Rainbow.swift:140 return Rainbow.generateStringForColor(String+Rainbow.swift:157 guard Rainbow.enabled else {String+Rainbow.swift:161 let current = Rainbow.extractModesForString(self)String+Rainbow.swift:179 return Rainbow.generateStringForColor(= OutputTarget.currentOutputTarget 0045 0046 /// Enable `Rainbow` to colorize string or not. Default is `true`. 0047 public static var enabled
Rainbow.swift:74 switch outputTarget {= true 0048 0049 static func extractModesForString
Rainbow.swift:70 guard enabled else {String+Rainbow.swift:102 guard Rainbow.enabled else {String+Rainbow.swift:135 guard Rainbow.enabled else {String+Rainbow.swift:157 guard Rainbow.enabled else {(string: String) 0050 -> (color: Color?, backgroundColor: BackgroundColor?, styles: [Style]?, text: String) 0051 { 0052 if string.isConsoleStyle { 0053 let result = ConsoleModesExtractor().extractModeCodes(string) 0054 let (color, backgroundColor, styles) = ConsoleCodesParser().parseModeCodes(result.codes) 0055 return (color, backgroundColor, styles, result.text) 0056 } else if string.isXcodeColorsStyle { 0057 let result = XcodeColorsModesExtractor().extractModeCodes(string) 0058 let (color, backgroundColor, _) = XcodeColorsCodesParser().parseModeCodes(result.codes) 0059 return (color, backgroundColor, nil, result.text) 0060 } else { 0061 return (nil, nil, nil, string) 0062 } 0063 } 0064 0065 static func generateStringForColor
String+Rainbow.swift:49 guard let _ = Rainbow.extractModesForString(self).color else {String+Rainbow.swift:75 guard let _ = Rainbow.extractModesForString(self).backgroundColor else {String+Rainbow.swift:106 let current = Rainbow.extractModesForString(self)String+Rainbow.swift:139 let current = Rainbow.extractModesForString(self)String+Rainbow.swift:161 let current = Rainbow.extractModesForString(self)(color: Color?, 0066 backgroundColor: BackgroundColor?, 0067 styles: [Style]?, 0068 text: String) -> String 0069 { 0070 guard enabled else { 0071 return text 0072 } 0073 0074 switch outputTarget { 0075 case .XcodeColors: 0076 return XcodeColorsStringGenerator().generateStringColor(color, backgroundColor: backgroundColor, styles: styles, text: text) 0077 case .Console: 0078 return ConsoleStringGenerator().generateStringColor(color, backgroundColor: backgroundColor, styles: styles, text: text) 0079 case .Unknown: 0080 return text 0081 } 0082 } 0083 0084 } 0085 0086 private extension String { 0087 var isConsoleStyle
String+Rainbow.swift:114 return Rainbow.generateStringForColor(String+Rainbow.swift:140 return Rainbow.generateStringForColor(String+Rainbow.swift:179 return Rainbow.generateStringForColor(: Bool { 0088 let token = ControlCode.CSI 0089 return hasPrefix(token) && hasSuffix("\(token)0m") 0090 } 0091 0092 var isXcodeColorsStyle
Rainbow.swift:52 if string.isConsoleStyle {: Bool { 0093 let token = ControlCode.CSI 0094 return hasPrefix(token) && hasSuffix("\(token);") 0095 } 0096 }
Rainbow.swift:56 } else if string.isXcodeColorsStyle {