Terminal string styling for Swift.
You can use The Swift Package Manager to install ColorizeSwift by adding it to your Package.swift file:
// 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"]),
]
)You can also manually add ColorizeSwift to you project:
- Download
ColorizeSwift.swiftfile, - Drag
ColorizeSwift.swiftinto you project's tree.
Please use SPM or add ColorizeSwift.swift file manually to your project.
If you use CocoaPods, you can still use version 1.2, but it does not have any updates like support for Swift 6 strict concurrency!
You can install ColorizeSwift 1.2 by adding it to your Podfile:
platform :ios, '9.0'
use_frameworks!
target 'MyApp' do
pod 'ColorizeSwift'
endRun pods install to integrate pods with your project.
You can run sample application:
- Open Terminal and go to
Examplefolder. - Run
./build.shscript to build sample application. - Run
./example pacmanto launch sample.
Available samples:
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())bold()dim()italic()(not widely supported)underline()reverse()hidden()strikethrough()(not widely supported)reset()
black()red()green()yellow()blue()magenta()cyan()lightGray()darkGray()lightRed()lightGreen()lightYellow()lightBlue()lightMagenta()lightCyan()white()
onBlack()onRed()onGreen()onYellow()onBlue()onMagenta()onCyan()onLightGray()onDarkGray()onLightRed()onLightGreen()onLightYellow()onLightBlue()onLightMagenta()onLightCyan()onWhite()
You can also use 256 colors, but keep in mind that not all Terminal clients support them.
foregroundColor(color: TerminalColor)backgroundColor(color: TerminalColor)colorize(foreground: TerminalColor, background: TerminalColor)
You can access 256 colors using TerminalColor enumeration.
Sometimes you only need the open code for a modifier. You can access them using TerminalStyle enum:
TerminalStyle.bold.open // "\u{001B}[1m"
TerminalStyle.bold.close // "\u{001B}[22m"For 256 colors use:
TerminalColor.red.foregroundStyleCode().open //"\u{001B}[38;5;9m"
TerminalColor.red.backgroundStyleCode().open //"\u{001B}[48;5;9m"To get string without any colorization use uncolorized() method:
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"Colorization can be disabled globally:
String.isColorizationEnabled = false // Default: trueYou can use this to support a command line option (./example --no-color):
String.isColorizationEnabled = !CommandLine.arguments.contains("--no-color")To disable colorization when program is running in a pipe (./example | wc) or when not writing to stdout (./example > output.txt):
String.isColorizationEnabled = (isatty(fileno(stdout)) == 1)ColorizeSwift is released under the MIT license. See LICENSE for details.