Color ouput with Swift command line tool

后端 未结 6 1834
萌比男神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

    Here is my solution:

    struct Colors {
        static let reset = "\u{001B}[0;0m"
        static let black = "\u{001B}[0;30m"
        static let red = "\u{001B}[0;31m"
        static let green = "\u{001B}[0;32m"
        static let yellow = "\u{001B}[0;33m"
        static let blue = "\u{001B}[0;34m"
        static let magenta = "\u{001B}[0;35m"
        static let cyan = "\u{001B}[0;36m"
        static let white = "\u{001B}[0;37m"
    }
    

    Demo

    print(Colors.yellow + "Please Enter the Output Directory Name:" + Colors.reset)
    

    or

    print(Colors.yellow + "Please " + Colors.blue + "Enter " + Colors.magenta + "the Output Directory Name:" + Colors.reset)
    

提交回复
热议问题