This package provides basic constructs to generate attributed text and simple layouts for terminal output.
Run following example code snippets with Swift's REPL (on a terminal capable of interpreting ANSI escape sequences).
git clone https://github.com/gallinapassus/AttributedText
cd AttributedText
swift run --repl
Copy/paste the example code into REPL to see formatted output.
import AttributedText
typealias Text = AttributedText<DefaultAttributes>
let rgb = Text("R", .bold, .fgColor(.red)) +
Text("G", .bold, .fgColor(.green)) +
Text("B", .bold, .fgColor(.blue))
print(rgb.render())
// -> RGB (colored text)
NOTE: Github README.md markdown doesn't properly support coloring of text, hence output examples (below) will be missing colors.
Example 1 output:
RGB
typealias Table = AttributedTable<DefaultAttributes>
let source:[[Text]] = [
[Text("βΌοΈ", .fgColor(.red)), Text("red", .bold), Text("[255,0,0]")],
[Text("βΌοΈ", .fgColor(.green)), Text("green", .bold), Text("[0,255,0]")],
[Text("βΌοΈ", .fgColor(.blue)), Text("blue", .bold), Text("[0,0,255]")],
]
let ascii_table = Table(table: source)
print(ascii_table.render())
// -> Colorful table with default ascii character frame
Example 2 output (colors missing):
+-+-----+---------+ |βΌοΈ|red |[255,0,0]| +-+-----+---------+ |βΌοΈ|green|[0,255,0]| +-+-----+---------+ |βΌοΈ|blue |[0,0,255]| +-+-----+---------+
let columns:[Table.Column<DefaultAttributes>] = [
Table.Column(Table.Header("Color", alignment: .bottomCenter, wrapping: .word), alignment: .middleCenter),
Table.Column(Table.Header("Name", alignment: .bottomLeft)),
Table.Column("Component values", width: 10, alignment: .middleRight)
]
let title = Table.Title(rgb + " base color component values", alignment: .middleCenter, wrapping: .word)
let fancy_table = Table(table: source,
title: title,
columns: columns,
frameElements: .lightDimmed,
frameRenderingOptions: .all)
print(fancy_table.render())
// -> Fancy table with title, headers, alignment & word wrapping
Example 3 output (colors missing):
ββββββββββββββββββββββββ β RGB base color β β component values β βββββββ¬ββββββ¬βββββββββββ€ β β βComponent β βColorβName βvalues β βββββββΌββββββΌβββββββββββ€ β βΌοΈ βred β [255,0,0]β βββββββΌββββββΌβββββββββββ€ β βΌοΈ βgreenβ [0,255,0]β βββββββΌββββββΌβββββββββββ€ β βΌοΈ βblue β [0,0,255]β βββββββ΄ββββββ΄βββββββββββ
typealias Document = AttributedDocument<DefaultAttributes>
let wiki = Text("The RGB color model is an additive color model in which red, green, and blue light are added together in various ways to reproduce a broad array of colors. The name of the model comes from the initials of the three additive primary colors, red, green, and blue.", .dim)
let doc = Document(Text("https://en.wikipedia.org/wiki/RGB_color_model", .fgColor(.blue)))
doc.append(attributed: wiki, width: 64, wrapping: .word)
.append(fancy_table)
print(doc.render())
// -> Wikipedia intro + fancy table
Example 4 output (colors missing):
https://en.wikipedia.org/wiki/RGB_color_model The RGB color model is an additive color model in which red, green, and blue light are added together in various ways to reproduce a broad array of colors. The name of the model comes from the initials of the three additive primary colors, red, green, and blue. ββββββββββββββββββββββββ β RGB base color β β component values β βββββββ¬ββββββ¬βββββββββββ€ β β βComponent β βColorβName βvalues β βββββββΌββββββΌβββββββββββ€ β βΌοΈ βred β [255,0,0]β βββββββΌββββββΌβββββββββββ€ β βΌοΈ βgreenβ [0,255,0]β βββββββΌββββββΌβββββββββββ€ β βΌοΈ βblue β [0,0,255]β βββββββ΄ββββββ΄βββββββββββ
AttributedText
package follows semantic versioning scheme https://semver.org/spec/v2.0.0.html. Current version is still at major version 0 (zero), which means "initial development". Anything may change at any time and the public API should not be considered stable.
ExpressibleByStringLiteral
ExpressibleByArrayLiteral
Equatable
Collection
BidirectionalCollection
RangeReplaceableCollection
TextOutputStreamable
Rendable
Please note that your terminal app may, or may not, support some of the above mentioned colors & traits.
AttributedText
uses it's own internal implementation for generating the attributed output (DefaultAttributes
), but it can be completely replaced with custom implementation if neededpublic protocol AttributeProtocol : Equatable {
static func render(_ attributed:AttributedText<Self>) -> String
}
TextOutputStreamable
Rendable
DefaultAttributes
can be replaced with custom implementation. For example DefaultAttributes can be replaced with your favourite Swift attributed text package and implement a specific renderer for itDefaultRenderer
can be replaced with custom implementationCollection
RandomAccessCollection
BidirectionalCollection
RangeReplaceableCollection
TextOutputStreamable
Rendable
Attributes
within a single document. Workaround: Use multiple documents.// Not supported:
// +-+-+-+ +-+-+-+
// |a|b|c| |1|2|3|
// +-+-+-+ +-+-+-+
// |d|e|f| |4|5|6|
// +-+-+-+ +-+-+-+
// |7|8|9|
// +-+-+-+
link |
Stars: 0 |
Last commit: 3 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics