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