feat: More styles, renames some items.

This commit is contained in:
2024-12-04 16:15:21 -05:00
parent 10119c3e51
commit 2a9b350b26
10 changed files with 198 additions and 30 deletions

View File

@@ -0,0 +1,105 @@
import Rainbow
public extension ExampleSection {
func style<S: ExampleSectionStyle>(_ style: S) -> some TextNode {
style.render(content: .init(
header: header,
label: label,
examples: examples
))
}
func exampleStyle<S: ExampleStyle>(_ style: S) -> some TextNode {
DefaultExamplesStyle().render(content: .init(
header: header,
label: label,
examples: examples
))
}
}
extension Array where Element == ExampleSection.Example {
func exampleStyle<S: ExampleStyle>(_ style: S) -> some TextNode {
style.render(content: .init(examples: self))
}
}
public struct ExampleSectionConfiguration {
@usableFromInline
let header: any TextNode
@usableFromInline
let label: any TextNode
@usableFromInline
let examples: [ExampleSection.Example]
@usableFromInline
init(header: any TextNode, label: any TextNode, examples: [ExampleSection.Example]) {
self.header = header
self.label = label
self.examples = examples
}
}
public struct ExampleConfiguration {
@usableFromInline
let examples: [ExampleSection.Example]
@usableFromInline
init(examples: [ExampleSection.Example]) {
self.examples = examples
}
}
public protocol ExampleSectionStyle: NodeModifier where Content == ExampleSectionConfiguration {}
public protocol ExampleStyle: NodeModifier where Content == ExampleConfiguration {}
public extension ExampleSectionStyle where Self == DefaultExamplesStyle {
static func `default`(exampleStyle: any ExampleStyle = .default) -> Self {
DefaultExamplesStyle(exampleStyle: exampleStyle)
}
}
public extension ExampleStyle where Self == DefaultExampleStyle {
static var `default`: Self {
DefaultExampleStyle()
}
}
public struct DefaultExamplesStyle: ExampleSectionStyle {
@usableFromInline
let exampleStyle: any ExampleStyle
@inlinable
public init(exampleStyle: any ExampleStyle = .default) {
self.exampleStyle = exampleStyle
}
@inlinable
public func render(content: ExampleSectionConfiguration) -> some TextNode {
VStack(spacing: 2) {
HStack {
content.header
content.label
}
exampleStyle.render(content: .init(examples: content.examples))
}
}
}
public struct DefaultExampleStyle: ExampleStyle {
public func render(content: ExampleConfiguration) -> some TextNode {
VStack(spacing: 2) {
content.examples.map { example in
VStack {
Label(example.label.green.bold)
ShellCommand { example.example }.style(.default)
}
}
}
}
}

View File

@@ -26,7 +26,7 @@ public struct LabelStyle<C: TextNode>: NodeModifier {
@inlinable
public func render(content: Label<C>) -> some TextNode {
var label: any TextNode = content.content
label = label.style(styles)
label = label.textStyle(styles)
if let color {
label = label.color(color)
}

View File

@@ -23,7 +23,7 @@ public struct DefaultNoteStyle: NoteStyleModifier {
public func render(content: NoteStyleConfiguration) -> some TextNode {
HStack {
content.label.color(.yellow).style(.bold)
content.label.color(.yellow).textStyle(.bold)
content.content
}
}

View File

@@ -0,0 +1,28 @@
import Rainbow
public struct ShellCommandConfiguration {
let symbol: any TextNode
let content: any TextNode
}
public extension ShellCommand {
func style<S: ShellCommandStyle>(_ style: S) -> some TextNode {
style.render(content: .init(symbol: symbol, content: content))
}
}
public protocol ShellCommandStyle: NodeModifier where Self.Content == ShellCommandConfiguration {}
public extension ShellCommandStyle where Self == DefaultShellCommandStyle {
static var `default`: Self { DefaultShellCommandStyle() }
}
public struct DefaultShellCommandStyle: ShellCommandStyle {
public func render(content: ShellCommandConfiguration) -> some TextNode {
HStack {
content.symbol
content.content.textStyle(.italic)
}
}
}

View File

@@ -2,12 +2,12 @@ import Rainbow
public extension TextNode {
@inlinable
func style(_ styles: Style...) -> some TextNode {
func textStyle(_ styles: Style...) -> some TextNode {
modifier(StyleModifier(styles: styles))
}
@inlinable
func style(_ styles: [Style]) -> some TextNode {
func textStyle(_ styles: [Style]) -> some TextNode {
modifier(StyleModifier(styles: styles))
}
}