Skip to content

Commit ac9dd23

Browse files
authored
Merge pull request #8 from enuance/CPFeature-006-TerminalColors
CPFeature-006 Terminal Colors
2 parents 7a9f02c + 3c83d16 commit ac9dd23

File tree

10 files changed

+246
-124
lines changed

10 files changed

+246
-124
lines changed

Package.resolved

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ let package = Package(
99
// 📁 John Sundell's Files Package is great for easy file reading/writing/moving/etc.
1010
.package(url: "https://github.com/JohnSundell/Files", from: "4.0.0"),
1111
// 🧰 SPMUtilities for CLI Argument Parsing.
12-
.package(url: "https://github.com/apple/swift-package-manager", from: "0.5.0")
12+
.package(url: "https://github.com/apple/swift-package-manager", from: "0.5.0"),
13+
// Consler for Styled outputs to the Console
14+
.package(url: "https://github.com/enuance/consler", from: "0.2.0")
1315
],
1416
targets: [
1517
.target(
1618
name: "commitPrefix",
17-
dependencies: ["Files", "SPMUtility"],
19+
dependencies: ["Files", "SPMUtility", "Consler"],
1820
// Normally don't have to specify the path, but I wan't the actual executable to be
1921
// lowercase and SPM brings folders in Uppercased by default.
2022
path: "Sources/CommitPrefix"),

Sources/CommitPrefix/CPFileHandler.swift

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2525
// SOFTWARE.
2626

27+
import Consler
2728
import Foundation
2829
import Files
2930

@@ -40,40 +41,40 @@ struct CPFileHandler {
4041
try CommitMessageHook.findOrCreate(with: gitDirectory)
4142
}
4243

43-
func outputPrefixes() throws -> String {
44+
func outputPrefixes() throws -> ConslerOutput {
4445
try cpInteractor.outputPrefixes()
4546
}
4647

47-
func viewState() throws -> String {
48+
func viewState() throws -> ConslerOutput {
4849
let cpState = try cpInteractor.getCommitPrefixState()
4950
switch cpState.mode {
5051
case .normal:
51-
return """
52-
CommitPrefix MODE NORMAL
53-
- prefixes: \(cpState.normalPrefixes.joined())
54-
"""
52+
return ConslerOutput(values: [
53+
"CommitPrefix ", "MODE NORMAL",
54+
"- prefixes: ", cpState.normalPrefixes.joined()])
55+
.describedBy(.normal, .cyanEndsLine, .normal, .cyan)
5556
case .branchParse:
56-
return """
57-
CommitPrefix MODE BRANCH_PARSE
58-
- branch prefixes: \(cpState.branchPrefixes.joined())
59-
- stored prefixes: \(cpState.normalPrefixes.joined())
60-
"""
57+
return ConslerOutput(values: [
58+
"CommitPrefix ", "MODE BRANCH_PARSE",
59+
"- branch prefixes: ", cpState.branchPrefixes.joined(),
60+
"- stored prefixes: ", cpState.normalPrefixes.joined()])
61+
.describedBy(.normal, .cyanEndsLine, .normal, .cyanEndsLine, .normal, .cyan)
6162
}
6263
}
6364

64-
func deletePrefixes() throws -> String {
65+
func deletePrefixes() throws -> ConslerOutput {
6566
try cpInteractor.deletePrefixes()
6667
}
6768

68-
func writeNew(prefixes rawValue: String) throws -> String {
69+
func writeNew(prefixes rawValue: String) throws -> ConslerOutput {
6970
try cpInteractor.writeNew(prefixes: rawValue)
7071
}
7172

72-
func activateBranchMode(with validator: String) throws -> String {
73+
func activateBranchMode(with validator: String) throws -> ConslerOutput {
7374
try cpInteractor.activateBranchMode(with: validator)
7475
}
7576

76-
func activateNormalMode() throws -> String {
77+
func activateNormalMode() throws -> ConslerOutput {
7778
try cpInteractor.activateNormalMode()
7879
}
7980

Sources/CommitPrefix/CPInteractor.swift

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2525
// SOFTWARE.
2626

27-
import Foundation
27+
import Consler
2828
import Files
29+
import Foundation
2930

3031
struct CPInteractor {
3132

@@ -53,7 +54,7 @@ struct CPInteractor {
5354
return (cpFile, cpModel, headFile)
5455
} catch {
5556
cpDebugPrint(error)
56-
throw CPError.fileReadWriteError
57+
throw CPError.cpFileIOError
5758
}
5859
}
5960

@@ -64,24 +65,24 @@ struct CPInteractor {
6465
try commitPrefixFile.write(modelData)
6566
} catch {
6667
cpDebugPrint(error)
67-
throw CPError.fileReadWriteError
68+
throw CPError.cpFileIOError
6869
}
6970
}
7071

7172
private func branchPrefixes() throws -> [String] {
7273
guard let regexValue = commitPrefixModel.regexValue else {
73-
throw CPTermination.branchValidatorNotPresent
74+
throw CPError.branchValidatorNotFound
7475
}
7576

7677
guard let branch = try? gitHEADFile.readAsString(encodedAs: .utf8) else {
77-
throw CPTermination.unableToReadHEAD
78+
throw CPError.headFileIOError
7879
}
7980

8081
let matches = branch.occurances(ofRegex: regexValue)
8182

8283
guard matches.count > 0 else {
8384
let validator = commitPrefixModel.branchValidator ?? "Validator Not Present"
84-
throw CPTermination.invalidBranchPrefix(validator: validator)
85+
throw CPError.invalidBranchPrefix(validator: validator)
8586
}
8687

8788
let uniqueMatches = Set(matches)
@@ -101,20 +102,20 @@ struct CPInteractor {
101102
let containsNoNumbers = validator.occurances(ofRegex: #"(\d+)"#).isEmpty
102103
let atLeastTwoCharacters = validator.count > 1
103104
guard containsNoNumbers && atLeastTwoCharacters else {
104-
throw CPError.branchValidatorFormatError
105+
throw CPError.invalidBranchValidatorFormat
105106
}
106107
return validator
107108
}
108109

109-
func outputPrefixes() throws -> String {
110+
func outputPrefixes() throws -> ConslerOutput {
110111
switch commitPrefixModel.prefixMode {
111112
case .normal:
112-
return commitPrefixModel.prefixes.joined()
113+
return ConslerOutput(commitPrefixModel.prefixes.joined())
113114
case .branchParse:
114115
let retrievedBranchPrefixes = try branchPrefixes()
115116
let branchPrefixes = retrievedBranchPrefixes.map { "[\($0)]" }.joined()
116117
let normalPrefixes = commitPrefixModel.prefixes.joined()
117-
return branchPrefixes + normalPrefixes
118+
return ConslerOutput(branchPrefixes, normalPrefixes)
118119
}
119120
}
120121

@@ -138,34 +139,37 @@ struct CPInteractor {
138139
}
139140
}
140141

141-
func deletePrefixes() throws -> String {
142+
func deletePrefixes() throws -> ConslerOutput {
142143
let newModel = commitPrefixModel.updated(with: [])
143144
try saveCommitPrefix(model: newModel)
144-
return "CommitPrefix DELETED"
145+
return ConslerOutput("CommitPrefix ", "DELETED").describedBy(.normal, .red)
145146
}
146147

147-
func writeNew(prefixes rawValue: String) throws -> String {
148+
func writeNew(prefixes rawValue: String) throws -> ConslerOutput {
148149
let newPrefixes = prefixFormatter(rawValue)
149150
let newModel = commitPrefixModel.updated(with: newPrefixes)
150151
try saveCommitPrefix(model: newModel)
151-
return "CommitPrefix STORED \(newPrefixes.joined())"
152+
return ConslerOutput("CommitPrefix ", "STORED ", newPrefixes.joined())
153+
.describedBy(.normal, .green, .green)
152154
}
153155

154-
func activateBranchMode(with validator: String) throws -> String {
156+
func activateBranchMode(with validator: String) throws -> ConslerOutput {
155157
let formattedValidator = try validatorFormatter(validator)
156158
let newModel = commitPrefixModel.updatedAsBranchMode(with: formattedValidator)
157159
try saveCommitPrefix(model: newModel)
158-
return "CommitPrefix MODE BRANCH_PARSE \(formattedValidator)"
160+
return ConslerOutput("CommitPrefix ","MODE BRANCH_PARSE ", formattedValidator)
161+
.describedBy(.normal, .cyan, .green)
159162
}
160163

161-
func activateNormalMode() throws -> String {
164+
func activateNormalMode() throws -> ConslerOutput {
162165
switch commitPrefixModel.prefixMode {
163166
case .normal:
164-
return "CommitPrefix already in MODE NORMAL"
167+
return ConslerOutput("CommitPrefix ", "already in ", "MODE NORMAL")
168+
.describedBy(.normal, .yellow, .cyan)
165169
case .branchParse:
166170
let newModel = commitPrefixModel.updatedAsNormalMode()
167171
try saveCommitPrefix(model: newModel)
168-
return "CommitPrefix MODE NORMAL"
172+
return ConslerOutput("CommitPrefix ", "MODE NORMAL").describedBy(.normal, .cyan)
169173
}
170174
}
171175

Sources/CommitPrefix/Constants.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import Foundation
2828

2929
struct CPInfo {
3030

31-
static let version = "1.3.2"
31+
static let version = "1.4.0"
3232

3333
}
3434

0 commit comments

Comments
 (0)