feat: Working on node builder
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user