From bef91c5277ba4c6e544f89613c004f74a28c9a36 Mon Sep 17 00:00:00 2001 From: Michael Housh Date: Thu, 5 Dec 2024 14:13:47 -0500 Subject: [PATCH] feat: Updating text style modifiers --- Sources/CliDoc/Nodes/ExampleSection.swift | 5 +- .../CliDocCore/Modifiers/ColorModifier.swift | 24 ---- Sources/CliDocCore/Modifiers/TextStyle.swift | 124 ++++++++++++++++++ .../Modifiers/TextStyleModifier.swift | 32 ----- 4 files changed, 126 insertions(+), 59 deletions(-) delete mode 100644 Sources/CliDocCore/Modifiers/ColorModifier.swift create mode 100644 Sources/CliDocCore/Modifiers/TextStyle.swift delete mode 100644 Sources/CliDocCore/Modifiers/TextStyleModifier.swift diff --git a/Sources/CliDoc/Nodes/ExampleSection.swift b/Sources/CliDoc/Nodes/ExampleSection.swift index a1e7285..a0340d9 100644 --- a/Sources/CliDoc/Nodes/ExampleSection.swift +++ b/Sources/CliDoc/Nodes/ExampleSection.swift @@ -130,10 +130,9 @@ public struct DefaultExampleSectionStyle: ExampleSectionSty HStack { content.title .color(.yellow) - .textStyle(.bold) + .bold() - content.label - .textStyle(.italic) + content.label.italic() } } } diff --git a/Sources/CliDocCore/Modifiers/ColorModifier.swift b/Sources/CliDocCore/Modifiers/ColorModifier.swift deleted file mode 100644 index a37c13f..0000000 --- a/Sources/CliDocCore/Modifiers/ColorModifier.swift +++ /dev/null @@ -1,24 +0,0 @@ -import Rainbow - -public extension TextNode { - @inlinable - func color(_ color: NamedColor) -> some TextNode { - modifier(ColorModifier(color: color)) - } -} - -@usableFromInline -struct ColorModifier: NodeModifier { - @usableFromInline - let color: NamedColor - - @usableFromInline - init(color: NamedColor) { - self.color = color - } - - @usableFromInline - func render(content: Content) -> some TextNode { - content.render().applyingColor(color) - } -} diff --git a/Sources/CliDocCore/Modifiers/TextStyle.swift b/Sources/CliDocCore/Modifiers/TextStyle.swift new file mode 100644 index 0000000..23d1fe8 --- /dev/null +++ b/Sources/CliDocCore/Modifiers/TextStyle.swift @@ -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(_ 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) + } +} diff --git a/Sources/CliDocCore/Modifiers/TextStyleModifier.swift b/Sources/CliDocCore/Modifiers/TextStyleModifier.swift deleted file mode 100644 index 64cae01..0000000 --- a/Sources/CliDocCore/Modifiers/TextStyleModifier.swift +++ /dev/null @@ -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: 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) - } - } -}