Skip to content

Commit c31e2d7

Browse files
authored
Merge pull request #1014 from rgoldberg/1013-remove-formatters
Inline code from formatter enums & remove them
2 parents e246c9a + c15d92a commit c31e2d7

File tree

9 files changed

+66
-236
lines changed

9 files changed

+66
-236
lines changed

Sources/mas/Commands/Info.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,34 @@ extension MAS {
3333
private func run(printer: Printer, searcher: some AppStoreSearcher) async {
3434
var spacing = ""
3535
await requiredAppIDsOptionGroup.forEachAppID(printer: printer) { appID in
36-
printer.info("", AppInfoFormatter.format(app: try await searcher.lookup(appID: appID)), separator: spacing)
36+
let result = try await searcher.lookup(appID: appID)
37+
printer.info(
38+
"",
39+
"""
40+
\(result.name) \(result.version) [\(result.formattedPrice)]
41+
By: \(result.vendorName)
42+
Released: \(result.releaseDate.humanReadableDate)
43+
Minimum OS: \(result.minimumOSVersion)
44+
Size: \(result.fileSizeBytes.humanReadableSize)
45+
From: \(result.appStoreURL)
46+
""",
47+
separator: spacing
48+
)
3749
spacing = "\n"
3850
}
3951
}
4052
}
4153
}
54+
55+
private extension String {
56+
var humanReadableSize: String {
57+
ByteCountFormatter.string(fromByteCount: Int64(self) ?? 0, countStyle: .file)
58+
}
59+
60+
var humanReadableDate: String {
61+
ISO8601DateFormatter().date(from: self).map { date in
62+
ISO8601DateFormatter.string(from: date, timeZone: .current, formatOptions: [.withFullDate])
63+
}
64+
?? "" // swiftformat:disable:this indent
65+
}
66+
}

Sources/mas/Commands/List.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
internal import ArgumentParser
9+
private import Foundation
910

1011
extension MAS {
1112
/// Lists all apps installed from the Mac App Store.
@@ -39,7 +40,25 @@ extension MAS {
3940
"""
4041
)
4142
} else {
42-
printer.info(AppListFormatter.format(installedApps))
43+
guard let maxADAMIDLength = installedApps.map({ String(describing: $0.adamID).count }).max() else {
44+
return
45+
}
46+
guard let maxAppNameLength = installedApps.map(\.name.count).max() else {
47+
return
48+
}
49+
50+
let format = "%\(maxADAMIDLength)lu %@ (%@)"
51+
printer.info(
52+
installedApps.map { installedApp in
53+
String(
54+
format: format,
55+
installedApp.adamID,
56+
installedApp.name.padding(toLength: maxAppNameLength, withPad: " ", startingAt: 0),
57+
installedApp.version
58+
)
59+
}
60+
.joined(separator: "\n")
61+
)
4362
}
4463
}
4564
}

Sources/mas/Commands/Search.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
internal import ArgumentParser
9+
private import Foundation
910

1011
extension MAS {
1112
/// Searches for apps in the Mac App Store.
@@ -37,8 +38,26 @@ extension MAS {
3738
guard !results.isEmpty else {
3839
throw MASError.noSearchResultsFound(for: searchTerm)
3940
}
41+
guard let maxADAMIDLength = results.map({ String(describing: $0.adamID).count }).max() else {
42+
return
43+
}
44+
guard let maxAppNameLength = results.map(\.name.count).max() else {
45+
return
46+
}
4047

41-
printer.info(SearchResultFormatter.format(results, includePrice: price))
48+
let format = "%\(maxADAMIDLength)lu %@ (%@)\(price ? " %@" : "")"
49+
printer.info(
50+
results.map { result in
51+
String(
52+
format: format,
53+
result.adamID,
54+
result.name.padding(toLength: maxAppNameLength, withPad: " ", startingAt: 0),
55+
result.version,
56+
result.formattedPrice
57+
)
58+
}
59+
.joined(separator: "\n")
60+
)
4261
}
4362
}
4463
}

Sources/mas/Formatters/AppInfoFormatter.swift

Lines changed: 0 additions & 45 deletions
This file was deleted.

Sources/mas/Formatters/AppListFormatter.swift

Lines changed: 0 additions & 36 deletions
This file was deleted.

Sources/mas/Formatters/SearchResultFormatter.swift

Lines changed: 0 additions & 39 deletions
This file was deleted.

Tests/MASTests/Formatters/MASTests+AppListFormatter.swift

Lines changed: 0 additions & 43 deletions
This file was deleted.

Tests/MASTests/Formatters/MASTests+SearchResultFormatter.swift

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)