2020-06-07 20:01:34 +00:00
|
|
|
//
|
|
|
|
// 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 {
|
|
|
|
/// 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
|
2020-06-07 20:17:07 +00:00
|
|
|
let minWidth = 50
|
|
|
|
let maxLength = products.map { $0.appNameOrBbundleIdentifier }
|
2020-06-07 20:01:34 +00:00
|
|
|
.max(by: { $1.count > $0.count })?.count
|
|
|
|
?? minWidth
|
|
|
|
|
|
|
|
var output: String = ""
|
|
|
|
|
|
|
|
for product in products {
|
2020-08-23 02:43:21 +00:00
|
|
|
let appId = product.itemIdentifier
|
2020-06-07 20:17:07 +00:00
|
|
|
let appName = product.appNameOrBbundleIdentifier.padding(toLength: maxLength, withPad: " ", startingAt: 0)
|
2020-06-07 20:01:34 +00:00
|
|
|
let version = product.bundleVersion
|
|
|
|
|
2020-08-23 02:43:21 +00:00
|
|
|
output += "\(appId) \(appName) (\(version))\n"
|
2020-06-07 20:01:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return output.trimmingCharacters(in: .newlines)
|
|
|
|
}
|
|
|
|
}
|