feat: Working on node builder

This commit is contained in:
2024-12-01 11:45:05 -05:00
parent 55d8888961
commit ff49b12198
20 changed files with 495 additions and 227 deletions

View File

@@ -1,32 +1,45 @@
@preconcurrency import Rainbow
public struct Header: NodeRepresentable {
public struct Header: NodeRepresentable, @unchecked Sendable {
@usableFromInline
let node: Text
let node: any NodeRepresentable
@usableFromInline
let styles: [Style]
let color: NamedColor?
@usableFromInline
let styles: [Style]?
@inlinable
public init(
@NodeBuilder _ build: () -> any NodeRepresentable
) {
self.node = build()
self.color = nil
self.styles = nil
}
@inlinable
public init(
_ text: String,
color: NamedColor? = .yellow,
styles: [Style] = [.bold]
styles: [Style]? = [.bold]
) {
var text = Text(text)
if let color {
text = text.color(color)
}
self.node = text
self.color = color
self.node = Text(text)
self.styles = styles
}
@inlinable
public func render() -> String {
styles.reduce(node) { node, style in
node.style(style)
var node = node
if let color {
node = node.color(color)
}
.render()
if let styles {
node = node.style(styles)
}
return node.render()
}
}