ColorizeSwift

Swift License

Swift 的终端字符串样式。

集成

Swift Package Manager (SPM)

您可以使用 Swift Package Manager 安装 ColorizeSwift,方法是将其添加到您的 Package.swift 文件中

// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "MyLibrary",
    products: [
        .library(name: "MyLibrary", targets: ["MyLibrary"]),
    ],
    dependencies: [
         .package(url: "https://github.com/mtynior/ColorizeSwift.git", from: "1.5.0"),
    ],
    targets: [
        .target(name: "MyLibrary", dependencies: ["ColorizeSwift"]),
        .testTarget(name: "MyLibraryTests", dependencies: ["MyLibrary", "ColorizeSwift"]),
    ]
)

手动

您也可以手动将 ColorizeSwift 添加到您的项目中

  1. 下载 ColorizeSwift.swift 文件,
  2. ColorizeSwift.swift 拖到您的项目树中。

CocoaPods

从版本 1.5 开始,不再支持 CocoaPods!

请使用 SPM 或手动将 ColorizeSwift.swift 文件添加到您的项目。如果您使用 CocoaPods,您仍然可以使用版本 1.2,但它没有任何更新,例如对 Swift 6 严格并发的支持!

您可以通过将其添加到您的 Podfile 来安装 ColorizeSwift 1.2

platform :ios, '9.0'
use_frameworks!

target 'MyApp' do
	pod 'ColorizeSwift'
end

运行 pods install 以将 pod 与您的项目集成。

示例

您可以运行示例应用程序

  1. 打开终端并转到 Example 文件夹。
  2. 运行 ./build.sh 脚本以构建示例应用程序。
  3. 运行 ./example pacman 以启动示例。

可用示例

  1. styles - 打印可用样式

    Example - Styles

  2. f1 - 打印 F1 赛车

    Example - F1

  3. pacman - 打印吃豆人

    Example - Pacman

  4. mario - 打印马里奥

    Example - Mario

用法

print("Normal")
print("Bold".bold())
print("Dim".dim())
print("Italic".italic())
print("Underline".underline())
print("Blink".blink())
print("Reverse".reverse())
print("hidden".hidden())
print("strikethrough".strikethrough())
print("Red".red())
print("On yellow".onYellow())
print("256 foreground".foregroundColor(.orange1))
print("226 background".backgroundColor(.orange1))
print("Awful combination".colorize(.yellow, background: .red))
    
let nested = "with a blue substring".blue().underline()
print("A bold, green line \(nested) that becomes bold and green again".green().bold())

样式

修饰符

前景色

背景色

256 色

您还可以使用 256 色,但请记住并非所有终端客户端都支持它们。

可用颜色

您可以使用 TerminalColor 枚举访问 256 种颜色。

256 Colors

转义码

有时您只需要修饰符的 open code。您可以使用 TerminalStyle 枚举访问它们

TerminalStyle.bold.open // "\u{001B}[1m"
TerminalStyle.bold.close  // "\u{001B}[22m"

对于 256 色,请使用

TerminalColor.red.foregroundStyleCode().open //"\u{001B}[38;5;9m"
TerminalColor.red.backgroundStyleCode().open //"\u{001B}[48;5;9m"

取消着色

要获取没有任何着色的字符串,请使用 uncolorized() 方法

let styledString = "Awful combination".colorize(.yellow, background: .red) // \u{001B}[48;5;9m\u{001B}[38;5;11mAwful combination\u{001B}[0m\u{001B}[48;5;9m\u{001B}[0m
let withoutStyles = styledString.uncolorized() // "Awful combination"

禁用着色

可以全局禁用着色

String.isColorizationEnabled = false // Default: true

您可以使用此方法来支持命令行选项 (./example --no-color)

String.isColorizationEnabled = !CommandLine.arguments.contains("--no-color")

在程序在管道中运行时 (./example | wc) 或未写入 stdout (./example > output.txt) 时禁用着色

String.isColorizationEnabled = (isatty(fileno(stdout)) == 1)

致谢

许可证

ColorizeSwift 是在 MIT 许可证下发布的。有关详细信息,请参阅 LICENSE。