feat: Working on node builder
This commit is contained in:
69
Sources/CliDoc/Nodes/Text.swift
Normal file
69
Sources/CliDoc/Nodes/Text.swift
Normal file
@@ -0,0 +1,69 @@
|
||||
@preconcurrency import Rainbow
|
||||
|
||||
public struct Text: NodeRepresentable {
|
||||
@usableFromInline
|
||||
let text: String
|
||||
|
||||
@inlinable
|
||||
public init(_ text: String) {
|
||||
self.text = text
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func render() -> String {
|
||||
text
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func color(_ color: NamedColor) -> Self {
|
||||
.init(text.applyingColor(color))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func style(_ style: Style) -> Self {
|
||||
.init(text.applyingStyle(style))
|
||||
}
|
||||
}
|
||||
|
||||
public struct NewLine: NodeRepresentable {
|
||||
@usableFromInline
|
||||
let node: any NodeRepresentable
|
||||
|
||||
@inlinable
|
||||
public init(count: Int = 1) {
|
||||
self.node = Text("\n").repeating(count)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func render() -> String {
|
||||
node.render()
|
||||
}
|
||||
}
|
||||
|
||||
public struct Space: NodeRepresentable {
|
||||
let node: any NodeRepresentable
|
||||
|
||||
public init(count: Int = 1) {
|
||||
self.node = Text(" ").repeating(count)
|
||||
}
|
||||
|
||||
public func render() -> String {
|
||||
node.render()
|
||||
}
|
||||
}
|
||||
|
||||
public extension NodeRepresentable {
|
||||
|
||||
static func empty() -> Self where Self == Text {
|
||||
Text("")
|
||||
}
|
||||
|
||||
static func newLine(count: Int = 1) -> Self where Self == NewLine {
|
||||
NewLine(count: count)
|
||||
}
|
||||
|
||||
static func space(count: Int = 1) -> Self where Self == Space {
|
||||
Space(count: count)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user