Console Color is lightweight package for creating colored strings when printing to standard output.
import ConsoleColor
var greeting = "Hello, world!"
greeting.applyCCFormat(textColor: .blue)
debugPrint(greeting)
// Prints "\u{1B}[34mHello, world!\u{1B}[0m"
print(greeting)
// Prints "Hello, world!" in blue
Include this line to your dependencies in Package.swift
:
let package = Package(
// ...
dependencies: [
.package(
url: "https://github.com/deus-x-mackina/console-color.git",
from: "0.1.1"),
]
// ...
)
If cloning the repo manually, you have a bit more flexibility with the things you can play around with.
git clone https://github.com/deus-x-mackina/console-color.git
cd console-color
I have added a small executable demo to preview what the colors will look like when using this package.
swift run console-color-demo
OVERVIEW: An executable demo for the ConsoleColors package.
USAGE: console-colors-demo <message> [-t <text color>] [-b <background color>]
[-d <decoration> ...]
ARGUMENTS:
<message> The message to format.
OPTIONS:
-t <text color> The color to apply to the message.
One of black, red, green, yellow, blue, magenta, cyan, or white.
Optionally add suffix "-bright". For example, red-bright.
-b <background color> The color to apply to the message's background.
One of black, red, green, yellow, blue, magenta, cyan, or white.
Optionally add suffix "-bright". For example, red-bright.
-d <decoration> The decoration(s) to apply to the message.
Zero or more of bold, italic, underline, invert, or strikethrough.
--version Show the version.
-h, --help Show help information.
For example, to print italicized, underlined red text on a blue background, run:
swift run console-color-demo "hello world\!" -t red -b blue -d italic -d underline
The package exports a few extensions on String
, with mutating and non-mutating
pairs.
// Mutating
applyCCFormat(textColor:background:decorations:addResetSequence:)
applyCCFormat(eightBitTextColor:eightBitBackground:decorations:addResetSequence:)
clearCCFormat()
// Non-mutating
applyingCCFormat(textColor:background:decorations:addResetSequence:)
applyingCCFormat(eightBitTextColor:eightBitBackground:decorations:addResetSequence:)
clearingCCFormat()
The function signatures with eightBitTextColor
and eightBitBackground
allow
you to select precisely which color is applied to the text by supplying RGB
values as a tuple. For example:
let greeting = "hello world".applyingCCFormat(eightBitTextColor: (255, 255, 0))
The input tuple also supports naming of each integer value.
let greeting = "hello world".applyingCCFormat(
eightBitTextColor: (red: 255, green: 255, blue: 0))
The other function signatures accept an enumerable type as input. Check the source files for foreground colors, background colors colors, and decorations.
The function ccPrint
allows you to merely print a colored message without
needing to modify the input. ccPrint
has two signatures:
ccPrint(_:textColor:background:decorations:separator:terminator:)
ccPrint(_:eightBitTextColor:eightBitBackground:decorations:separator:terminator:)
The ccPrint
parameters take in the same input as the String
extensions do.
For example:
ccPrint("Hello world!", eightBitTextColor: (255, 255, 0))