feat: Working on documentation
This commit is contained in:
@@ -2,36 +2,68 @@ import Rainbow
|
||||
|
||||
public extension TextNode {
|
||||
|
||||
/// Apply coloring to a text node.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - color: The color to apply to the text node.
|
||||
@inlinable
|
||||
func color(_ color: NamedColor) -> some TextNode {
|
||||
textStyle(.color(color))
|
||||
textStyle(_ColorTextStyle(.foreground(.named(color))))
|
||||
}
|
||||
|
||||
/// Apply coloring to a text node.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - bit8: The color to apply to the text node.
|
||||
@inlinable
|
||||
func color(_ bit8: UInt8) -> some TextNode {
|
||||
textStyle(.color(bit8: bit8))
|
||||
textStyle(_ColorTextStyle(.foreground(.bit8(bit8))))
|
||||
}
|
||||
|
||||
/// Apply coloring to a text node using RGB.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - red: The red color to apply to the text node.
|
||||
/// - green: The green color to apply to the text node.
|
||||
/// - blue: The blue color to apply to the text node.
|
||||
@inlinable
|
||||
func color(_ red: UInt8, _ green: UInt8, _ blue: UInt8) -> some TextNode {
|
||||
textStyle(.color(rgb: (red, green, blue)))
|
||||
textStyle(_ColorTextStyle(.foreground(.bit24((red, green, blue)))))
|
||||
}
|
||||
|
||||
/// Apply background coloring to a text node.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - color: The color to apply to the text node.
|
||||
@inlinable
|
||||
func backgroundColor(_ name: NamedBackgroundColor) -> some TextNode {
|
||||
textStyle(.backgroundColor(name))
|
||||
func backgroundColor(_ color: NamedBackgroundColor) -> some TextNode {
|
||||
textStyle(_ColorTextStyle(.background(.named(color))))
|
||||
}
|
||||
|
||||
/// Apply background coloring to a text node.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - bit8: The color to apply to the text node.
|
||||
@inlinable
|
||||
func backgroundColor(_ bit8: UInt8) -> some TextNode {
|
||||
textStyle(.backgroundColor(bit8: bit8))
|
||||
textStyle(_ColorTextStyle(.background(.bit8(bit8))))
|
||||
}
|
||||
|
||||
/// Apply background coloring to a text node using RGB.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - red: The red color to apply to the text node.
|
||||
/// - green: The green color to apply to the text node.
|
||||
/// - blue: The blue color to apply to the text node.
|
||||
@inlinable
|
||||
func backgroundColor(_ red: UInt8, _ green: UInt8, _ blue: UInt8) -> some TextNode {
|
||||
textStyle(.backgroundColor(rgb: (red, green, blue)))
|
||||
textStyle(_ColorTextStyle(.background(.bit24((red, green, blue)))))
|
||||
}
|
||||
|
||||
/// Apply styles to a text node.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - styles: The styles to apply.
|
||||
@inlinable
|
||||
func textStyle<S: TextStyle>(_ styles: S...) -> some TextNode {
|
||||
styles.reduce(render()) { string, style in
|
||||
@@ -39,28 +71,49 @@ public extension TextNode {
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply a bold text style to a text node.
|
||||
@inlinable
|
||||
func bold() -> some TextNode { textStyle(.bold) }
|
||||
|
||||
/// Apply a dim text style to a text node.
|
||||
@inlinable
|
||||
func dim() -> some TextNode { textStyle(.dim) }
|
||||
|
||||
/// Apply an italic text style to a text node.
|
||||
@inlinable
|
||||
func italic() -> some TextNode { textStyle(.italic) }
|
||||
|
||||
/// Apply an underline text style to a text node.
|
||||
@inlinable
|
||||
func underline() -> some TextNode { textStyle(.underline) }
|
||||
|
||||
/// Apply a blink text style to a text node.
|
||||
@inlinable
|
||||
func blink() -> some TextNode { textStyle(.blink) }
|
||||
|
||||
/// Apply a strike-through text style to a text node.
|
||||
@inlinable
|
||||
func strikeThrough() -> some TextNode { textStyle(.strikeThrough) }
|
||||
|
||||
}
|
||||
|
||||
/// A general purpose way of styling a text node.
|
||||
///
|
||||
/// This is generally used for applying styling that can work with any text node.
|
||||
///
|
||||
/// Most of the time you will want to customize styles for a text node's type instead
|
||||
/// of using this.
|
||||
public protocol TextStyle: TextModifier where Content == TextStyleConfiguration {}
|
||||
|
||||
/// A type-erased text node that can be used for creating
|
||||
/// custom styles.
|
||||
///
|
||||
/// This is generally used to change the style of text nodes as
|
||||
/// a whole, such as applying text colors or styling such as `bold`.
|
||||
///
|
||||
/// Most of the time you will want to customize styles for a text node's
|
||||
/// type, instead of using this.
|
||||
///
|
||||
public struct TextStyleConfiguration {
|
||||
public let node: any TextNode
|
||||
|
||||
@@ -70,7 +123,7 @@ public struct TextStyleConfiguration {
|
||||
}
|
||||
}
|
||||
|
||||
public extension TextStyle where Self == StyledText {
|
||||
public extension TextStyle where Self == _StyledText {
|
||||
|
||||
@inlinable
|
||||
static var bold: Self { .init(.bold) }
|
||||
@@ -91,40 +144,8 @@ public extension TextStyle where Self == StyledText {
|
||||
static var strikeThrough: Self { .init(.strikethrough) }
|
||||
}
|
||||
|
||||
public extension TextStyle where Self == ColorTextStyle {
|
||||
|
||||
@inlinable
|
||||
static func color(_ name: NamedColor) -> Self {
|
||||
.init(.foreground(.named(name)))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
static func color(bit8: UInt8) -> Self {
|
||||
.init(.foreground(.bit8(bit8)))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
static func color(rgb: RGB) -> Self {
|
||||
.init(.foreground(.bit24(rgb)))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
static func backgroundColor(_ name: NamedBackgroundColor) -> Self {
|
||||
.init(.background(.named(name)))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
static func backgroundColor(bit8: UInt8) -> Self {
|
||||
.init(.background(.bit8(bit8)))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
static func backgroundColor(rgb: RGB) -> Self {
|
||||
.init(.background(.bit24(rgb)))
|
||||
}
|
||||
}
|
||||
|
||||
public struct ColorTextStyle: TextStyle {
|
||||
// swiftlint:disable type_name
|
||||
public struct _ColorTextStyle: TextStyle {
|
||||
@usableFromInline
|
||||
enum Style {
|
||||
case foreground(ColorType)
|
||||
@@ -150,7 +171,7 @@ public struct ColorTextStyle: TextStyle {
|
||||
}
|
||||
}
|
||||
|
||||
public struct StyledText: TextStyle {
|
||||
public struct _StyledText: TextStyle {
|
||||
@usableFromInline
|
||||
let style: Style
|
||||
|
||||
@@ -164,3 +185,5 @@ public struct StyledText: TextStyle {
|
||||
content.node.render().applyingStyle(style)
|
||||
}
|
||||
}
|
||||
|
||||
// swiftlint:enable type_name
|
||||
|
||||
Reference in New Issue
Block a user