Xcode上のコンソールには色をつけて表示することができませんが、ターミナルで実行するCLIには色をつけて表示することができます。
実装
extension String {
enum ConsoleColor: Int {
case black = 30
case red
case green
case yellow
case blue
case magenta
case cyan
case white
}
enum ConsoleStyle: Int {
case `default`
case bold
case thin
case italic
case underline
case blink
case fastBrink
case reverseFrontBack
case hidden
case cancel
}
func colored(_ color: ConsoleColor, style: ConsoleStyle = .default) -> String {
"\u{001B}[\(style.rawValue);\(color.rawValue)m\(self)\u{001B}[0m"
}
}
使い方
let text = "This is bold yellow text\n".colored(.yellow, style: .bold)
+ "This is blue background text\n".colored(.green, style: .reverseFrontBack)
+ "This is default color text"
print(text)
結果
参考までに、こちらのリポジトリで使用しております。
https://github.com/s2mr/memo-cli