This commit is contained in:
2024-12-05 07:50:55 -05:00
parent b0b218e047
commit 33356d8648
16 changed files with 216 additions and 146 deletions

View File

@@ -1,11 +0,0 @@
/// An empty text node.
public struct Empty: TextNode {
@inlinable
public init() {}
@inlinable
public var body: some TextNode {
""
}
}

View File

@@ -4,23 +4,32 @@ public struct ExampleSection<Header: TextNode, Label: TextNode>: TextNode {
public typealias Example = (label: String, example: String)
@usableFromInline
let examples: [Example]
@usableFromInline
let header: Header
@usableFromInline
let label: Label
let configuration: ExampleSectionConfiguration
@inlinable
public init(
examples: [Example],
@TextBuilder header: () -> Header,
@TextBuilder title: () -> Header,
@TextBuilder label: () -> Label
) {
self.examples = examples
self.header = header()
self.label = label()
self.configuration = .init(
title: title(),
label: label(),
examples: examples
)
}
@inlinable
public init(
_ title: @autoclosure () -> Header,
label: @autoclosure () -> Label,
examples: [Example]
) {
self.init(
examples: examples,
title: title,
label: label
)
}
@inlinable
@@ -29,23 +38,105 @@ public struct ExampleSection<Header: TextNode, Label: TextNode>: TextNode {
}
}
public extension ExampleSection where Header == String, Label == String {
@inlinable
init(
header: String = "Examples:".yellow.bold,
label: String = "Some common usage examples.",
examples: [Example]
) {
self.init(examples: examples) { header } label: { label }
}
/// The type-erased configuration of an ``ExampleSection``
public struct ExampleSectionConfiguration {
@usableFromInline
let title: any TextNode
@inlinable
init(
header: String = "Examples:".yellow.bold,
label: String = "Some common usage examples.",
examples: Example...
) {
self.init(header: header, label: label, examples: examples)
}
@usableFromInline
let label: any TextNode
@usableFromInline
let examples: [ExampleSection.Example]
@usableFromInline
init(title: any TextNode, label: any TextNode, examples: [ExampleSection.Example]) {
self.title = title
self.label = label
self.examples = examples
}
}
// MARK: - Style
public extension ExampleSection {
func style<S: ExampleSectionStyle>(_ style: S) -> some TextNode {
style.render(content: configuration)
}
func exampleStyle<S: ExampleStyle>(_ style: S) -> some TextNode {
DefaultExamplesStyle(exampleStyle: style).render(content: configuration)
}
}
extension Array where Element == ExampleSection.Example {
func exampleStyle<S: ExampleStyle>(_ style: S) -> some TextNode {
style.render(content: .init(examples: self))
}
}
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.title
.color(.yellow)
.textStyle(.bold)
content.label
.textStyle(.italic)
}
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

@@ -1,16 +0,0 @@
public struct Group<Content: TextNode>: TextNode {
@usableFromInline
var content: Content
@inlinable
public init(
@TextBuilder content: () -> Content
) {
self.content = content()
}
@inlinable
public var body: some TextNode {
content
}
}

View File

@@ -1,22 +0,0 @@
public struct HStack: TextNode {
@usableFromInline
let content: [any TextNode]
@usableFromInline
let separator: any TextNode
@inlinable
public init(
spacing: Int = 1,
@TextBuilder content: () -> any TextNode
) {
self.content = array(from: content())
self.separator = seperator(" ", count: spacing > 0 ? spacing - 1 : 0)
}
@inlinable
public var body: some TextNode {
content.map { $0.render() }.joined(separator: separator.render())
}
}

View File

@@ -1,46 +0,0 @@
public struct Section<Header: TextNode, Content: TextNode, Footer: TextNode>: TextNode {
@usableFromInline
let header: Header
@usableFromInline
let content: Content
@usableFromInline
let footer: Footer
@inlinable
public init(
@TextBuilder header: () -> Header,
@TextBuilder content: () -> Content,
@TextBuilder footer: () -> Footer
) {
self.header = header()
self.content = content()
self.footer = footer()
}
public var body: some TextNode {
style(.default)
}
}
public extension Section where Footer == Empty {
@inlinable
init(
@TextBuilder header: () -> Header,
@TextBuilder content: () -> Content
) {
self.init(header: header, content: content) { Empty() }
}
}
public extension Section where Header == Empty {
@inlinable
init(
@TextBuilder content: () -> Content,
@TextBuilder footer: () -> Footer
) {
self.init(header: { Empty() }, content: content, footer: footer)
}
}

View File

@@ -1,22 +0,0 @@
public struct VStack: TextNode {
@usableFromInline
let content: [any TextNode]
@usableFromInline
let separator: any TextNode
@inlinable
public init(
spacing: Int = 1,
@TextBuilder content: () -> any TextNode
) {
self.content = array(from: content())
self.separator = seperator("\n", count: spacing > 0 ? spacing - 1 : 0)
}
@inlinable
public var body: some TextNode {
content.map { $0.render() }.joined(separator: separator.render())
}
}