feat: Working on node builder
This commit is contained in:
30
Sources/CliDoc/Nodes/Section.swift
Normal file
30
Sources/CliDoc/Nodes/Section.swift
Normal file
@@ -0,0 +1,30 @@
|
||||
public struct Section: NodeRepresentable {
|
||||
|
||||
@usableFromInline
|
||||
let content: any NodeRepresentable
|
||||
|
||||
public init(@NodeBuilder content: () -> any NodeRepresentable) {
|
||||
self.content = content()
|
||||
}
|
||||
|
||||
public func render() -> String {
|
||||
guard let many = content as? _ManyNode else {
|
||||
return content.render() + "\n\n"
|
||||
}
|
||||
|
||||
let node = _ManyNode(many.nodes, separator: "\n\n")
|
||||
return node.render()
|
||||
|
||||
// var output = ""
|
||||
// let lastIndex = many.nodes.count - 1
|
||||
//
|
||||
// for (idx, content) in many.nodes.enumerated() {
|
||||
// if idx != lastIndex {
|
||||
// output += content.render() + "\n\n"
|
||||
// } else {
|
||||
// output += content.render() + "\n"
|
||||
// }
|
||||
// }
|
||||
// return output
|
||||
}
|
||||
}
|
||||
41
Sources/CliDoc/Nodes/ShellCommand.swift
Normal file
41
Sources/CliDoc/Nodes/ShellCommand.swift
Normal file
@@ -0,0 +1,41 @@
|
||||
public struct ShellCommand: NodeRepresentable {
|
||||
|
||||
@usableFromInline
|
||||
let symbol: any NodeRepresentable
|
||||
|
||||
@usableFromInline
|
||||
let command: any NodeRepresentable
|
||||
|
||||
@inlinable
|
||||
public init(
|
||||
@NodeBuilder symbol: () -> any NodeRepresentable,
|
||||
@NodeBuilder command: () -> any NodeRepresentable
|
||||
) {
|
||||
self.symbol = symbol()
|
||||
self.command = command()
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public init(
|
||||
symbol: String = " $",
|
||||
@NodeBuilder command: () -> any NodeRepresentable
|
||||
) {
|
||||
self.init { symbol } command: { command() }
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public init(
|
||||
symbol: String = " $",
|
||||
command: String
|
||||
) {
|
||||
self.init { symbol } command: { command }
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func render() -> String {
|
||||
Group(separator: " ") {
|
||||
symbol
|
||||
command
|
||||
}.render()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user