Skip to content

Commit

Permalink
Fix column alignment when selected version has longest name
Browse files Browse the repository at this point in the history
Followup to #134.
  • Loading branch information
interstateone committed Feb 2, 2021
1 parent 2db228c commit 784751d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
33 changes: 19 additions & 14 deletions Sources/XcodesKit/XcodeInstaller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -642,30 +642,35 @@ public final class XcodeInstaller {
let installedXcodes = Current.files.installedXcodes(directory)
.sorted { $0.version < $1.version }

// Add one so there's always at least one space between columns
let maxWidthOfFirstColumn = (installedXcodes.map(\.version.appleDescriptionWithBuildIdentifier.count).max() ?? 0) + 1

for installedXcode in installedXcodes {
let widthOfFirstColumnInThisRow = installedXcode.version.appleDescriptionWithBuildIdentifier.count
var spaceBetweenFirstAndSecondColumns = maxWidthOfFirstColumn - widthOfFirstColumnInThisRow

var output = installedXcode.version.appleDescriptionWithBuildIdentifier
let lines = installedXcodes.map { installedXcode -> String in
var line = installedXcode.version.appleDescriptionWithBuildIdentifier

let selectedString = " (Selected)"
if pathOutput.out.hasPrefix(installedXcode.path.string) {
output += selectedString
spaceBetweenFirstAndSecondColumns -= selectedString.count
line += selectedString
}

return line
}

// Add one so there's always at least one space between columns
let maxWidthOfFirstColumn = (lines.map(\.count).max() ?? 0) + 1

for (index, installedXcode) in installedXcodes.enumerated() {
var line = lines[index]
let widthOfFirstColumnInThisRow = line.count
let spaceBetweenFirstAndSecondColumns = maxWidthOfFirstColumn - widthOfFirstColumnInThisRow

// If outputting to an interactive terminal, align the columns so they're easier for a human to read
// Otherwise, separate columns by a tab character so it's easier for a computer to split up
if Current.shell.isatty() {
output += Array(repeating: " ", count: max(spaceBetweenFirstAndSecondColumns, 0))
output += "\(installedXcode.path.string)"
line += Array(repeating: " ", count: max(spaceBetweenFirstAndSecondColumns, 0))
line += "\(installedXcode.path.string)"
} else {
output += "\t\(installedXcode.path.string)"
line += "\t\(installedXcode.path.string)"
}

Current.logging.log(output)
Current.logging.log(line)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/XcodesKitTests/XcodesKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ final class XcodesKitTests: XCTestCase {

// One is selected
Current.shell.xcodeSelectPrintPath = {
Promise.value((status: 0, out: "/Applications/Xcode-2.0.0.app/Contents/Developer", err: ""))
Promise.value((status: 0, out: "/Applications/Xcode-2.0.1-Release.Candidate.app/Contents/Developer", err: ""))
}

// Standard output is an interactive terminal
Expand All @@ -954,9 +954,9 @@ final class XcodesKitTests: XCTestCase {
XCTAssertEqual(
log,
"""
0.0 (ABC123) /Applications/Xcode-0.0.0.app
2.0 (ABC123) (Selected) /Applications/Xcode-2.0.0.app
2.0.1 Release Candidate (ABC123) /Applications/Xcode-2.0.1-Release.Candidate.app
0.0 (ABC123) /Applications/Xcode-0.0.0.app
2.0 (ABC123) /Applications/Xcode-2.0.0.app
2.0.1 Release Candidate (ABC123) (Selected) /Applications/Xcode-2.0.1-Release.Candidate.app
"""
)
Expand Down

0 comments on commit 784751d

Please sign in to comment.