Merge pull request #278 from danielbayley/list

Improve list command output
This commit is contained in:
Ben Chatelain 2020-08-22 21:08:28 -06:00 committed by GitHub
commit b212acfd34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 8 deletions

View file

@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
- ✨ `Makefile` #277
- 🎨 Improve `mas list` command output #278
## [v1.7.0] 🛍 Purchase Command - 2020-05-24

View file

@ -35,13 +35,10 @@ public struct ListCommand: CommandProtocol {
print("No installed apps found")
return .success(())
}
for product in products {
var appName = product.appName
if appName == "" {
appName = product.bundleIdentifier
}
print("\(product.itemIdentifier) \(appName) (\(product.bundleVersion))")
}
let output = AppListFormatter.format(products: products)
print(output)
return .success(())
}
}

View file

@ -0,0 +1,36 @@
//
// 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
let minWidth = 50
let maxLength = products.map { $0.appNameOrBbundleIdentifier }
.max(by: { $1.count > $0.count })?.count
?? minWidth
var output: String = ""
for product in products {
let appId = product.itemIdentifier
let appName = product.appNameOrBbundleIdentifier.padding(toLength: maxLength, withPad: " ", startingAt: 0)
let version = product.bundleVersion
output += "\(appId) \(appName) (\(version))\n"
}
return output.trimmingCharacters(in: .newlines)
}
}

View file

@ -24,4 +24,9 @@ extension SoftwareProduct {
&& lhs.bundleVersion == rhs.bundleVersion
&& lhs.itemIdentifier == rhs.itemIdentifier
}
/// Returns bundleIdentifier if appName is empty string.
var appNameOrBbundleIdentifier: String {
appName == "" ? bundleIdentifier : appName
}
}

View file

@ -110,6 +110,7 @@
F85DA8B0240C32FA00FE5650 /* SoftwareMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = F85DA8AF240C32FA00FE5650 /* SoftwareMap.swift */; };
F85DA8B2240CBAFE00FE5650 /* CKSoftwareMap+SoftwareMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = F85DA8B1240CBAFE00FE5650 /* CKSoftwareMap+SoftwareMap.swift */; };
F88CB8E12404DAAD00B691B5 /* OpenSystemCommandSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = F88CB8E02404DAAD00B691B5 /* OpenSystemCommandSpec.swift */; };
F8A41ECE248D76CB00D374CF /* AppListFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8A41ECD248D76CB00D374CF /* AppListFormatter.swift */; };
F8FB715B20F2B41400F56FDC /* MasKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F8FB715220F2B41400F56FDC /* MasKit.framework */; };
F8FB716220F2B41400F56FDC /* MasKit.h in Headers */ = {isa = PBXBuildFile; fileRef = F8FB715420F2B41400F56FDC /* MasKit.h */; settings = {ATTRIBUTES = (Public, ); }; };
F8FB716A20F2B4DD00F56FDC /* Downloader.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED0F238A1B87569C00AE40CD /* Downloader.swift */; };
@ -326,6 +327,7 @@
F85DA8AF240C32FA00FE5650 /* SoftwareMap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoftwareMap.swift; sourceTree = "<group>"; };
F85DA8B1240CBAFE00FE5650 /* CKSoftwareMap+SoftwareMap.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CKSoftwareMap+SoftwareMap.swift"; sourceTree = "<group>"; };
F88CB8E02404DAAD00B691B5 /* OpenSystemCommandSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenSystemCommandSpec.swift; sourceTree = "<group>"; };
F8A41ECD248D76CB00D374CF /* AppListFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppListFormatter.swift; sourceTree = "<group>"; };
F8FB715220F2B41400F56FDC /* MasKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MasKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
F8FB715420F2B41400F56FDC /* MasKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MasKit.h; sourceTree = "<group>"; };
F8FB715520F2B41400F56FDC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@ -527,6 +529,7 @@
isa = PBXGroup;
children = (
B576FE2921E4240B0016B39D /* AppInfoFormatter.swift */,
F8A41ECD248D76CB00D374CF /* AppListFormatter.swift */,
B576FE3221E985250016B39D /* SearchResultFormatter.swift */,
EDCBF9541D89CFC7000039C6 /* Utilities.swift */,
);
@ -1029,6 +1032,7 @@
B576FE0021E113610016B39D /* NetworkSession.swift in Sources */,
B5DBF80D21DEE4E600F3B151 /* Open.swift in Sources */,
B576FDF721E107AA0016B39D /* OpenSystemCommand.swift in Sources */,
F8A41ECE248D76CB00D374CF /* AppListFormatter.swift in Sources */,
F8FB717420F2B4DD00F56FDC /* Outdated.swift in Sources */,
75FB3E761F9F7841005B6F20 /* Purchase.swift in Sources */,
F8FB716C20F2B4DD00F56FDC /* PurchaseDownloadObserver.swift in Sources */,

View file

@ -71,7 +71,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "1"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
@ -87,6 +87,12 @@
ReferencedContainer = "container:mas-cli.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "list"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"