feat: Removes old version / files

This commit is contained in:
2024-12-03 21:23:54 -05:00
parent 590df275cc
commit 0a18b57dc8
25 changed files with 206 additions and 606 deletions

View File

@@ -17,19 +17,9 @@ let package = Package(
.product(name: "Rainbow", package: "Rainbow")
]
),
.target(
name: "CliDoc2",
dependencies: [
.product(name: "Rainbow", package: "Rainbow")
]
),
.testTarget(
name: "CliDocTests",
dependencies: ["CliDoc"]
),
.testTarget(
name: "CliDoc2Tests",
dependencies: ["CliDoc2"]
)
]
)

View File

@@ -1,59 +0,0 @@
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
}
}
}
}

View File

@@ -1,21 +0,0 @@
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
}
}

View File

@@ -1,27 +0,0 @@
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 }
}

View File

@@ -1,36 +0,0 @@
@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() ?? ""
}
}

View File

@@ -1,73 +1,68 @@
import Rainbow
public protocol NodeModifier {
associatedtype Body: Node
// swiftlint:disable type_name
associatedtype _Content: Node
typealias Content = _Content
associatedtype _Body: TextNode
typealias Body = _Body
// swiftlint:enable type_name
associatedtype Content: TextNode
@TextBuilder
func render(content: Content) -> Body
}
public extension NodeModifier {
func concat<T: NodeModifier>(
_ modifier: T
) -> ConcatModifier<Content, Self, T> {
ConcatModifier(first: self, second: modifier)
func concat<T: NodeModifier>(_ modifier: T) -> ConcatModifier<Self, T> {
return .init(firstModifier: self, secondModifier: modifier)
}
}
public extension Node {
func modifier<T: NodeModifier>(_ modifier: T) -> ModifiedNode<Self, T> {
public struct ConcatModifier<M0: NodeModifier, M1: NodeModifier>: NodeModifier where M1.Content == M0.Body {
let firstModifier: M0
let secondModifier: M1
public func render(content: M0.Content) -> some TextNode {
let firstOutput = firstModifier.render(content: content)
return secondModifier.render(content: firstOutput)
}
}
public struct ModifiedNode<Content: TextNode, Modifier: NodeModifier> {
@usableFromInline
let content: Content
@usableFromInline
let modifier: Modifier
@usableFromInline
init(content: Content, modifier: Modifier) {
self.content = content
self.modifier = modifier
}
}
extension ModifiedNode: TextNode where Modifier.Content == Content {
public var body: some TextNode {
modifier.render(content: content)
}
@inlinable
func apply<M: NodeModifier>(_ modifier: M) -> ModifiedNode<Content, ConcatModifier<Modifier, M>> {
return .init(content: content, modifier: self.modifier.concat(modifier))
}
}
extension ModifiedNode: NodeRepresentable where Self: TextNode {
public func render() -> String {
body.render()
}
}
public extension TextNode {
@inlinable
func modifier<M: NodeModifier>(_ modifier: M) -> ModifiedNode<Self, M> {
.init(content: self, modifier: modifier)
}
}
public struct ConcatModifier<Content, Modifier1, Modifier2>: NodeModifier where
Content: Node,
Modifier1: NodeModifier,
Modifier2: NodeModifier,
Modifier1.Content == Content,
Modifier1.Body == Modifier2.Content
{
let first: Modifier1
let second: Modifier2
public func render(content: Content) -> some Node {
second.render(content: first.render(content: content))
}
}
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: Content {
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()
}
}

View File

@@ -1,18 +0,0 @@
/// 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

@@ -1,18 +0,0 @@
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,40 @@
import Rainbow
public struct Examples<Header: TextNode, Label: TextNode>: TextNode {
public typealias Example = (label: String, example: String)
@usableFromInline
let examples: [Example]
@usableFromInline
let header: Header
@usableFromInline
let label: Label
public init(
examples: [Example],
@TextBuilder header: () -> Header,
@TextBuilder label: () -> Label
) {
self.examples = examples
self.header = header()
self.label = label()
}
public var body: some TextNode {
Group(separator: "") {
Group(separator: " ", content: [header.color(.yellow).style(.bold), label, "\n"])
"\n"
Group(
separator: "\n\n",
content: self.examples.map { example in
Group(separator: "\n") {
CliDoc.Label(example.label.green.bold)
ShellCommand { example.example.italic }
}
}
)
}
}
}

View File

@@ -1,28 +1,41 @@
/// A group container holding one or more nodes.
public struct Group: Node {
var nodes: [any Node]
var separator: String
public struct Group: TextNode {
@usableFromInline
var content: [any TextNode]
init(_ nodes: [any Node], separator: String = "\n") {
self.nodes = nodes
@usableFromInline
var separator: any TextNode
@inlinable
public init(
separator: any TextNode = "\n",
content: [any TextNode]
) {
self.content = content
self.separator = separator
}
@inlinable
public init(
separator: String = " ",
@NodeBuilder build: () -> any Node
separator: any TextNode = "\n",
@TextBuilder content: () -> any TextNode
) {
let node = build()
if var group = node as? Self {
group.separator = separator
self = group
// Check if the content is a NodeContainer, typically is when
// using the TextBuilder with more than one text node.
//
// We need to take over the contents, so we can control the separator.
let content = content()
if let many = content as? NodeContainer {
self.content = many.nodes
} else {
self.init([node], separator: separator)
// We didn't get a NodeContainer, so fallback to just storing
// the content.
self.content = [content]
}
self.separator = separator
}
public var body: some Node {
nodes.map { $0.render() }.joined(separator: separator)
@inlinable
public var body: some TextNode {
content.map { $0.render() }.joined(separator: separator.render())
}
}

View File

@@ -1,45 +1,19 @@
import Rainbow
public struct Label<Content: TextNode>: TextNode {
@usableFromInline
let content: Content
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
@inlinable
public init(@TextBuilder _ content: () -> Content) {
self.content = content()
}
public init(
_ label: String,
color: NamedColor? = nil,
style styles: [Style] = []
) {
self.color = color
self.node = label
self.styles = styles
@inlinable
public init(_ content: Content) {
self.content = content
}
public init(
color: NamedColor? = nil,
styles: [Style] = [],
@NodeBuilder _ build: () -> any Node
) {
self.color = color
self.styles = styles
self.node = build()
@inlinable
public var body: some TextNode {
content
}
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

@@ -1,31 +1,58 @@
public struct Note<Label: Node, Content: Node>: Node {
import Rainbow
var separator: String
var label: Label
var content: Content
public struct Note<Label: TextNode, Content: TextNode>: TextNode {
@usableFromInline
let label: Label
@usableFromInline
let content: Content
@usableFromInline
var separator: any TextNode = " "
@inlinable
public init(
separator: String = " ",
@NodeBuilder label: () -> Label,
@NodeBuilder content: () -> Content
separator: any TextNode = " ",
@TextBuilder _ label: () -> Label,
@TextBuilder content: () -> Content
) {
self.separator = separator
self.label = label()
self.content = content()
}
public var body: some Node {
Group([label, content], separator: separator)
@inlinable
public var body: some TextNode {
Group(separator: separator, content: [label, content])
}
}
public extension Note where Label == CliDoc.Label {
public extension Note where Label == String {
@inlinable
init(
separator: String = " ",
label: String = "NOTE:",
@NodeBuilder content: () -> Content
separator: any TextNode = " ",
_ label: String = "NOTE:".yellow.bold,
@TextBuilder content: () -> Content
) {
self.init(separator: separator, label: { CliDoc.Label(label) }, content: content)
self.separator = separator
self.label = label
self.content = content()
}
static func important(
separator: any TextNode = " ",
_ label: String = "IMPORTANT NOTE:".red.underline,
@TextBuilder content: () -> Content
) -> Self {
self.init(separator: separator, label, content: content)
}
static func seeAlso(
separator: any TextNode = " ",
_ label: String = "SEE ALSO:".yellow.bold,
@TextBuilder content: () -> Content
) -> Self {
self.init(separator: separator, label, content: content)
}
}

View File

@@ -1,20 +1,21 @@
public struct ShellCommand<Content: Node>: Node {
public struct ShellCommand<Content: TextNode>: TextNode {
public var symbol: String
public var content: Content
@usableFromInline
var symbol: any TextNode
@usableFromInline
var content: Content
@inlinable
public init(
symbol: String = "$",
@NodeBuilder content: () -> Content
symbol: any TextNode = "$",
@TextBuilder content: () -> Content
) {
self.symbol = symbol
self.content = content()
}
public var body: some Node {
Group(separator: " ") {
symbol
content
}
public var body: some TextNode {
Group(separator: " ", content: [symbol, content])
}
}

View File

@@ -1,68 +0,0 @@
import Rainbow
public protocol NodeModifier {
// swiftlint:disable type_name
associatedtype _Body: TextNode
typealias Body = _Body
// swiftlint:enable type_name
associatedtype Content: TextNode
@TextBuilder
func render(content: Content) -> Body
}
public extension NodeModifier {
func concat<T: NodeModifier>(_ modifier: T) -> ConcatModifier<Self, T> {
return .init(firstModifier: self, secondModifier: modifier)
}
}
public struct ConcatModifier<M0: NodeModifier, M1: NodeModifier>: NodeModifier where M1.Content == M0.Body {
let firstModifier: M0
let secondModifier: M1
public func render(content: M0.Content) -> some TextNode {
let firstOutput = firstModifier.render(content: content)
return secondModifier.render(content: firstOutput)
}
}
public struct ModifiedNode<Content: TextNode, Modifier: NodeModifier> {
@usableFromInline
let content: Content
@usableFromInline
let modifier: Modifier
@usableFromInline
init(content: Content, modifier: Modifier) {
self.content = content
self.modifier = modifier
}
}
extension ModifiedNode: TextNode where Modifier.Content == Content {
public var body: some TextNode {
modifier.render(content: content)
}
@inlinable
func apply<M: NodeModifier>(_ modifier: M) -> ModifiedNode<Content, ConcatModifier<Modifier, M>> {
return .init(content: content, modifier: self.modifier.concat(modifier))
}
}
extension ModifiedNode: NodeRepresentable where Self: TextNode {
public func render() -> String {
body.render()
}
}
public extension TextNode {
@inlinable
func modifier<M: NodeModifier>(_ modifier: M) -> ModifiedNode<Self, M> {
.init(content: self, modifier: modifier)
}
}

View File

@@ -1,37 +0,0 @@
public struct Group: TextNode {
@usableFromInline
var content: [any TextNode]
@usableFromInline
var separator: any TextNode
@usableFromInline
init(
content: [any TextNode],
separator: any TextNode = "\n"
) {
self.content = content
self.separator = separator
}
@inlinable
public init(
separator: any TextNode = "\n",
@TextBuilder content: () -> any TextNode
) {
let content = content()
if let many = content as? NodeContainer {
self.content = many.nodes
} else {
self.content = [content]
}
self.separator = separator
}
@inlinable
public var body: some TextNode {
content.reduce("") {
$0 + $1.render() + separator.render()
}
}
}

View File

@@ -1,19 +0,0 @@
public struct Label<Content: TextNode>: TextNode {
@usableFromInline
let content: Content
@inlinable
public init(@TextBuilder _ content: () -> Content) {
self.content = content()
}
@inlinable
public init(_ content: Content) {
self.content = content
}
@inlinable
public var body: some TextNode {
content
}
}

View File

@@ -1,51 +0,0 @@
import Rainbow
public struct Note<Label: TextNode, Content: TextNode>: TextNode {
@usableFromInline
let label: Label
@usableFromInline
let content: Content
@usableFromInline
var separator: any TextNode = " "
@inlinable
public init(
separator: any TextNode = " ",
@TextBuilder _ label: () -> Label,
@TextBuilder content: () -> Content
) {
self.separator = separator
self.label = label()
self.content = content()
}
@inlinable
public var body: some TextNode {
Group(content: [label, content], separator: separator)
}
}
public extension Note where Label == String {
@inlinable
init(
separator: any TextNode = " ",
_ label: String = "NOTE:".yellow.bold,
@TextBuilder content: () -> Content
) {
self.separator = separator
self.label = label
self.content = content()
}
static func important(
separator: any TextNode = " ",
_ label: String = "IMPORTANT NOTE:".red.underline,
@TextBuilder content: () -> Content
) {
self.init(separator: separator, label, content: content)
}
}

View File

@@ -1,21 +0,0 @@
public struct ShellCommand<Content: TextNode>: TextNode {
@usableFromInline
var symbol: any TextNode
@usableFromInline
var content: Content
@inlinable
public init(
symbol: any TextNode = "$",
@TextBuilder content: () -> Content
) {
self.symbol = symbol
self.content = content()
}
public var body: some TextNode {
Group(content: [symbol, content], separator: " ")
}
}

View File

@@ -1,34 +0,0 @@
@testable import CliDoc2
@preconcurrency import Rainbow
import Testing
let setupRainbow: Bool = {
Rainbow.enabled = true
Rainbow.outputTarget = .console
return true
}()
@Test
func testGroup() {
#expect(setupRainbow)
let group = Group {
Label { "Foo:" }
"Bar"
"Baz"
Note { "Bang:" } content: { "boom" }
if setupRainbow {
Label("Hello, rainbow").color(.blue)
} else {
Label("No color for you!").color(.red)
}
}
.color(.green)
.style(.italic)
print(type(of: group))
print(group.render())
// let note = Note { "Bang:" } content: { "boom" }
// print(note.render())
// print(type(of: note.label))
}

View File

@@ -9,65 +9,34 @@ let setupRainbow: Bool = {
}()
@Test
func checkStringBuilder() {
func testGroup() {
#expect(setupRainbow)
let group = Group {
Label("foo:")
"bar"
}
#expect(group.render() == "foo: bar")
#expect(setupRainbow)
let coloredLabel = group.labelColor(.green)
#expect(
coloredLabel.render() == """
\("foo:".green) bar
""")
}
@Test
func checkLabelColorModifier() {
#expect(setupRainbow)
let group = Group(separator: "\n") {
Label("Foo:")
Group(separator: "\n") {
"Bar"
Label("baz:")
Group {
Label("Bang")
"boom"
}
.labelColor(.green)
Label { "Foo:" }
"Bar"
"Baz"
Note { "Bang:" } content: { "boom" }
if setupRainbow {
Label("Hello, rainbow").color(.blue)
} else {
Label("No color for you!").color(.red)
}
}
.labelColor(.blue)
.color(.green)
.style(.italic)
print(type(of: group))
print(type(of: group.body))
print(group.render())
let expected = """
\("Foo:".blue)
Bar
\("baz:".blue)
\("Bang".green) boom
"""
#expect(group.render() == expected)
// var foo = group
// if var bar = group as? ModifiedNode<Group, GroupLabelModifier> {
// print("Modified Node")
// bar.modifier = bar.modifier.concat(GroupLabelModifier(color: .green))
// print(type(of: bar.body))
// }
// let note = Note { "Bang:" } content: { "boom" }
// print(note.render())
// print(type(of: note.label))
}
@Test
func checkNote() {
func testExamples() {
#expect(setupRainbow)
let note = Note {
"My note..."
}
.labelColor(.yellow)
let examples = Examples(examples: [("First", "ls -lah"), ("Second", "find . -name foo")], header: { "Examples:" }, label: { "Common examples." })
#expect(note.render() == "\("NOTE:".yellow) My note...")
print(examples.render())
}