🐛 Print warnings and errors to stderr

Fixed #351.
This commit is contained in:
Chris Araman 2021-11-01 21:10:56 -07:00 committed by Chris Araman
parent 7fe27fc693
commit 3dc5789cdf
3 changed files with 49 additions and 10 deletions

View file

@ -51,7 +51,7 @@ private func downloadWithRetries(
let attempts = attempts - 1
printWarning((downloadError ?? error).localizedDescription)
print("Trying again up to \(attempts) more \(attempts == 1 ? "time" : "times").")
printWarning("Trying again up to \(attempts) more \(attempts == 1 ? "time" : "times").")
return downloadWithRetries(appID, purchase: purchase, attempts: attempts)
}
}

View file

@ -50,7 +50,7 @@ public struct OpenCommand: CommandProtocol {
guard let appId = Int(options.appId)
else {
print("Invalid app ID")
printError("Invalid app ID")
return .failure(.noSearchResultsFound)
}

View file

@ -19,7 +19,11 @@ let csi = "\u{001B}["
// Override global print for testability.
// See MasKitTests/OutputListener.swift.
func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
func print(
_ items: Any...,
separator: String = " ",
terminator: String = "\n"
) {
if let observer = printObserver {
let output =
items
@ -39,8 +43,42 @@ let csi = "\u{001B}["
Swift.print(terminator, terminator: "")
}
func print<Target>(
_ items: Any...,
separator: String = " ",
terminator: String = "\n",
to output: inout Target
) where Target: TextOutputStream {
if let observer = printObserver {
let output =
items
.map { "\($0)" }
.joined(separator: separator)
.appending(terminator)
observer(output)
}
var prefix = ""
for item in items {
Swift.print(prefix, terminator: "", to: &output)
Swift.print(item, terminator: "", to: &output)
prefix = separator
}
Swift.print(terminator, terminator: "", to: &output)
}
#endif
private var standardError = FileHandle.standardError
extension FileHandle: TextOutputStream {
public func write(_ string: String) {
guard let data = string.data(using: .utf8) else { return }
write(data)
}
}
func printInfo(_ message: String) {
guard isatty(fileno(stdout)) != 0 else {
print("==> \(message)")
@ -51,30 +89,31 @@ func printInfo(_ message: String) {
print("\(csi)1;34m==>\(csi)0m \(csi)1m\(message)\(csi)0m")
}
func printWarning(_ message: String) {
guard isatty(fileno(stdout)) != 0 else {
print("Warning: \(message)")
public func printWarning(_ message: String) {
guard isatty(fileno(stderr)) != 0 else {
print("Warning: \(message)", to: &standardError)
return
}
// Yellow, underlined "Warning:" prefix
print("\(csi)4;33mWarning:\(csi)0m \(message)")
print("\(csi)4;33mWarning:\(csi)0m \(message)", to: &standardError)
}
public func printError(_ message: String) {
guard isatty(fileno(stdout)) != 0 else {
print("Error: \(message)")
guard isatty(fileno(stderr)) != 0 else {
print("Error: \(message)", to: &standardError)
return
}
// Red, underlined "Error:" prefix
print("\(csi)4;31mError:\(csi)0m \(message)")
print("\(csi)4;31mError:\(csi)0m \(message)", to: &standardError)
}
func clearLine() {
guard isatty(fileno(stdout)) != 0 else {
return
}
print("\(csi)2K\(csi)0G", terminator: "")
fflush(stdout)
}