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

@@ -0,0 +1,73 @@
@resultBuilder
public enum TextBuilder {
@inlinable
public static func buildPartialBlock<N: TextNode>(first: N) -> N {
first
}
@inlinable
public static func buildPartialBlock<N0: TextNode, N1: TextNode>(accumulated: N0, next: N1) -> NodeContainer {
.init(nodes: [accumulated, next])
}
@inlinable
public static func buildArray<N: TextNode>(_ components: [N]) -> NodeContainer {
.init(nodes: components)
}
@inlinable
public static func buildBlock<N: TextNode>(_ components: N...) -> NodeContainer {
.init(nodes: components)
}
@inlinable
public static func buildEither<N: TextNode, N1: TextNode>(first component: N) -> EitherNode<N, N1> {
.first(component)
}
@inlinable
public static func buildEither<N: TextNode, N1: TextNode>(second component: N1) -> EitherNode<N, N1> {
.second(component)
}
@inlinable
public static func buildOptional<N: TextNode>(_ component: N?) -> N? {
component
}
}
public enum EitherNode<N: TextNode, N1: TextNode>: TextNode {
case first(N)
case second(N1)
public var body: some TextNode {
switch self {
case let .first(node): return node.eraseToAnyTextNode()
case let .second(node): return node.eraseToAnyTextNode()
}
}
}
public struct NodeContainer: TextNode {
@usableFromInline
var nodes: [any TextNode]
@usableFromInline
init(nodes: [any TextNode]) {
self.nodes = nodes.reduce(into: [any TextNode]()) { array, next in
if let many = next as? NodeContainer {
array += many.nodes
} else {
array.append(next)
}
}
}
@inlinable
public var body: some TextNode {
nodes.reduce("") { $0 + $1.render() }
}
}

View File

@@ -0,0 +1,24 @@
import Rainbow
public extension TextNode {
@inlinable
func color(_ color: NamedColor) -> some TextNode {
modifier(ColorModifier(color: color))
}
}
@usableFromInline
struct ColorModifier<Content: TextNode>: NodeModifier {
@usableFromInline
let color: NamedColor
@usableFromInline
init(color: NamedColor) {
self.color = color
}
@usableFromInline
func render(content: Content) -> some TextNode {
content.render().applyingColor(color)
}
}

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

@@ -0,0 +1,35 @@
import Rainbow
public extension TextNode {
func labelStyle<C: TextNode>(color: NamedColor? = nil, styles: [Style] = []) -> some TextNode where Self == Label<C> {
modifier(LabelStyle(color: color, styles: styles))
}
func labelStyle<C: TextNode>(color: NamedColor? = nil, styles: Style...) -> some TextNode where Self == Label<C> {
labelStyle(color: color, styles: styles)
}
}
public struct LabelStyle<C: TextNode>: NodeModifier {
@usableFromInline
let color: NamedColor?
@usableFromInline
let styles: [Style]
@usableFromInline
init(color: NamedColor? = nil, styles: [Style] = []) {
self.color = color
self.styles = styles
}
@inlinable
public func render(content: Label<C>) -> some TextNode {
var label: any TextNode = content.content
label = label.style(styles)
if let color {
label = label.color(color)
}
return Label { label.eraseToAnyTextNode() }
}
}

View File

@@ -0,0 +1,32 @@
import Rainbow
public extension TextNode {
@inlinable
func style(_ styles: Style...) -> some TextNode {
modifier(StyleModifier(styles: styles))
}
@inlinable
func style(_ styles: [Style]) -> some TextNode {
modifier(StyleModifier(styles: styles))
}
}
@usableFromInline
struct StyleModifier<Content: TextNode>: NodeModifier {
@usableFromInline
let styles: [Style]
@usableFromInline
init(styles: [Style]) {
self.styles = styles
}
@usableFromInline
func render(content: Content) -> some TextNode {
styles.reduce(content.render()) {
$0.applyingStyle($1)
}
}
}

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

@@ -0,0 +1,61 @@
public protocol NodeRepresentable {
func render() -> String
}
public protocol TextNode: NodeRepresentable {
// swiftlint:disable type_name
associatedtype _Body: TextNode
typealias Body = _Body
// swiftlint:enable type_name
var body: Body { get }
}
public extension TextNode {
func render() -> String {
body.render()
}
}
extension String: NodeRepresentable {
public func render() -> String {
self
}
}
extension String: TextNode {
public var body: some TextNode {
self
}
}
public struct AnyTextNode: TextNode {
let makeString: () -> String
public init<N: TextNode>(_ node: N) {
self.makeString = node.render
}
public var body: some TextNode { makeString() }
}
public extension TextNode {
func eraseToAnyTextNode() -> AnyTextNode {
AnyTextNode(self)
}
}
extension Optional: TextNode where Wrapped: TextNode {
public var body: some TextNode {
guard let node = self else { return "".eraseToAnyTextNode() }
return node.eraseToAnyTextNode()
}
}
extension Optional: NodeRepresentable where Wrapped: NodeRepresentable {
public func render() -> String {
guard let node = self else { return "" }
return node.render()
}
}