使用Swift命令行工具进行颜色输出

Rom*_*let 17 command colors xterm command-line-interface swift

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

println("\033[31;32mhey\033[39;39m")
Run Code Online (Sandbox Code Playgroud)

甚至

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

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

谢谢!

小智 31

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")
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.

  • Xcode控制台不打印颜色你不安装XcodeColors插件 - > https://github.com/robbiehanson/XcodeColors (2认同)

Die*_*che 25

基于@cyt的答案,我用这些颜色编写了一个简单的枚举,并且还重载了+运算符,因此您可以使用该枚举进行打印.

这完全取决于Github,但它真的很简单:

enum ANSIColors: 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"

    func name() -> String {
        switch self {
        case .black: return "Black"
        case .red: return "Red"
        case .green: return "Green"
        case .yellow: return "Yellow"
        case .blue: return "Blue"
        case .magenta: return "Magenta"
        case .cyan: return "Cyan"
        case .white: return "White"
        }
    }

    static func all() -> [ANSIColors] {
        return [.black, .red, .green, .yellow, .blue, .magenta, .cyan, .white]
    }
}

func + (let left: ANSIColors, let right: String) -> String {
    return left.rawValue + right
}

// END


// Demo:

for c in ANSIColors.all() {
    println(c + "This is printed in " + c.name())
}
Run Code Online (Sandbox Code Playgroud)

  • ``\ u {001B} [0; 0m"`重置为默认颜色. (7认同)
  • 顺便说一句,Swift 现在提供了一个“CaseIterable”协议,它为您的枚举合成一个“allCases”方法,该方法返回所有情况的数组。尽管它与具有原始值的情况兼容,但它不适用于具有关联值的情况。请参阅 https://developer.apple.com/documentation/swift/caseiterable (2认同)

Unc*_*rks 9

如果您不介意将其用作框架,则可以使用Rainbow.

import Rainbow
print("Red text".red)
print("Yellow background".onYellow)
print("Light green text on white background".lightGreen.onWhite)
Run Code Online (Sandbox Code Playgroud)

https://github.com/onevcat/Rainbow


Cha*_*tas 7

结合@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)"
Run Code Online (Sandbox Code Playgroud)

  • 感谢您还添加了`default` (2认同)

mgy*_*yky 5

优雅的解决方案:

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"
}
Run Code Online (Sandbox Code Playgroud)

演示

print(Colors.yellow + "Please Enter the Output Directory Name:" + Colors.reset)
Run Code Online (Sandbox Code Playgroud)

或者

print(Colors.yellow + "Please " + Colors.blue + "Enter " + Colors.magenta + "the Output Directory Name:" + Colors.reset)
Run Code Online (Sandbox Code Playgroud)