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[39;39m")
or even
NSFileHandle.fileHandleWithStandardOutput().writeData("\033[31;32mhey\033[39;39m".dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)!)
It works when I use a simple echo in php (the text is displayed in green) but is there a reason it doesn't work in a Swift command line tool?
Thanks!
Based on @cyt answer, I've written a simple enum with these colors and also overloaded
+
operator so you can print using that enum.It's all up on Github, but it's really that simple:
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.
Hope it helps.
You can use Rainbow if you don't mind using it as a framework.
https://github.com/onevcat/Rainbow