Skip to content

Commit

Permalink
Merge pull request #6 from flmb/swift3-update
Browse files Browse the repository at this point in the history
Updated Swift 3 syntax
  • Loading branch information
icodeforlove committed Feb 16, 2017
2 parents 4a6f382 + 45c7227 commit a72f9b8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
6 changes: 6 additions & 0 deletions Colors.xcodeproj/project.pbxproj
Expand Up @@ -163,9 +163,11 @@
TargetAttributes = {
CCE936601AAAF5B800C9AB54 = {
CreatedOnToolsVersion = 6.1.1;
LastSwiftMigration = 0820;
};
CCE9366B1AAAF5B800C9AB54 = {
CreatedOnToolsVersion = 6.1.1;
LastSwiftMigration = 0820;
};
};
};
Expand Down Expand Up @@ -329,6 +331,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -347,6 +350,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -365,6 +369,7 @@
INFOPLIST_FILE = ColorsTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -379,6 +384,7 @@
INFOPLIST_FILE = ColorsTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
24 changes: 12 additions & 12 deletions Colors/Colors.swift
Expand Up @@ -45,7 +45,7 @@ var styleCodes:[String:Int] = [
"strikethroughOff": 29
]

func getCode(key: String) -> Int? {
func getCode(_ key: String) -> Int? {
if colorCodes[key] != nil {
return colorCodes[key]
}
Expand All @@ -59,7 +59,7 @@ func getCode(key: String) -> Int? {
return nil
}

func addCodeToCodesArray(codes: Array<Int>, code: Int) -> Array<Int> {
func addCodeToCodesArray(_ codes: Array<Int>, code: Int) -> Array<Int> {
var result:Array<Int> = codes

if colorCodes.values.contains(code) {
Expand All @@ -79,22 +79,22 @@ func addCodeToCodesArray(codes: Array<Int>, code: Int) -> Array<Int> {
return result
}

func matchesForRegexInText(regex: String!, text: String!, global: Bool = false) -> [String] {
func matchesForRegexInText(_ regex: String!, text: String!, global: Bool = false) -> [String] {
let regex = try! NSRegularExpression(pattern: regex, options: [])
let nsString = text as NSString
let results = regex.matchesInString(nsString as String, options: [], range: NSMakeRange(0, nsString.length))
let results = regex.matches(in: nsString as String, options: [], range: NSMakeRange(0, nsString.length))

if !global && results.count == 1 {
var result:[String] = []

for i in 0..<results[0].numberOfRanges {
result.append(nsString.substringWithRange(results[0].rangeAtIndex(i)))
result.append(nsString.substring(with: results[0].rangeAt(i)))
}

return result
}
else {
return results.map { nsString.substringWithRange($0.range) }
return results.map { nsString.substring(with: $0.range) }
}
}

Expand All @@ -104,11 +104,11 @@ struct ANSIGroup {

func toString() -> String {
let codeStrings = codes.map { String($0) }
return "\u{001B}[" + codeStrings.joinWithSeparator(";") + "m" + string + "\u{001B}[0m"
return "\u{001B}[" + codeStrings.joined(separator: ";") + "m" + string + "\u{001B}[0m"
}
}

func parseExistingANSI(string: String) -> [ANSIGroup] {
func parseExistingANSI(_ string: String) -> [ANSIGroup] {
var results:[ANSIGroup] = []

let matches = matchesForRegexInText("\\u001B\\[([^m]*)m(.+?)\\u001B\\[0m", text: string, global: true)
Expand All @@ -124,9 +124,9 @@ func parseExistingANSI(string: String) -> [ANSIGroup] {
return results
}

func format(string: String, _ command: String) -> String {
func format(_ string: String, _ command: String) -> String {

if (NSProcessInfo.processInfo().environment["DEBUG"] != nil && NSProcessInfo.processInfo().environment["DEBUG"]! as String == "true" && (NSProcessInfo.processInfo().environment["TEST"] == nil || NSProcessInfo.processInfo().environment["TEST"]! as String == "false")) {
if (ProcessInfo.processInfo.environment["DEBUG"] != nil && ProcessInfo.processInfo.environment["DEBUG"]! as String == "true" && (ProcessInfo.processInfo.environment["TEST"] == nil || ProcessInfo.processInfo.environment["TEST"]! as String == "false")) {
return string
}

Expand All @@ -138,7 +138,7 @@ func format(string: String, _ command: String) -> String {
} else if existingANSI.count > 0 {
return existingANSI.map {
return ANSIGroup(codes: addCodeToCodesArray($0.codes, code: code!), string: $0.string).toString()
}.joinWithSeparator("")
}.joined(separator: "")
} else {
let group = ANSIGroup(codes: [code!], string: string)
return group.toString()
Expand Down Expand Up @@ -242,4 +242,4 @@ public extension String {
var strikethroughOff: String {
return format(self, "strikethroughOff")
}
}
}
10 changes: 5 additions & 5 deletions ColorsTests/ColorsTests.swift
Expand Up @@ -14,7 +14,7 @@ import Colors
class ColorsTests: XCTestCase {

func testForegroundColors() {
self.measureBlock() {
self.measure() {
XCTAssertEqual("test".black, "\u{001B}[30mtest\u{001B}[0m")
XCTAssertEqual("test".red, "\u{001B}[31mtest\u{001B}[0m")
XCTAssertEqual("test".green, "\u{001B}[32mtest\u{001B}[0m")
Expand All @@ -38,7 +38,7 @@ class ColorsTests: XCTestCase {
}

func testBackgroundColors() {
self.measureBlock() {
self.measure() {
XCTAssertEqual("test".blackBackground, "\u{001B}[40mtest\u{001B}[0m")
XCTAssertEqual("test".redBackground, "\u{001B}[41mtest\u{001B}[0m")
XCTAssertEqual("test".greenBackground, "\u{001B}[42mtest\u{001B}[0m")
Expand All @@ -62,7 +62,7 @@ class ColorsTests: XCTestCase {
}

func testStyles() {
self.measureBlock() {
self.measure() {
XCTAssertEqual("test".bold, "\u{001B}[1mtest\u{001B}[0m")
XCTAssertEqual("test".italic, "\u{001B}[3mtest\u{001B}[0m")
XCTAssertEqual("test".underline, "\u{001B}[4mtest\u{001B}[0m")
Expand Down Expand Up @@ -91,13 +91,13 @@ class ColorsTests: XCTestCase {
}

func testReset() {
self.measureBlock() {
self.measure() {
XCTAssertEqual("test".red.reset, "\u{001B}[mtest\u{001B}[0m")
}
}

func testNestedCommands() {
self.measureBlock() {
self.measure() {
XCTAssertEqual("test".red.bold.underline, "\u{001B}[31;1;4mtest\u{001B}[0m")
XCTAssertEqual(("test".red + "test".blue).bold, "\u{001B}[31;1mtest\u{001B}[0m\u{001B}[34;1mtest\u{001B}[0m")
}
Expand Down

0 comments on commit a72f9b8

Please sign in to comment.