Color ouput with Swift command line tool

后端 未结 6 1886
萌比男神i
萌比男神i 2021-02-01 05:21

I\'m writing a command line tool with Swift and I\'m having trouble displaying colors in my shell. I\'m using the following code:

println(\"\\033[31;32mhey\\033         


        
6条回答
  •  终归单人心
    2021-02-01 06:01

    Expanding upon Diego Freniche's answer we can incorporate the functionality of Rainbow, as referenced in Uncharted Works's Answer, without needing to import the framework itself using a simple String extension:

    enum ANSIColor: String {
    
        typealias This = ANSIColor
    
        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"
    
        static var values: [This] {
            return [.black, .red, .green, .yellow, .blue, .magenta, .cyan, .white, .default]
        }
    
        static var names: [This: String] = {
            return [
                .black: "black",
                .red: "red",
                .green: "green",
                .yellow: "yellow",
                .blue: "blue",
                .magenta: "magenta",
                .cyan: "cyan",
                .white: "white",
                .default: "default",
            ]
        }
    
        var name: String {
            return This.names[self] ?? "unknown"
        }
    
        static func + (lhs: This, rhs: String) -> String {
            return lhs.rawValue + rhs
        }
    
        static func + (lhs: String, rhs: This) -> String {
            return lhs + rhs.rawValue
        }
    
    }
    
    extension String {
    
        func colored(_ color: ANSIColor) -> String {
            return color + self + ANSIColor.default
        }
    
        var black: String {
            return colored(.black)
        }
    
        var red: String {
            return colored(.red)
        }
    
        var green: String {
            return colored(.green)
        }
    
        var yellow: String {
            return colored(.yellow)
        }
    
        var blue: String {
            return colored(.blue)
        }
    
        var magenta: String {
            return colored(.magenta)
        }
    
        var cyan: String {
            return colored(.cyan)
        }
    
        var white: String {
            return colored(.white)
        }
    
    }
    

提交回复
热议问题