0001 // 0002 // StringStyle.swift 0003 // StringStyle 0004 // 0005 // Created by Omar Abdelhafith on 31/10/2015. 0006 // Copyright © 2015 Omar Abdelhafith. All rights reserved. 0007 // 0008 0009 import Foundation 0010 0011 0012 let startOfCode= "\u{001B}[" 0013 let endOfCode
StringStyle.swift:49 return "\(startOfCode)\(color)\(endOfCode)"StringStyle.swift:53 return string.containsString(startOfCode)StringStyle.swift:59 let newString = string.stringByReplacingOccurrencesOfString(startOfCode, withString: ";", options: .LiteralSearch, range: rangeWithInset)= "m" 0014 let codesSeperators = ";" 0015 0016 0017 protocol StringStyle
StringStyle.swift:49 return "\(startOfCode)\(color)\(endOfCode)"{ 0018 var rawValue
Colorizer.swift:48 let color: StringStyleColorizer.swift:51 init(string: String, color: StringStyle) {StringStyle.swift:23 extension StringStyle {StringStyle.swift:76 enum ForegroundColor: Int, StringStyle {StringStyle.swift:89 enum BackgroundColor: Int, StringStyle {StringStyle.swift:102 enum StringTextStyle: Int, StringStyle {: Int { get } 0019 func colorize
StringStyle.swift:35 return "\(preparedColorCode(self.rawValue))\(string)\(endingColorCode())"StringStyle.swift:41 let sringwWithStart = "\(preparedColorCode(self.rawValue))\(stringByRemovingEnding)"(string string: String) -> String 0020 } 0021 0022 0023 extension StringStyle { 0024 0025 func colorize(string string: String) -> String { 0026 0027 if hasAnyStyleCode(string) { 0028 return colorizeStringAndAddCodeSeperators(string) 0029 } else { 0030 return colorizeStringWithoutPriorCode(string) 0031 } 0032 } 0033 0034 private func colorizeStringWithoutPriorCode
Colorizer.swift:57 return color.colorize(string: string)(string: String) -> String { 0035 return "\(preparedColorCode(self.rawValue))\(string)\(endingColorCode())" 0036 } 0037 0038 private func colorizeStringAndAddCodeSeperators
StringStyle.swift:30 return colorizeStringWithoutPriorCode(string)(string: String) -> String { 0039 //To refactor and use regex matching instead of replacing strings and using tricks 0040 let stringByRemovingEnding = removeEndingCode(string) 0041 let sringwWithStart = "\(preparedColorCode(self.rawValue))\(stringByRemovingEnding)" 0042 0043 let stringByAddingCodeSeperator = addCommandSeperators(sringwWithStart) 0044 0045 return "\(stringByAddingCodeSeperator)\(endingColorCode())" 0046 } 0047 0048 private func preparedColorCode
StringStyle.swift:28 return colorizeStringAndAddCodeSeperators(string)(color: Int) -> String { 0049 return "\(startOfCode)\(color)\(endOfCode)" 0050 } 0051 0052 private func hasAnyStyleCode
StringStyle.swift:35 return "\(preparedColorCode(self.rawValue))\(string)\(endingColorCode())"StringStyle.swift:41 let sringwWithStart = "\(preparedColorCode(self.rawValue))\(stringByRemovingEnding)"StringStyle.swift:71 return preparedColorCode(0)(string: String) -> Bool { 0053 return string.containsString(startOfCode) 0054 } 0055 0056 private func addCommandSeperators
StringStyle.swift:27 if hasAnyStyleCode(string) {(string: String) -> String { 0057 0058 var rangeWithInset = Range(start: string.startIndex.successor(), end: string.endIndex.predecessor()) 0059 let newString = string.stringByReplacingOccurrencesOfString(startOfCode, withString: ";", options: .LiteralSearch, range: rangeWithInset) 0060 0061 rangeWithInset = Range(start: newString.startIndex.successor(), end: newString.endIndex.predecessor()) 0062 return newString.stringByReplacingOccurrencesOfString("m;", withString: ";", options: .LiteralSearch, range: rangeWithInset) 0063 } 0064 0065 private func removeEndingCode
StringStyle.swift:43 let stringByAddingCodeSeperator = addCommandSeperators(sringwWithStart)(string: String) -> String { 0066 let rangeWithInset = Range(start: string.startIndex.successor(), end: string.endIndex) 0067 return string.stringByReplacingOccurrencesOfString(endingColorCode(), withString: "", options: .LiteralSearch, range: rangeWithInset) 0068 } 0069 0070 private func endingColorCode
StringStyle.swift:40 let stringByRemovingEnding = removeEndingCode(string)() -> String { 0071 return preparedColorCode(0) 0072 } 0073 } 0074 0075 0076 enum ForegroundColor
StringStyle.swift:35 return "\(preparedColorCode(self.rawValue))\(string)\(endingColorCode())"StringStyle.swift:45 return "\(stringByAddingCodeSeperator)\(endingColorCode())"StringStyle.swift:67 return string.stringByReplacingOccurrencesOfString(endingColorCode(), withString: "", options: .LiteralSearch, range: rangeWithInset): Int, StringStyle { 0077 0078 case Black
StringForegroundColorizer.swift:17 return Colorizer(string: string, color: ForegroundColor.Black).descriptionStringForegroundColorizer.swift:21 return Colorizer(string: string, color: ForegroundColor.Red).descriptionStringForegroundColorizer.swift:25 return Colorizer(string: string, color: ForegroundColor.Green).descriptionStringForegroundColorizer.swift:29 return Colorizer(string: string, color: ForegroundColor.Yellow).descriptionStringForegroundColorizer.swift:33 return Colorizer(string: string, color: ForegroundColor.Blue).descriptionStringForegroundColorizer.swift:37 return Colorizer(string: string, color: ForegroundColor.Magenta).descriptionStringForegroundColorizer.swift:41 return Colorizer(string: string, color: ForegroundColor.Cyan).descriptionStringForegroundColorizer.swift:45 return Colorizer(string: string, color: ForegroundColor.White).description= 30 0079 case Red
StringForegroundColorizer.swift:17 return Colorizer(string: string, color: ForegroundColor.Black).description= 31 0080 case Green
StringForegroundColorizer.swift:21 return Colorizer(string: string, color: ForegroundColor.Red).description= 32 0081 case Yellow
StringForegroundColorizer.swift:25 return Colorizer(string: string, color: ForegroundColor.Green).description= 33 0082 case Blue
StringForegroundColorizer.swift:29 return Colorizer(string: string, color: ForegroundColor.Yellow).description= 34 0083 case Magenta
StringForegroundColorizer.swift:33 return Colorizer(string: string, color: ForegroundColor.Blue).description= 35 0084 case Cyan
StringForegroundColorizer.swift:37 return Colorizer(string: string, color: ForegroundColor.Magenta).description= 36 0085 case White
StringForegroundColorizer.swift:41 return Colorizer(string: string, color: ForegroundColor.Cyan).description= 37 0086 } 0087 0088 0089 enum BackgroundColor
StringForegroundColorizer.swift:45 return Colorizer(string: string, color: ForegroundColor.White).description: Int, StringStyle { 0090 0091 case Black
StringBackgroundColorizer.swift:17 return Colorizer(string: string, color: BackgroundColor.Black).descriptionStringBackgroundColorizer.swift:21 return Colorizer(string: string, color: BackgroundColor.Red).descriptionStringBackgroundColorizer.swift:25 return Colorizer(string: string, color: BackgroundColor.Green).descriptionStringBackgroundColorizer.swift:29 return Colorizer(string: string, color: BackgroundColor.Yellow).descriptionStringBackgroundColorizer.swift:33 return Colorizer(string: string, color: BackgroundColor.Blue).descriptionStringBackgroundColorizer.swift:37 return Colorizer(string: string, color: BackgroundColor.Magenta).descriptionStringBackgroundColorizer.swift:41 return Colorizer(string: string, color: BackgroundColor.Cyan).descriptionStringBackgroundColorizer.swift:45 return Colorizer(string: string, color: BackgroundColor.White).description= 40 0092 case Red
StringBackgroundColorizer.swift:17 return Colorizer(string: string, color: BackgroundColor.Black).description= 41 0093 case Green
StringBackgroundColorizer.swift:21 return Colorizer(string: string, color: BackgroundColor.Red).description= 42 0094 case Yellow
StringBackgroundColorizer.swift:25 return Colorizer(string: string, color: BackgroundColor.Green).description= 43 0095 case Blue
StringBackgroundColorizer.swift:29 return Colorizer(string: string, color: BackgroundColor.Yellow).description= 44 0096 case Magenta
StringBackgroundColorizer.swift:33 return Colorizer(string: string, color: BackgroundColor.Blue).description= 45 0097 case Cyan
StringBackgroundColorizer.swift:37 return Colorizer(string: string, color: BackgroundColor.Magenta).description= 46 0098 case White
StringBackgroundColorizer.swift:41 return Colorizer(string: string, color: BackgroundColor.Cyan).description= 47 0099 } 0100 0101 0102 enum StringTextStyle
StringBackgroundColorizer.swift:45 return Colorizer(string: string, color: BackgroundColor.White).description: Int, StringStyle { 0103 0104 case Reset
StringStyleColorizer.swift:17 return Colorizer(string: string, color: StringTextStyle.Reset).descriptionStringStyleColorizer.swift:21 return Colorizer(string: string, color: StringTextStyle.Bold).descriptionStringStyleColorizer.swift:25 return Colorizer(string: string, color: StringTextStyle.Italic).descriptionStringStyleColorizer.swift:29 return Colorizer(string: string, color: StringTextStyle.Underline).descriptionStringStyleColorizer.swift:33 return Colorizer(string: string, color: StringTextStyle.Inverse).descriptionStringStyleColorizer.swift:37 return Colorizer(string: string, color: StringTextStyle.Strikethrough).descriptionStringStyleColorizer.swift:41 return Colorizer(string: string, color: StringTextStyle.BoldOff).descriptionStringStyleColorizer.swift:45 return Colorizer(string: string, color: StringTextStyle.ItalicOff).descriptionStringStyleColorizer.swift:49 return Colorizer(string: string, color: StringTextStyle.UnderlineOff).descriptionStringStyleColorizer.swift:53 return Colorizer(string: string, color: StringTextStyle.InverseOff).descriptionStringStyleColorizer.swift:57 return Colorizer(string: string, color: StringTextStyle.StrikethroughOff).description= 0 0105 case Bold
StringStyleColorizer.swift:17 return Colorizer(string: string, color: StringTextStyle.Reset).description= 1 0106 case Italic
StringStyleColorizer.swift:21 return Colorizer(string: string, color: StringTextStyle.Bold).description= 3 0107 case Underline
StringStyleColorizer.swift:25 return Colorizer(string: string, color: StringTextStyle.Italic).description= 4 0108 case Inverse
StringStyleColorizer.swift:29 return Colorizer(string: string, color: StringTextStyle.Underline).description= 7 0109 case Strikethrough
StringStyleColorizer.swift:33 return Colorizer(string: string, color: StringTextStyle.Inverse).description= 9 0110 case BoldOff
StringStyleColorizer.swift:37 return Colorizer(string: string, color: StringTextStyle.Strikethrough).description= 22 0111 case ItalicOff
StringStyleColorizer.swift:41 return Colorizer(string: string, color: StringTextStyle.BoldOff).description= 23 0112 case UnderlineOff
StringStyleColorizer.swift:45 return Colorizer(string: string, color: StringTextStyle.ItalicOff).description= 24 0113 case InverseOff
StringStyleColorizer.swift:49 return Colorizer(string: string, color: StringTextStyle.UnderlineOff).description= 27 0114 case StrikethroughOff
StringStyleColorizer.swift:53 return Colorizer(string: string, color: StringTextStyle.InverseOff).description= 29 0115 }
StringStyleColorizer.swift:57 return Colorizer(string: string, color: StringTextStyle.StrikethroughOff).description