Skip to content

Commit 2348da8

Browse files
committed
Refactor version color logic
1 parent 0ad2a0a commit 2348da8

File tree

2 files changed

+63
-34
lines changed

2 files changed

+63
-34
lines changed

Source/carthage/Formatting.swift

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,6 @@ public struct ColorOptions: OptionsProtocol {
7777
func quote(_ string: String, quotationMark: String = "\"") -> String {
7878
return wrap(isColorful, wrap: Color.Wrap(foreground: .green))(quotationMark + string + quotationMark)
7979
}
80-
81-
/// Wraps a string in a color
82-
func colored(_ string: String, color: Color.Named.Color) -> String {
83-
return wrap(isColorful, wrap: Color.Wrap(foreground: color))(string)
84-
}
8580
}
8681

8782
private init(argument: ColorArgument) {
@@ -98,3 +93,36 @@ public struct ColorOptions: OptionsProtocol {
9893
)
9994
}
10095
}
96+
97+
extension OutdatedCommand.UpdateAvailabilityAndApplicability {
98+
fileprivate static let dict: [OutdatedCommand.UpdateAvailabilityAndApplicability: Color.Named.Color] = [
99+
.updatesAvailableAllApplicable: .green,
100+
.updatesAvailableSomeApplicable: .yellow,
101+
.updatesAvailableNoneApplicable: .red,
102+
]
103+
}
104+
105+
extension ColorOptions.Formatting {
106+
subscript(_ index: OutdatedCommand.UpdateAvailabilityAndApplicability) -> Wrap {
107+
guard self.isColorful, let color = OutdatedCommand.UpdateAvailabilityAndApplicability.dict[index] else {
108+
return { $0 }
109+
}
110+
return Color.Wrap(foreground: color).wrap
111+
}
112+
113+
public var legendForOutdatedCommand: String? {
114+
guard self.isColorful else { return nil }
115+
116+
let header = "Legend — <color> • «what happens when you run `carthage update`»:\n"
117+
return OutdatedCommand.UpdateAvailabilityAndApplicability.dict.reduce(into: header) {
118+
let (situation, color) = $1
119+
let tabs = String(
120+
repeating: "\t",
121+
count: color == .yellow || color == .magenta ? 1 : 2
122+
)
123+
let colorDescription = Color.Wrap(foreground: color)
124+
.wrap("<" + String(describing: color) + ">")
125+
$0.append(colorDescription + tabs + "" + situation.rawValue + "\n")
126+
}
127+
}
128+
}

Source/carthage/Outdated.swift

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,33 @@ import Foundation
44
import Result
55
import ReactiveSwift
66
import Curry
7-
import PrettyColors
87

98
/// Type that encapsulates the configuration and evaluation of the `outdated` subcommand.
109
public struct OutdatedCommand: CommandProtocol {
10+
enum UpdateAvailabilityAndApplicability: String {
11+
case updatesAvailableAllApplicable =
12+
"Will be updated to the newest version."
13+
case updatesAvailableSomeApplicable =
14+
"Will be updated, but not to the newest version because of the specified version in Cartfile."
15+
case updatesAvailableNoneApplicable =
16+
"Will not be updated because of the specified version in Cartfile."
17+
case noUpdatesAvailable =
18+
"Will not be updated: no updates available."
19+
20+
init(currentVersion current: PinnedVersion, applicableVersion applicable: PinnedVersion, latestVersion latest: PinnedVersion) {
21+
switch (current, applicable, latest) {
22+
case (current, applicable, latest) where current != latest && applicable == latest:
23+
self = .updatesAvailableAllApplicable
24+
case (current, applicable, latest) where current != applicable && applicable != latest:
25+
self = .updatesAvailableSomeApplicable
26+
case (current, applicable, latest) where current != latest && current == applicable:
27+
self = .updatesAvailableNoneApplicable
28+
default:
29+
self = .noUpdatesAvailable
30+
}
31+
}
32+
}
33+
1134
public struct Options: OptionsProtocol {
1235
public let useSSH: Bool
1336
public let isVerbose: Bool
@@ -56,39 +79,17 @@ public struct OutdatedCommand: CommandProtocol {
5679
if !outdatedDependencies.isEmpty {
5780
carthage.println(formatting.path("The following dependencies are outdated:"))
5881

59-
for (project, current, updated, latest) in outdatedDependencies {
60-
let versionColor: Color.Named.Color
61-
switch (current, updated, latest) {
62-
case (_, updated, latest) where updated == latest:
63-
// Update available and applicable
64-
versionColor = .green
65-
case (current, updated, latest) where current != updated && updated != latest:
66-
// Update availabe and applicable, but not to the latest version due to version lock
67-
versionColor = .yellow
68-
case (current, updated, latest) where current == updated:
69-
// Update available, but not applicable due to version lock
70-
versionColor = .red
71-
default:
72-
versionColor = .white
73-
}
74-
82+
for (project, current, applicable, latest) in outdatedDependencies {
7583
if options.outputXcodeWarnings {
76-
carthage.println("warning: \(formatting.projectName(project.name)) is out of date (\(current) -> \(updated)) (Latest: \(latest))")
84+
carthage.println("warning: \(formatting.projectName(project.name)) is out of date (\(current) -> \(applicable)) (Latest: \(latest))")
7785
} else {
78-
let versionSummary = formatting.colored(current.description, color: versionColor)
79-
+ " -> " + formatting.colored(updated.description, color: versionColor)
80-
+ " (Latest: \(latest))"
86+
let availability = UpdateAvailabilityAndApplicability(currentVersion: current, applicableVersion: applicable, latestVersion: latest)
87+
let style = formatting[availability]
88+
let versionSummary = "\(style(current.description)) -> \(style(applicable.description)) (Latest: \(latest))"
8189
carthage.println(formatting.projectName(project.name) + " " + versionSummary)
8290
}
8391
}
84-
85-
if options.colorOptions.formatting.isColorful {
86-
carthage.println(formatting.path("The color indicates what happens when you run `carthage update`"))
87-
carthage.println(formatting.colored("<green>", color: .green) + "\t\t- Will be updated to the newest version")
88-
carthage.println(formatting.colored("<yellow>", color: .yellow) + "\t- Will be updated, but not to the newest version"
89-
+ " because of the specified version in Cartfile")
90-
carthage.println(formatting.colored("<red>", color: .red) + "\t\t- Will not be updated because of the specified version in Cartfile")
91-
}
92+
formatting.legendForOutdatedCommand.map(carthage.println)
9293
} else {
9394
carthage.println("All dependencies are up to date.")
9495
}

0 commit comments

Comments
 (0)