颜色输出与Swift命令行工具 [英] Color ouput with Swift command line tool

查看:330
本文介绍了颜色输出与Swift命令行工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift编写命令行工具,而且在我的shell中显示颜色时遇到问题。我使用以下代码:

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")

或甚至

NSFileHandle.fileHandleWithStandardOutput().writeData("\033[31;32mhey\033[39;39m".dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)!)

当我在php中使用简单的echo(文本显示为绿色)时,它工作,但有一个原因,它不工作在Swift命令行工具?

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?

谢谢!

推荐答案

在unicode支持。这将使用反斜线无效。所以我使用带有\u {}语法的颜色代码。这是一个println代码,在终端上完美工作。

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")

希望它有帮助。

这篇关于颜色输出与Swift命令行工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆