2
0
Fork 0
mirror of https://github.com/mas-cli/mas synced 2025-02-22 23:48:33 +00:00
mas/Sources/MasKit/Formatters/AppListFormatter.swift
Chris Araman 2cf5ce3013
⌨️ Fix typo
2021-04-27 15:33:49 -07:00

37 lines
1.2 KiB
Swift

//
// AppListFormatter.swift
// MasKit
//
// Created by Ben Chatelain on 6/7/20.
// Copyright © 2019 mas-cli. All rights reserved.
//
import Foundation
/// Formats text output for the search command.
struct AppListFormatter {
static let idColumnMinWidth = 10
static let nameColumnMinWidth = 50
/// Formats text output with list results.
///
/// - Parameter products: List of sortware products app data.
/// - Returns: Multiliune text outoutp.
static func format(products: [SoftwareProduct]) -> String {
// find longest appName for formatting, default 50
let maxLength = products.map(\.appNameOrBundleIdentifier.count).max() ?? nameColumnMinWidth
var output: String = ""
for product in products {
let appId = product.itemIdentifier.stringValue
.padding(toLength: idColumnMinWidth, withPad: " ", startingAt: 0)
let appName = product.appNameOrBundleIdentifier.padding(toLength: maxLength, withPad: " ", startingAt: 0)
let version = product.bundleVersion
output += "\(appId) \(appName) (\(version))\n"
}
return output.trimmingCharacters(in: .newlines)
}
}