feat: Updating text style modifiers
This commit is contained in:
@@ -130,10 +130,9 @@ public struct DefaultExampleSectionStyle<Style: ExampleStyle>: ExampleSectionSty
|
||||
HStack {
|
||||
content.title
|
||||
.color(.yellow)
|
||||
.textStyle(.bold)
|
||||
.bold()
|
||||
|
||||
content.label
|
||||
.textStyle(.italic)
|
||||
content.label.italic()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
import Rainbow
|
||||
|
||||
public extension TextNode {
|
||||
@inlinable
|
||||
func color(_ color: NamedColor) -> some TextNode {
|
||||
modifier(ColorModifier(color: color))
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
struct ColorModifier<Content: TextNode>: NodeModifier {
|
||||
@usableFromInline
|
||||
let color: NamedColor
|
||||
|
||||
@usableFromInline
|
||||
init(color: NamedColor) {
|
||||
self.color = color
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
func render(content: Content) -> some TextNode {
|
||||
content.render().applyingColor(color)
|
||||
}
|
||||
}
|
||||
124
Sources/CliDocCore/Modifiers/TextStyle.swift
Normal file
124
Sources/CliDocCore/Modifiers/TextStyle.swift
Normal file
@@ -0,0 +1,124 @@
|
||||
import Rainbow
|
||||
|
||||
public extension TextNode {
|
||||
|
||||
@inlinable
|
||||
func color(_ color: NamedColor) -> some TextNode {
|
||||
textStyle(.color(color))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
func color(_ bit8: UInt8) -> some TextNode {
|
||||
textStyle(.color(bit8: bit8))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
func color(red: UInt8, green: UInt8, blue: UInt8) -> some TextNode {
|
||||
textStyle(.color(rgb: (red, green, blue)))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
func textStyle<S: TextStyle>(_ styles: S...) -> some TextNode {
|
||||
styles.reduce(render()) { string, style in
|
||||
style.render(content: string).render()
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
func bold() -> some TextNode { textStyle(.bold) }
|
||||
|
||||
@inlinable
|
||||
func dim() -> some TextNode { textStyle(.dim) }
|
||||
|
||||
@inlinable
|
||||
func italic() -> some TextNode { textStyle(.italic) }
|
||||
|
||||
@inlinable
|
||||
func underline() -> some TextNode { textStyle(.underline) }
|
||||
|
||||
@inlinable
|
||||
func blink() -> some TextNode { textStyle(.blink) }
|
||||
|
||||
@inlinable
|
||||
func strikeThrough() -> some TextNode { textStyle(.strikeThrough) }
|
||||
|
||||
}
|
||||
|
||||
// TODO: Remove string restraint.
|
||||
public protocol TextStyle: NodeModifier where Content == String {}
|
||||
|
||||
public extension TextStyle where Self == StyledText {
|
||||
@inlinable
|
||||
static var bold: Self { .init(.bold) }
|
||||
|
||||
@inlinable
|
||||
static var dim: Self { .init(.dim) }
|
||||
|
||||
@inlinable
|
||||
static var italic: Self { .init(.italic) }
|
||||
|
||||
@inlinable
|
||||
static var underline: Self { .init(.underline) }
|
||||
|
||||
@inlinable
|
||||
static var blink: Self { .init(.blink) }
|
||||
|
||||
@inlinable
|
||||
static var swap: Self { .init(.swap) }
|
||||
|
||||
@inlinable
|
||||
static var strikeThrough: Self { .init(.strikethrough) }
|
||||
}
|
||||
|
||||
public extension TextStyle where Self == ColorTextStyle {
|
||||
|
||||
@inlinable
|
||||
static func color(_ name: NamedColor) -> Self {
|
||||
.init(.named(name))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
static func color(bit8: UInt8) -> Self {
|
||||
.init(.bit8(bit8))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
static func color(rgb: RGB) -> Self {
|
||||
.init(.bit24(rgb))
|
||||
}
|
||||
}
|
||||
|
||||
public struct ColorTextStyle: TextStyle {
|
||||
enum Location {
|
||||
case foreground(ColorType)
|
||||
case background(ColorType)
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
let color: ColorType
|
||||
|
||||
@usableFromInline
|
||||
init(_ color: ColorType) {
|
||||
self.color = color
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func render(content: String) -> some TextNode {
|
||||
content.applyingColor(color)
|
||||
}
|
||||
}
|
||||
|
||||
public struct StyledText: TextStyle {
|
||||
@usableFromInline
|
||||
let style: Style
|
||||
|
||||
@usableFromInline
|
||||
init(_ style: Style) {
|
||||
self.style = style
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func render(content: String) -> some TextNode {
|
||||
content.applyingStyle(style)
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
import Rainbow
|
||||
|
||||
public extension TextNode {
|
||||
@inlinable
|
||||
func textStyle(_ styles: Style...) -> some TextNode {
|
||||
modifier(StyleModifier(styles: styles))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
func textStyle(_ styles: [Style]) -> some TextNode {
|
||||
modifier(StyleModifier(styles: styles))
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
struct StyleModifier<Content: TextNode>: NodeModifier {
|
||||
|
||||
@usableFromInline
|
||||
let styles: [Style]
|
||||
|
||||
@usableFromInline
|
||||
init(styles: [Style]) {
|
||||
self.styles = styles
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
func render(content: Content) -> some TextNode {
|
||||
styles.reduce(content.render()) {
|
||||
$0.applyingStyle($1)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user