feat: Initial commit

This commit is contained in:
2024-12-02 17:04:28 -05:00
commit 22dfc6ce51
18 changed files with 505 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
/// A type erased node.
public struct AnyNode: Node {
private var renderNode: () -> String
public init<N: NodeRepresentable>(_ node: N) {
self.renderNode = node.render
}
public var body: some Node {
renderNode()
}
}
public extension Node {
func eraseToAnyNode() -> AnyNode {
.init(self)
}
}

View File

@@ -0,0 +1,18 @@
import Rainbow
public struct Colored: Node {
var color: NamedColor
var node: any Node
public init(
color: NamedColor,
@NodeBuilder build: () -> any Node
) {
self.color = color
self.node = build()
}
public var body: some Node {
node.render().applyingColor(color)
}
}

View File

@@ -0,0 +1,28 @@
/// A group container holding one or more nodes.
public struct Group: Node {
var nodes: [any Node]
var separator: String
init(_ nodes: [any Node], separator: String = "\n") {
self.nodes = nodes
self.separator = separator
}
public init(
separator: String = " ",
@NodeBuilder build: () -> any Node
) {
let node = build()
if var group = node as? Self {
group.separator = separator
self = group
} else {
self.init([node], separator: separator)
}
}
public var body: some Node {
nodes.map { $0.render() }.joined(separator: separator)
}
}

View File

@@ -0,0 +1,45 @@
import Rainbow
public struct Label: Node {
var color: NamedColor?
var styles: [Style]
let node: any Node
public init(
_ label: String,
color: NamedColor? = nil,
style styles: Style...
) {
self.color = color
self.node = label
self.styles = styles
}
public init(
_ label: String,
color: NamedColor? = nil,
style styles: [Style] = []
) {
self.color = color
self.node = label
self.styles = styles
}
public init(
color: NamedColor? = nil,
styles: [Style] = [],
@NodeBuilder _ build: () -> any Node
) {
self.color = color
self.styles = styles
self.node = build()
}
public var body: some Node {
let output = styles.reduce(node.render()) { $0.applyingStyle($1) }
guard let color else { return output }
return output.applyingColor(color)
}
}

View File

@@ -0,0 +1,31 @@
public struct Note<Label: Node, Content: Node>: Node {
var separator: String
var label: Label
var content: Content
public init(
separator: String = " ",
@NodeBuilder label: () -> Label,
@NodeBuilder content: () -> Content
) {
self.separator = separator
self.label = label()
self.content = content()
}
public var body: some Node {
Group([label, content], separator: separator)
}
}
public extension Note where Label == CliDoc.Label {
init(
separator: String = " ",
label: String = "NOTE:",
@NodeBuilder content: () -> Content
) {
self.init(separator: separator, label: { CliDoc.Label(label) }, content: content)
}
}

View File

@@ -0,0 +1,20 @@
public struct ShellCommand<Content: Node>: Node {
public var symbol: String
public var content: Content
public init(
symbol: String = "$",
@NodeBuilder content: () -> Content
) {
self.symbol = symbol
self.content = content()
}
public var body: some Node {
Group(separator: " ") {
symbol
content
}
}
}