我正在使用 Swift 编写命令行工具,但在我的 shell 中显示颜色时遇到了问题。我正在使用以下代码:
println("\033[31;32mhey\033[39;39m")
甚至
NSFileHandle.fileHandleWithStandardOutput().writeData("\033[31;32mhey\033[39;39m".dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)!)
当我在 php 中使用一个简单的 echo 时它可以工作(文本显示为绿色)但是它在 Swift 命令行工具中不起作用有什么原因吗?
谢谢!
最佳答案
Swift 内置了 unicode 支持。这会使反斜杠的使用无效。所以我使用带有“\u{}”语法的颜色代码。这是一个在终端上完美运行的 println 代码。
// \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")
希望对您有所帮助。
关于swift - 使用 Swift 命令行工具进行颜色输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27807925/