Color ouput with Swift command line tool

后端 未结 6 1828
萌比男神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 05:56

    Swift has built in unicode support. This invalidates using of back slash. So that I use color codes with "\u{}" syntax. Here is a println code which works perfectly on terminal.

    // \u{001B}[\(attribute code like bold, dim, normal);\(color code)m
    
    // Color codes
    // black   30
    // red     31
    // green   32
    // yellow  33
    // blue    34
    // magenta 35
    // cyan    36
    // white   37
    
    println("\u{001B}[0;33myellow")
    

    Hope it helps.

提交回复
热议问题