使用Swift命令行工具进行颜色输出
我正在用Swift编写一个命令行工具,但在shell中显示颜色时遇到了问题。我正在使用以下代码:使用Swift命令行工具进行颜色输出,swift,colors,command,command-line-interface,xterm,Swift,Colors,Command,Command Line Interface,Xterm,我正在用Swift编写一个命令行工具,但在shell中显示颜色时遇到了问题。我正在使用以下代码: println("\033[31;32mhey\033[39;39m") 甚至 NSFileHandle.fileHandleWithStandardOutput().writeData("\033[31;32mhey\033[39;39m".dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)!) 当我在php
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")
希望能有所帮助。基于@cyt answer,我用这些颜色编写了一个简单的枚举,还重载了
+
操作符,以便您可以使用该枚举进行打印
就这些,但其实很简单:
enum ANSIColors:String{
黑色大小写=“\u{001B}[0;30m”
大小写红色=“\u{001B}[0;31m”
绿色大小写=“\u{001B}[0;32m”
大小写黄色=“\u{001B}[0;33m”
大小写蓝色=“\u{001B}[0;34m”
大小写洋红色=“\u{001B}[0;35m”
case cyan=“\u{001B}[0;36m”
大小写白色=“\u{001B}[0;37m”
func name()->字符串{
切换自身{
黑色:返回“黑色”
红色:返回“红色”
绿色:返回“绿色”
黄色:返回“黄色”
案例。蓝色:返回“蓝色”
案例.洋红:返回“洋红”
案例.青色:返回“青色”
白色:返回“白色”
}
}
静态func all()->[ANSIColors]{
返回[.黑色、.红色、.绿色、.黄色、.蓝色、.品红色、.青色、.白色]
}
}
func+(左:ANSIColors,右:String)->String{
返回left.rawValue+right
}
//结束
//演示:
对于ANSIColors.all()中的c{
println(c+)这是在“+c.name()中打印的)
}
如果您不介意将Rainbow用作框架,可以使用它
import Rainbow
print("Red text".red)
print("Yellow background".onYellow)
print("Light green text on white background".lightGreen.onWhite)
结合@Diego的一些答案,您可以使用Swift新的
DefaultStringInterpolation
结构将此装饰扩展到字符串文本中——
enum ASCIIColor: String {
case black = "\u{001B}[0;30m"
case red = "\u{001B}[0;31m"
case green = "\u{001B}[0;32m"
case yellow = "\u{001B}[0;33m"
case blue = "\u{001B}[0;34m"
case magenta = "\u{001B}[0;35m"
case cyan = "\u{001B}[0;36m"
case white = "\u{001B}[0;37m"
case `default` = "\u{001B}[0;0m"
}
extension DefaultStringInterpolation {
mutating func appendInterpolation<T: CustomStringConvertible>(_ value: T, color: ASCIIColor) {
appendInterpolation("\(color.rawValue)\(value)\(ASCIIColor.default.rawValue)")
}
}
// USAGE:
// "\("only this string will be green!", color: .green)"
enum ascicolor:字符串{
黑色大小写=“\u{001B}[0;30m”
大小写红色=“\u{001B}[0;31m”
绿色大小写=“\u{001B}[0;32m”
大小写黄色=“\u{001B}[0;33m”
大小写蓝色=“\u{001B}[0;34m”
大小写洋红色=“\u{001B}[0;35m”
case cyan=“\u{001B}[0;36m”
大小写白色=“\u{001B}[0;37m”
大小写`default`=“\u{001B}[0;0m”
}
扩展插值{
变异函数插值(u值:T,颜色:asciocolor){
appendInterpolation(“\(color.rawValue)\(value)\(ascicolor.default.rawValue)”)
}
}
//用法:
//\(“只有此字符串为绿色!”,颜色:。绿色)
扩展后,我们可以合并Rainbow的功能,如中所述,而无需使用简单的字符串
扩展导入框架本身:
enum ANSIColor:字符串{
typealias This=ANSIColor
黑色大小写=“\u{001B}[0;30m”
大小写红色=“\u{001B}[0;31m”
绿色大小写=“\u{001B}[0;32m”
大小写黄色=“\u{001B}[0;33m”
大小写蓝色=“\u{001B}[0;34m”
大小写洋红色=“\u{001B}[0;35m”
case cyan=“\u{001B}[0;36m”
大小写白色=“\u{001B}[0;37m”
大小写`default`=“\u{001B}[0;0m”
静态变量值:[此]{
返回[.黑色、.红色、.绿色、.黄色、.蓝色、.品红色、.青色、.白色、.默认值]
}
静态变量名称:[此:字符串]={
返回[
.黑色:“黑色”,
红色:“红色”,
.绿色:“绿色”,
黄色:“黄色”,
蓝色:“蓝色”,
.洋红:“洋红”,
.cyan:“cyan”,
白色:“白色”,
.default:“默认”,
]
}
变量名称:String{
返回此。名称[自身]??“未知”
}
静态func+(lhs:This,rhs:String)->String{
返回lhs.rawValue+rhs
}
静态func+(lhs:String,rhs:This)->String{
返回lhs+rhs.rawValue
}
}
扩展字符串{
func有色(uColor:ANSIColor)->字符串{
返回颜色+self+ANSIColor.default
}
黑色:字符串{
返回彩色(.黑色)
}
变量红色:字符串{
返回彩色(.red)
}
绿色变量:字符串{
返回颜色(.绿色)
}
黄色变量:字符串{
返回彩色(.黄色)
}
蓝色:字符串{
返回颜色(.blue)
}
品红色:字符串{
返回彩色(.洋红)
}
var-cyan:String{
返回彩色(.cyan)
}
白色:字符串{
返回彩色(.白色)
}
}
以下是我的解决方案:
演示
或
我不适用于我的swift 1.2 iOS编程,我看不到输出中的颜色Xcode控制台中不打印颜色您不安装Xcode颜色插件->有没有办法将部分彩色字符串和其余字符串设置为默认控制台颜色?在这里查看这个惊人的解决方案:
“\u{001B}[0;0m”
重置为默认颜色。顺便说一句,Swift现在提供了一个caseitrable
协议,它为您的枚举合成一个allCases
方法,该方法返回所有案例的数组。它不适用于具有关联值的案例,尽管它与具有原始值的案例兼容。请参阅,感谢您还添加了default
struct Colors {
static let reset = "\u{001B}[0;0m"
static let black = "\u{001B}[0;30m"
static let red = "\u{001B}[0;31m"
static let green = "\u{001B}[0;32m"
static let yellow = "\u{001B}[0;33m"
static let blue = "\u{001B}[0;34m"
static let magenta = "\u{001B}[0;35m"
static let cyan = "\u{001B}[0;36m"
static let white = "\u{001B}[0;37m"
}
print(Colors.yellow + "Please Enter the Output Directory Name:" + Colors.reset)
print(Colors.yellow + "Please " + Colors.blue + "Enter " + Colors.magenta + "the Output Directory Name:" + Colors.reset)