diff --git a/Source/swiftlint/ExplainCommand.swift b/Source/swiftlint/ExplainCommand.swift index 57020087e..16c8ade50 100644 --- a/Source/swiftlint/ExplainCommand.swift +++ b/Source/swiftlint/ExplainCommand.swift @@ -11,42 +11,6 @@ import Commandant import LlamaKit import SwiftLintFramework -typealias StructuredInlineText = String - -extension Array { - func dropWhile(x: Element -> Bool) -> [Element] { - var count = 0 - while x(self[count]) { count++ } - var copy = self - copy.removeRange(0.. StructuredText { return .Joined([ .Header(level: 1, text: example.ruleName), @@ -68,7 +32,7 @@ struct ExplainCommand: CommandType { switch mode { case let .Arguments: for example: RuleExample in Linter.explainableRules { - println(describeExample(example).markdown) + println(describeExample(example).ansi) } diff --git a/Source/swiftlint/StructuredText.swift b/Source/swiftlint/StructuredText.swift new file mode 100644 index 000000000..f97c6c1be --- /dev/null +++ b/Source/swiftlint/StructuredText.swift @@ -0,0 +1,79 @@ +// +// StructuredText.swift +// SwiftLint +// +// Created by Chris Eidhof on 20/05/15. +// Copyright (c) 2015 Realm. All rights reserved. +// + +import Foundation + +typealias StructuredInlineText = String + +private extension Array { + func dropWhile(x: Element -> Bool) -> [Element] { + var count = 0 + while x(self[count]) { count++ } + var copy = self + copy.removeRange(0.. String { + return self.string + text + AnsiCode.Reset.string + } + + static func red(string: String) -> String { + return AnsiCode.RedForeground.wrap(string) + } + + static func green(string: String) -> String { + return AnsiCode.GreenForeground.wrap(string) + } +} + +enum StructuredText { + case Header(level: Int, text: StructuredInlineText) + case Paragraph(StructuredInlineText) + case List(items: [StructuredText]) + case Joined([StructuredText]) + + var markdown: String { + switch self { + case let .Header(level, t): return join("", Array(count: level, repeatedValue: "#")) + " " + t + case .Paragraph(let t): return t.chomped + case .List(let items): return "\n".join(items.map { "* " + $0.markdown }) + case .Joined(let items): return "\n\n".join(items.map { $0.markdown } ) + } + } + + var ansi: String { + switch self { + case .Header(1, let t): return t.uppercaseString + case let .Header(other, t): return t + case .Paragraph(let t): return t.chomped + case .List(let items): return "\n".join(items.map { "* " + $0.ansi }) + case .Joined(let items): return "\n\n".join(items.map { $0.ansi } ) + } + } +} \ No newline at end of file diff --git a/SwiftLint.xcodeproj/project.pbxproj b/SwiftLint.xcodeproj/project.pbxproj index 495405d4c..22ec0001b 100644 --- a/SwiftLint.xcodeproj/project.pbxproj +++ b/SwiftLint.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 83894F221B0C928A006214E1 /* ExplainCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83894F211B0C928A006214E1 /* ExplainCommand.swift */; }; + 83894F241B0CAD41006214E1 /* StructuredText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83894F231B0CAD41006214E1 /* StructuredText.swift */; }; D0AAAB5019FB0960007B24B3 /* SwiftLintFramework.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D0D1216D19E87B05005E4BAA /* SwiftLintFramework.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; D0D1217819E87B05005E4BAA /* SwiftLintFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0D1216D19E87B05005E4BAA /* SwiftLintFramework.framework */; }; D0E7B65319E9C6AD00EDBA4D /* SwiftLintFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0D1216D19E87B05005E4BAA /* SwiftLintFramework.framework */; }; @@ -102,6 +103,7 @@ 5499CA961A2394B700783309 /* Components.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Components.plist; sourceTree = ""; }; 5499CA971A2394B700783309 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 83894F211B0C928A006214E1 /* ExplainCommand.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExplainCommand.swift; sourceTree = ""; }; + 83894F231B0CAD41006214E1 /* StructuredText.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StructuredText.swift; sourceTree = ""; }; D0D1211B19E87861005E4BAA /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; usesTabs = 0; }; D0D1212419E878CC005E4BAA /* Common.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Common.xcconfig; sourceTree = ""; }; D0D1212619E878CC005E4BAA /* Debug.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Debug.xcconfig; sourceTree = ""; }; @@ -235,6 +237,7 @@ E83A0B341A5D382B0041A60A /* Version.swift */, 5499CA981A2394BD00783309 /* Supporting Files */, 83894F211B0C928A006214E1 /* ExplainCommand.swift */, + 83894F231B0CAD41006214E1 /* StructuredText.swift */, ); name = swiftlint; path = Source/swiftlint; @@ -582,6 +585,7 @@ E83A0B351A5D382B0041A60A /* Version.swift in Sources */, E861519B1B0573B900C54AC0 /* Lint.swift in Sources */, D0E7B65619E9C76900EDBA4D /* main.swift in Sources */, + 83894F241B0CAD41006214E1 /* StructuredText.swift in Sources */, 83894F221B0C928A006214E1 /* ExplainCommand.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0;