From f80cb000f95d7cd1e364f6dd099f271b278a166d Mon Sep 17 00:00:00 2001 From: Julian Kahnert Date: Thu, 16 Jan 2020 23:46:17 +0100 Subject: [PATCH] cli implementation completed --- Package.swift | 4 ++-- Sources/TerminalLog/ASCIIColor.swift | 25 +++++++++++++++++++++++++ Sources/TerminalLog/TerminalLog.swift | 15 +++++---------- 3 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 Sources/TerminalLog/ASCIIColor.swift diff --git a/Package.swift b/Package.swift index 5f1a11a..c61b194 100644 --- a/Package.swift +++ b/Package.swift @@ -17,7 +17,7 @@ let package = Package( .package(url: "https://github.com/johnsundell/shellout.git", from: "2.0.0"), .package(url: "https://github.com/JohnSundell/Files", from: "4.0.0"), .package(url: "https://github.com/jpsim/Yams.git", from: "2.0.0"), - .package(url: "https://github.com/apple/swift-tools-support-core.git", from: "0.0.1"), + .package(url: "https://github.com/apple/swift-tools-support-core.git", from: "0.0.0"), .package(url: "https://github.com/apple/swift-log.git", from: "1.0.0") ], targets: [ @@ -30,7 +30,7 @@ let package = Package( .target( name: "TerminalLog", dependencies: [ - "Logging", "SwiftToolsSupport-auto" + "Logging" ] ), .target( diff --git a/Sources/TerminalLog/ASCIIColor.swift b/Sources/TerminalLog/ASCIIColor.swift new file mode 100644 index 0000000..0dfa7e4 --- /dev/null +++ b/Sources/TerminalLog/ASCIIColor.swift @@ -0,0 +1,25 @@ +// +// ASCIIColor.swift +// +// +// Created by Julian Kahnert on 16.01.20. +// +// Source: https://stackoverflow.com/a/56510700/10026834 + +enum ASCIIColor: String { + case black = "\u{001B}[0;30m" + case red = "\u{001B}[0;31m" + case green = "\u{001B}[0;32m" + case yellow = "\u{001B}[0;33m" + case blue = "\u{001B}[0;34m" + case magenta = "\u{001B}[0;35m" + case cyan = "\u{001B}[0;36m" + case white = "\u{001B}[0;37m" + case `default` = "\u{001B}[0;0m" +} + +extension DefaultStringInterpolation { + mutating func appendInterpolation(_ value: T, color: ASCIIColor) { + appendInterpolation("\(color.rawValue)\(value)\(ASCIIColor.default.rawValue)") + } +} diff --git a/Sources/TerminalLog/TerminalLog.swift b/Sources/TerminalLog/TerminalLog.swift index bdfa03f..f3c718e 100644 --- a/Sources/TerminalLog/TerminalLog.swift +++ b/Sources/TerminalLog/TerminalLog.swift @@ -7,7 +7,6 @@ // Source: https://github.com/chrisaljoudi/swift-log-oslog import Logging -import TSCBasic public struct TerminalLog: LogHandler { @@ -19,12 +18,8 @@ public struct TerminalLog: LogHandler { public var logLevel: Logger.Level = .info private var prettyMetadata: String? - private let terminalController: TerminalController - public init(_: String) { - guard let tc = TerminalController(stream: stdoutStream) else { fatalError("Could not create an instance of TerminalController.") } - self.terminalController = tc - } + public init(_: String) { } public func log(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, file: String, function: String, line: UInt) { @@ -42,7 +37,7 @@ public struct TerminalLog: LogHandler { formedMessage += " -- " + combinedPrettyMetadata! } - terminalController.write(formedMessage + "\n", inColor: getColor(for: level)) + print("\(formedMessage, color: getColor(for: level))\n") } /// Add, remove, or change the logging metadata. @@ -66,8 +61,8 @@ public struct TerminalLog: LogHandler { }.joined(separator: " ") } - private func getColor(for level: Logger.Level) -> TerminalController.Color { - let color: TerminalController.Color + private func getColor(for level: Logger.Level) -> ASCIIColor { + let color: ASCIIColor switch level { case .critical, .error: color = .red @@ -76,7 +71,7 @@ public struct TerminalLog: LogHandler { case .info: color = .green default: - color = .noColor + color = .default } return color }