Skip to content

Commit

Permalink
Merge pull request #14 from mtynior/swift_6_update
Browse files Browse the repository at this point in the history
Update code to Swift 6. Fix #13
  • Loading branch information
mtynior authored Sep 24, 2024
2 parents 50b6a31 + 13aeddb commit 4e7daa1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
6 changes: 0 additions & 6 deletions Example/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func printStyles() {
}

func printF1() {

let car1 = TerminalColor.green.foregroundStyleCode().open
let car2 = TerminalColor.red.foregroundStyleCode().open
let car3 = TerminalColor.mediumPurple.foregroundStyleCode().open
Expand Down Expand Up @@ -58,11 +57,9 @@ func printF1() {
print(car3 + " ▀ ▀ " + car4 + " ███▀████▀███ ")
print(car3 + " " + car4 + " ▀ ▀ ")
print("\n\n")

}

func printPacman() {

// inspired by https://github.com/jaykru/colorscripts

let pacmanColor = TerminalColor.yellow.foregroundStyleCode().open
Expand All @@ -83,7 +80,6 @@ func printPacman() {
}

func printMario() {

let red = TerminalColor.red.foregroundStyleCode().open
let brown = TerminalColor.darkRed.foregroundStyleCode().open
let skin = TerminalColor.sandyBrown.foregroundStyleCode().open
Expand All @@ -109,7 +105,6 @@ func printMario() {
print("\(brown) ███ ███ ")
print("\(brown) ████ ████")
print("\n\n")

}

func printInfo() {
Expand All @@ -136,5 +131,4 @@ else {
print("Unrecognized command `\(argument)`.")
printInfo()
}

}
30 changes: 27 additions & 3 deletions Sources/ColorizeSwift/ColorizeSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ public struct TerminalStyle {

extension String {
/// Enable/disable colorization
public static var isColorizationEnabled = true
public static var isColorizationEnabled: Bool {
get {
return StringColorizationManager.shared.isColorizationEnabled
}
set {
StringColorizationManager.shared.isColorizationEnabled = newValue
}
}

public func bold() -> String {
return applyStyle(TerminalStyle.bold)
Expand Down Expand Up @@ -93,7 +100,7 @@ extension String {
}

public func reset() -> String {
guard String.isColorizationEnabled else { return self }
guard StringColorizationManager.shared.isColorizationEnabled else { return self }
return "\u{001B}[0m" + self
}

Expand All @@ -116,7 +123,7 @@ extension String {
}

private func applyStyle(_ codeStyle: TerminalStyleCode) -> String {
guard String.isColorizationEnabled else { return self }
guard StringColorizationManager.shared.isColorizationEnabled else { return self }
let str = self.replacingOccurrences(of: TerminalStyle.reset.open, with: TerminalStyle.reset.open + codeStyle.open)

return codeStyle.open + str + TerminalStyle.reset.open
Expand Down Expand Up @@ -521,3 +528,20 @@ public enum TerminalColor: UInt8 {
return ("\u{001B}[48;5;\(self.rawValue)m", TerminalStyle.reset.open)
}
}

// MARK: - Colorization Manager
internal final class StringColorizationManager: @unchecked Sendable {
internal static let shared = StringColorizationManager()

private let queue = DispatchQueue(label: "com.github.mtynior.ColorizeSwift.StringColorizationManager")
private var _isColorizationEnabled = true

internal var isColorizationEnabled: Bool {
get {
return queue.sync { _isColorizationEnabled }
}
set {
queue.sync { _isColorizationEnabled = newValue }
}
}
}

0 comments on commit 4e7daa1

Please sign in to comment.