SwiftyTerminalColors allows you to change colors and fonts of your CLI outputs in a familiar, SwiftUI-like way.
print("I will be bright green, bold and in italics".color(.greenBright).font([.bold, .italic]))
The only tested way of using this package is through Xcode. To do so:
- Copy the address of this repository
- In Xcode, click
File → Add Package Dependencies…
- In the top right of the sheet that opens, paste the address you copied in step 1 into the field called
Search or Enter Package URL
. Xcode will find this package, calledswiftyterminalcolors
- Click
Add Package
And it's done!
The package provides three extensions on String
, called .color()
, .background()
and .font()
. As their names suggest, .color()
lets you change the color of a particular string, .background()
the background, and .font()
accepts a set of possible fonts.
To change the formatting of a particular string, add .color()
, .background()
or .font()
(in any order) to the end of it:
print("I will be green".color(.green))
print("I will be blue".background(.blue))
print("I will be bold".font([.bold, .italic]).background(.red))
This package also supports formatting only portions of strings. To do so, you can use either string interpolation or concatenation.
print("\("I will be bold".font([.bold])) \("And I will be in italics")".font([.italics]))
print("I will be bold".font([.bold]) + " And I will be in italics".font([.italics]))
Here is how I'm using this package in my app RenameEverything to display a colorful help:
extension String
{
static let helpText: String = """
\("\("Rename".color(.cyan))\("Everything".color(.blue))"): A tool for randomly mass-renaming files in a folder
\("Usage:".font([.bold]))
renameEverything [optional arguments] [folder location]
\("Optional Arguments:".font([.bold]))
--renameType: Specify how files should be renamed
\("Possible types:".font([.italic]).color(.blue))
randomUUID: Rename files to random UUIDs
randomWord: Rename files to random words
\("Example usage:".font([.italic]).color(.blue))
renameEverything --renameType randomUUID [folder location]
\("Will rename all files in the specified folder to random UUIDs".font([.italic]))
renameEverything --renameType randomWord [folder location]
\("Will rename all files in the specified folder to random English words".font([.italic]))
\("Options:".font([.bold]))
--help: Show this help
"""
}
These features will be added in the future:
- Support for RGB colors
- Any other ideas you might have :)
This library is licensed under CC BY-ND 4.0 DEED