feat: Moves core functionality into it's own library.

This commit is contained in:
2024-12-05 09:59:30 -05:00
parent 33356d8648
commit e7bbbec7c2
24 changed files with 464 additions and 387 deletions

View File

@@ -1,11 +0,0 @@
/// An empty text node.
public struct Empty: TextNode {
@inlinable
public init() {}
@inlinable
public var body: some TextNode {
""
}
}

View File

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

View File

@@ -1,23 +0,0 @@
public struct HStack: TextNode {
@usableFromInline
let content: [any TextNode]
@usableFromInline
let separator: any TextNode
@inlinable
public init(
spacing: Int = 1,
@TextBuilder content: () -> any TextNode
) {
self.content = array(from: content())
self.separator = seperator(" ", count: spacing > 0 ? spacing - 1 : 0)
}
@inlinable
public var body: some TextNode {
content.removingEmptys()
.joined(separator: separator.render())
}
}

View File

@@ -1,46 +0,0 @@
public struct Section<Header: TextNode, Content: TextNode, Footer: TextNode>: TextNode {
@usableFromInline
let header: Header
@usableFromInline
let content: Content
@usableFromInline
let footer: Footer
@inlinable
public init(
@TextBuilder header: () -> Header,
@TextBuilder content: () -> Content,
@TextBuilder footer: () -> Footer
) {
self.header = header()
self.content = content()
self.footer = footer()
}
public var body: some TextNode {
style(.default)
}
}
public extension Section where Footer == Empty {
@inlinable
init(
@TextBuilder header: () -> Header,
@TextBuilder content: () -> Content
) {
self.init(header: header, content: content) { Empty() }
}
}
public extension Section where Header == Empty {
@inlinable
init(
@TextBuilder content: () -> Content,
@TextBuilder footer: () -> Footer
) {
self.init(header: { Empty() }, content: content, footer: footer)
}
}

View File

@@ -1,23 +0,0 @@
public struct VStack: TextNode {
@usableFromInline
let content: [any TextNode]
@usableFromInline
let separator: any TextNode
@inlinable
public init(
spacing: Int = 1,
@TextBuilder content: () -> any TextNode
) {
self.content = array(from: content())
self.separator = seperator("\n", count: spacing > 0 ? spacing - 1 : 0)
}
@inlinable
public var body: some TextNode {
content.removingEmptys()
.joined(separator: separator.render())
}
}

View File

@@ -1,73 +0,0 @@
@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 @@
@_exported import CliDocCore

View File

@@ -1,24 +0,0 @@
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,35 +0,0 @@
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.textStyle(styles)
if let color {
label = label.color(color)
}
return Label { label.eraseToAnyTextNode() }
}
}

View File

@@ -1,30 +0,0 @@
import Rainbow
public struct NoteStyleConfiguration {
let label: any TextNode
let content: any TextNode
}
public extension Note {
func noteStyle<S: NoteStyleModifier>(_ modifier: S) -> some TextNode {
modifier.render(content: .init(label: label, content: content))
}
}
public protocol NoteStyleModifier: NodeModifier where Content == NoteStyleConfiguration {}
public extension NoteStyleModifier where Self == DefaultNoteStyle {
static var `default`: Self {
DefaultNoteStyle()
}
}
public struct DefaultNoteStyle: NoteStyleModifier {
public func render(content: NoteStyleConfiguration) -> some TextNode {
HStack {
content.label.color(.yellow).textStyle(.bold)
content.content
}
}
}

View File

@@ -1,37 +0,0 @@
public extension Section {
@inlinable
func style<S: SectionStyle>(_ style: S) -> some TextNode {
style.render(content: .init(header: header, content: content, footer: footer))
}
}
public struct SectionConfiguration {
public let header: any TextNode
public let content: any TextNode
public let footer: any TextNode
@usableFromInline
init(header: any TextNode, content: any TextNode, footer: any TextNode) {
self.header = header
self.content = content
self.footer = footer
}
}
public protocol SectionStyle: NodeModifier where Content == SectionConfiguration {}
public extension SectionStyle where Self == DefaultSectionStyle {
static var `default`: Self { DefaultSectionStyle() }
}
public struct DefaultSectionStyle: SectionStyle {
public func render(content: SectionConfiguration) -> some TextNode {
VStack {
content.header
content.content
content.footer
}
}
}

View File

@@ -1,28 +0,0 @@
import Rainbow
public struct ShellCommandConfiguration {
let symbol: any TextNode
let content: any TextNode
}
public extension ShellCommand {
func style<S: ShellCommandStyle>(_ style: S) -> some TextNode {
style.render(content: .init(symbol: symbol, content: content))
}
}
public protocol ShellCommandStyle: NodeModifier where Self.Content == ShellCommandConfiguration {}
public extension ShellCommandStyle where Self == DefaultShellCommandStyle {
static var `default`: Self { DefaultShellCommandStyle() }
}
public struct DefaultShellCommandStyle: ShellCommandStyle {
public func render(content: ShellCommandConfiguration) -> some TextNode {
HStack {
content.symbol
content.content.textStyle(.italic)
}
}
}

View File

@@ -1,32 +0,0 @@
import Rainbow
public extension TextNode {
@inlinable
func textStyle(_ styles: Style...) -> some TextNode {
modifier(StyleModifier(styles: styles))
}
@inlinable
func textStyle(_ 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,68 +0,0 @@
import Rainbow
public protocol NodeModifier {
// swiftlint:disable type_name
associatedtype _Body: TextNode
typealias Body = _Body
// swiftlint:enable type_name
associatedtype Content
@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

@@ -57,25 +57,6 @@ public struct ExampleSectionConfiguration {
}
}
// MARK: - Style
public extension ExampleSection {
func style<S: ExampleSectionStyle>(_ style: S) -> some TextNode {
style.render(content: configuration)
}
func exampleStyle<S: ExampleStyle>(_ style: S) -> some TextNode {
DefaultExamplesStyle(exampleStyle: style).render(content: configuration)
}
}
extension Array where Element == ExampleSection.Example {
func exampleStyle<S: ExampleStyle>(_ style: S) -> some TextNode {
style.render(content: .init(examples: self))
}
}
public struct ExampleConfiguration {
@usableFromInline
let examples: [ExampleSection.Example]
@@ -86,12 +67,42 @@ public struct ExampleConfiguration {
}
}
// MARK: - Style
public protocol ExampleSectionStyle: NodeModifier where Content == ExampleSectionConfiguration {}
public protocol ExampleStyle: NodeModifier where Content == ExampleConfiguration {}
public extension ExampleSectionStyle where Self == DefaultExamplesStyle {
static func `default`(exampleStyle: any ExampleStyle = .default) -> Self {
DefaultExamplesStyle(exampleStyle: exampleStyle)
public extension ExampleSection {
@inlinable
func style<S: ExampleSectionStyle>(_ style: S) -> some TextNode {
style.render(content: configuration)
}
@inlinable
func exampleStyle<S: ExampleStyle>(_ style: S) -> some TextNode {
DefaultExampleSectionStyle(exampleStyle: style).render(content: configuration)
}
}
extension Array where Element == ExampleSection.Example {
@inlinable
func exampleStyle<S: ExampleStyle>(_ style: S) -> some TextNode {
style.render(content: .init(examples: self))
}
}
public extension ExampleSectionStyle {
@inlinable
static func `default`<S: ExampleStyle>(exampleStyle: S) -> DefaultExampleSectionStyle<S> {
DefaultExampleSectionStyle(exampleStyle: exampleStyle)
}
}
public extension ExampleSectionStyle where Self == DefaultExampleSectionStyle<DefaultExampleStyle> {
@inlinable
static func `default`() -> DefaultExampleSectionStyle<DefaultExampleStyle> {
DefaultExampleSectionStyle()
}
}
@@ -101,19 +112,21 @@ public extension ExampleStyle where Self == DefaultExampleStyle {
}
}
public struct DefaultExamplesStyle: ExampleSectionStyle {
public struct DefaultExampleSectionStyle<Style: ExampleStyle>: ExampleSectionStyle {
@usableFromInline
let exampleStyle: any ExampleStyle
let exampleStyle: Style
@inlinable
public init(exampleStyle: any ExampleStyle = .default) {
public init(exampleStyle: Style) {
self.exampleStyle = exampleStyle
}
@inlinable
public func render(content: ExampleSectionConfiguration) -> some TextNode {
VStack(spacing: 2) {
Section {
exampleStyle.render(content: .init(examples: content.examples))
} header: {
HStack {
content.title
.color(.yellow)
@@ -122,13 +135,20 @@ public struct DefaultExamplesStyle: ExampleSectionStyle {
content.label
.textStyle(.italic)
}
exampleStyle.render(content: .init(examples: content.examples))
}
}
}
public extension DefaultExampleSectionStyle where Style == DefaultExampleStyle {
@inlinable
init() {
self.init(exampleStyle: .default)
}
}
public struct DefaultExampleStyle: ExampleStyle {
@inlinable
public func render(content: ExampleConfiguration) -> some TextNode {
VStack(spacing: 2) {
content.examples.map { example in

View File

@@ -72,3 +72,34 @@ public extension Note where Label == String, Content == String {
self.init(label, content: content)
}
}
public struct NoteStyleConfiguration {
let label: any TextNode
let content: any TextNode
}
public extension Note {
func noteStyle<S: NoteStyleModifier>(_ modifier: S) -> some TextNode {
modifier.render(content: .init(label: label, content: content))
}
}
// MARK: - Style
public protocol NoteStyleModifier: NodeModifier where Content == NoteStyleConfiguration {}
public extension NoteStyleModifier where Self == DefaultNoteStyle {
static var `default`: Self {
DefaultNoteStyle()
}
}
public struct DefaultNoteStyle: NoteStyleModifier {
public func render(content: NoteStyleConfiguration) -> some TextNode {
HStack {
content.label.color(.yellow).textStyle(.bold)
content.content
}
}
}

View File

@@ -1,3 +1,5 @@
import Rainbow
public struct ShellCommand<Content: TextNode>: TextNode {
@usableFromInline
@@ -30,3 +32,32 @@ public extension ShellCommand where Content == String {
self.init(symbol: symbol) { content }
}
}
public struct ShellCommandConfiguration {
let symbol: any TextNode
let content: any TextNode
}
public extension ShellCommand {
func style<S: ShellCommandStyle>(_ style: S) -> some TextNode {
style.render(content: .init(symbol: symbol, content: content))
}
}
// MARK: - Style
public protocol ShellCommandStyle: NodeModifier where Self.Content == ShellCommandConfiguration {}
public extension ShellCommandStyle where Self == DefaultShellCommandStyle {
static var `default`: Self { DefaultShellCommandStyle() }
}
public struct DefaultShellCommandStyle: ShellCommandStyle {
public func render(content: ShellCommandConfiguration) -> some TextNode {
HStack {
content.symbol
content.content.textStyle(.italic)
}
}
}

View File

@@ -1,73 +0,0 @@
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()
}
}
extension Array: TextNode where Element: TextNode {
public var body: some TextNode {
NodeContainer(nodes: self)
}
}
extension Array: NodeRepresentable where Element: NodeRepresentable {
public func render() -> String {
map { $0.render() }.joined()
}
}

View File

@@ -1,36 +0,0 @@
@usableFromInline
func array(from node: any TextNode) -> [any TextNode] {
if let container = node as? NodeContainer {
return container.nodes
} else if let array = node as? [any TextNode] {
return array
} else {
return [node]
}
}
@usableFromInline
func seperator(_ separator: String, count: Int) -> any TextNode {
assert(count >= 0, "Invalid count while creating a separator")
var output = ""
for _ in 0 ... count {
output += separator
}
return output
}
extension Array where Element == (any TextNode) {
@usableFromInline
func removingEmptys() -> [String] {
compactMap { node in
let string = node.render()
if string == "" {
return nil
}
return string
}
}
}