Skip to content

Commit

Permalink
Merge pull request #355 from mas-cli/print
Browse files Browse the repository at this point in the history
🖨 Override print only in debug builds
  • Loading branch information
phatblat authored May 4, 2021
2 parents cd3f2de + d046417 commit ba4e62f
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions Sources/MasKit/Formatters/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,33 @@ import Foundation
/// Terminal Control Sequence Indicator
let csi = "\u{001B}["

#if DEBUG

var printObserver: ((String) -> Void)?

// Override global print for testability.
// See MasKitTests/OutputListener.swift.
func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
let output = items
.map { "\($0)" }
.joined(separator: separator)
.appending(terminator)

if let observer = printObserver {
let output = items
.map { "\($0)" }
.joined(separator: separator)
.appending(terminator)
observer(output)
}

Swift.print(output)
var prefix = ""
for item in items {
Swift.print(prefix, terminator: "")
Swift.print(item, terminator: "")
prefix = separator
}

Swift.print(terminator, terminator: "")
}

#endif

func printInfo(_ message: String) {
guard isatty(fileno(stdout)) != 0 else {
print("==> \(message)")
Expand Down

0 comments on commit ba4e62f

Please sign in to comment.