feat: Initial commit
This commit is contained in:
59
Sources/CliDoc/Modifiers/LabelColor.swift
Normal file
59
Sources/CliDoc/Modifiers/LabelColor.swift
Normal file
@@ -0,0 +1,59 @@
|
||||
import Rainbow
|
||||
|
||||
public extension Group {
|
||||
func labelColor(_ color: NamedColor) -> some Node {
|
||||
modifier(GroupLabelModifier(color: color))
|
||||
}
|
||||
}
|
||||
|
||||
public extension Node where Self.Body == Group {
|
||||
func labelColor(_ color: NamedColor) -> some Node {
|
||||
body.modifier(GroupLabelModifier(color: color))
|
||||
}
|
||||
}
|
||||
|
||||
public extension Node where Self == Label {
|
||||
func labelColor(_ color: NamedColor) -> some Node {
|
||||
modifier(LabelColorModifier(color: color))
|
||||
}
|
||||
}
|
||||
|
||||
public extension Note where Label == CliDoc.Label {
|
||||
func labelColor(_ color: NamedColor) -> some Node {
|
||||
var node = self
|
||||
node.label.color = color
|
||||
return node
|
||||
}
|
||||
}
|
||||
|
||||
struct LabelColorModifier: NodeModifier {
|
||||
let color: NamedColor
|
||||
|
||||
func render(content: Label) -> some Node {
|
||||
var label = content
|
||||
label.color = color
|
||||
return label
|
||||
}
|
||||
}
|
||||
|
||||
struct GroupLabelModifier: NodeModifier {
|
||||
let color: NamedColor
|
||||
|
||||
func render(content: Group) -> some Node {
|
||||
var group = content
|
||||
applyLabelColor(&group)
|
||||
return group
|
||||
}
|
||||
|
||||
private func applyLabelColor(_ group: inout Group) {
|
||||
for (idx, node) in group.nodes.enumerated() {
|
||||
if var label = node as? Label {
|
||||
label.color = color
|
||||
group.nodes[idx] = label
|
||||
} else if var nestedGroup = node as? Group {
|
||||
applyLabelColor(&nestedGroup)
|
||||
group.nodes[idx] = nestedGroup
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
21
Sources/CliDoc/Modifiers/LabelStyle.swift
Normal file
21
Sources/CliDoc/Modifiers/LabelStyle.swift
Normal file
@@ -0,0 +1,21 @@
|
||||
import Rainbow
|
||||
|
||||
// public extension Node {
|
||||
// func labelStyel(_ styles: Style...) -> any Node {}
|
||||
// }
|
||||
|
||||
public extension Node where Self == Label {
|
||||
func labelStyle(_ styles: Style...) -> some Node {
|
||||
modifier(LabelStyleModifier(styles: styles))
|
||||
}
|
||||
}
|
||||
|
||||
struct LabelStyleModifier: NodeModifier {
|
||||
let styles: [Style]
|
||||
|
||||
func render(content: Label) -> some Node {
|
||||
var label = content
|
||||
label.styles = styles
|
||||
return label
|
||||
}
|
||||
}
|
||||
27
Sources/CliDoc/Node.swift
Normal file
27
Sources/CliDoc/Node.swift
Normal file
@@ -0,0 +1,27 @@
|
||||
public protocol NodeRepresentable {
|
||||
func render() -> String
|
||||
}
|
||||
|
||||
public protocol Node: NodeRepresentable {
|
||||
// swiftlint:disable type_name
|
||||
associatedtype _Body: Node
|
||||
typealias Body = _Body
|
||||
// swiftlint:enable type_name
|
||||
|
||||
@NodeBuilder
|
||||
var body: Body { get }
|
||||
}
|
||||
|
||||
public extension Node {
|
||||
func render() -> String {
|
||||
body.render()
|
||||
}
|
||||
}
|
||||
|
||||
extension String: NodeRepresentable {
|
||||
public func render() -> String { self }
|
||||
}
|
||||
|
||||
extension String: Node {
|
||||
public var body: some Node { self }
|
||||
}
|
||||
36
Sources/CliDoc/NodeBuilder.swift
Normal file
36
Sources/CliDoc/NodeBuilder.swift
Normal file
@@ -0,0 +1,36 @@
|
||||
@resultBuilder
|
||||
public enum NodeBuilder {
|
||||
|
||||
public static func buildPartialBlock<N: Node>(first: N) -> N {
|
||||
first
|
||||
}
|
||||
|
||||
public static func buildArray<N: Node>(_ components: [N]) -> Group {
|
||||
.init(components)
|
||||
}
|
||||
|
||||
public static func buildOptional<N: Node>(_ component: N?) -> OptionalNode<N> {
|
||||
.init(component: component)
|
||||
}
|
||||
|
||||
public static func buildEither<N: Node>(first component: N) -> N {
|
||||
component
|
||||
}
|
||||
|
||||
public static func buildEither<N: Node>(second component: N) -> N {
|
||||
component
|
||||
}
|
||||
|
||||
public static func buildPartialBlock<N0: Node, N1: Node>(accumulated: N0, next: N1) -> Group {
|
||||
.init([accumulated, next])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public struct OptionalNode<N: Node>: Node {
|
||||
let component: N?
|
||||
|
||||
public var body: some Node {
|
||||
component?.render() ?? ""
|
||||
}
|
||||
}
|
||||
59
Sources/CliDoc/NodeModifier.swift
Normal file
59
Sources/CliDoc/NodeModifier.swift
Normal file
@@ -0,0 +1,59 @@
|
||||
public protocol NodeModifier {
|
||||
associatedtype Body: Node
|
||||
// swiftlint:disable type_name
|
||||
associatedtype _Content: Node
|
||||
typealias Content = _Content
|
||||
// swiftlint:enable type_name
|
||||
|
||||
func render(content: Content) -> Body
|
||||
}
|
||||
|
||||
public extension NodeModifier {
|
||||
func modifier<T: NodeModifier>(
|
||||
_ modifier: T
|
||||
) -> ModifiedNode<Self, T> where T.Content == Body {
|
||||
.init(content: self, modifier: modifier)
|
||||
}
|
||||
}
|
||||
|
||||
public extension Node {
|
||||
|
||||
func modifier<T: NodeModifier>(_ modifier: T) -> ModifiedNode<Self, T> {
|
||||
.init(content: self, modifier: modifier)
|
||||
}
|
||||
}
|
||||
|
||||
public struct ModifiedNode<Content, Modifier> {
|
||||
var content: Content
|
||||
var modifier: Modifier
|
||||
}
|
||||
|
||||
extension ModifiedNode: NodeRepresentable where Content: NodeRepresentable,
|
||||
Modifier: NodeModifier,
|
||||
Modifier.Content == Content
|
||||
{
|
||||
public func render() -> String {
|
||||
modifier.render(content: content).render()
|
||||
}
|
||||
}
|
||||
|
||||
extension ModifiedNode: Node where Content: Node,
|
||||
Modifier: NodeModifier,
|
||||
Modifier.Content == Content
|
||||
{
|
||||
public var body: some Node {
|
||||
content
|
||||
}
|
||||
}
|
||||
|
||||
extension ModifiedNode: NodeModifier where
|
||||
Content: NodeModifier,
|
||||
Modifier: NodeModifier,
|
||||
Content.Body == Modifier.Content,
|
||||
Content: Node
|
||||
{
|
||||
public func render(content: Content) -> some Node {
|
||||
let body = content.body
|
||||
return modifier.render(content: body).render()
|
||||
}
|
||||
}
|
||||
18
Sources/CliDoc/Nodes/AnyNode.swift
Normal file
18
Sources/CliDoc/Nodes/AnyNode.swift
Normal 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)
|
||||
}
|
||||
}
|
||||
18
Sources/CliDoc/Nodes/Colored.swift
Normal file
18
Sources/CliDoc/Nodes/Colored.swift
Normal 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)
|
||||
}
|
||||
}
|
||||
28
Sources/CliDoc/Nodes/Group.swift
Normal file
28
Sources/CliDoc/Nodes/Group.swift
Normal 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)
|
||||
}
|
||||
|
||||
}
|
||||
45
Sources/CliDoc/Nodes/Label.swift
Normal file
45
Sources/CliDoc/Nodes/Label.swift
Normal 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)
|
||||
}
|
||||
|
||||
}
|
||||
31
Sources/CliDoc/Nodes/Note.swift
Normal file
31
Sources/CliDoc/Nodes/Note.swift
Normal 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)
|
||||
}
|
||||
}
|
||||
20
Sources/CliDoc/Nodes/ShellCommand.swift
Normal file
20
Sources/CliDoc/Nodes/ShellCommand.swift
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user