Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions App/Commands/Search.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
}
}
}

Expand All @@ -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<SearchOptions, CommandantError<MASError>> {
return create
<*> m <| Argument(usage: "the app name to search")
<*> m <| Option(key: "price", defaultValue: false, usage: "Show price of found apps")
}
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down