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

@@ -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)
}
}
}