From 6f2e521a5a31303286986d1ed54168e4ea1955eb Mon Sep 17 00:00:00 2001 From: ddiekmann Date: Mon, 25 Jun 2018 16:18:00 +0200 Subject: [PATCH 1/2] :sparkles: Adding price feature to search command. Add '--price' to list prices in search. --- App/Commands/Search.swift | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/App/Commands/Search.swift b/App/Commands/Search.swift index cb766b6..cfa5075 100644 --- a/App/Commands/Search.swift +++ b/App/Commands/Search.swift @@ -12,6 +12,7 @@ struct ResultKeys { static let TrackName = "trackName" static let TrackId = "trackId" static let Version = "version" + static let Price = "price" } struct SearchCommand: CommandProtocol { @@ -32,11 +33,33 @@ struct SearchCommand: CommandProtocol { return .failure(.noSearchResultsFound) } + // find out longest appName for formatting + var appNameMaxLength = 0 + for result in results { + if let appName = result[ResultKeys.TrackName] as? String { + if appName.count > appNameMaxLength { + appNameMaxLength = appName.count + } + } + } + if appNameMaxLength > 50 { + appNameMaxLength = 50 + } + for result in results { if let appName = result[ResultKeys.TrackName] as? String, - let appVersion = result[ResultKeys.Version] as? String, - let appId = result[ResultKeys.TrackId] as? Int { - print("\(String(appId)) \(appName) (\(appVersion))") + let appVersion = result[ResultKeys.Version] as? String, + let appId = result[ResultKeys.TrackId] as? Int, + let appPrice = result[ResultKeys.Price] as? Double { + + // add empty spaces to app name that every app name has the same length + let countedAppName = String((appName + String(repeating: " ", count: appNameMaxLength)).prefix(appNameMaxLength)) + + if options.price { + print(String(format:"%12d %@ $%5.2f (%@)", appId, countedAppName, appPrice, appVersion)) + } else { + print(String(format:"%12d %@ (%@)", appId, countedAppName, appVersion)) + } } } @@ -53,13 +76,17 @@ struct SearchCommand: CommandProtocol { struct SearchOptions: OptionsProtocol { let appName: String + let price: Bool - static func create(_ appName: String) -> SearchOptions { - return SearchOptions(appName: appName) + static func create(_ appName: String) -> (_ price: Bool) -> SearchOptions { + return { price in + SearchOptions(appName: appName, price: price) + } } static func evaluate(_ m: CommandMode) -> Result> { return create <*> m <| Argument(usage: "the app name to search") + <*> m <| Option(key: "price", defaultValue: false, usage: "Show price of found apps") } } From be688408d0e52fed6a82422f49cce2a459fd4b5c Mon Sep 17 00:00:00 2001 From: ddiekmann Date: Mon, 25 Jun 2018 16:46:25 +0200 Subject: [PATCH 2/2] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc651ee..7b74921 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] - 👷🏻‍♀️⚠️ Re-enable Danger #137 +- ✨ Add price to search #62 ## [v1.4.1] Stop Littering - 2018-02-18