feat: More styles, renames some items.
This commit is contained in:
105
Sources/CliDoc/Modifiers/ExamplesStyle.swift
Normal file
105
Sources/CliDoc/Modifiers/ExamplesStyle.swift
Normal file
@@ -0,0 +1,105 @@
|
||||
import Rainbow
|
||||
|
||||
public extension ExampleSection {
|
||||
|
||||
func style<S: ExampleSectionStyle>(_ style: S) -> some TextNode {
|
||||
style.render(content: .init(
|
||||
header: header,
|
||||
label: label,
|
||||
examples: examples
|
||||
))
|
||||
}
|
||||
|
||||
func exampleStyle<S: ExampleStyle>(_ style: S) -> some TextNode {
|
||||
DefaultExamplesStyle().render(content: .init(
|
||||
header: header,
|
||||
label: label,
|
||||
examples: examples
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
extension Array where Element == ExampleSection.Example {
|
||||
func exampleStyle<S: ExampleStyle>(_ style: S) -> some TextNode {
|
||||
style.render(content: .init(examples: self))
|
||||
}
|
||||
}
|
||||
|
||||
public struct ExampleSectionConfiguration {
|
||||
@usableFromInline
|
||||
let header: any TextNode
|
||||
|
||||
@usableFromInline
|
||||
let label: any TextNode
|
||||
|
||||
@usableFromInline
|
||||
let examples: [ExampleSection.Example]
|
||||
|
||||
@usableFromInline
|
||||
init(header: any TextNode, label: any TextNode, examples: [ExampleSection.Example]) {
|
||||
self.header = header
|
||||
self.label = label
|
||||
self.examples = examples
|
||||
}
|
||||
}
|
||||
|
||||
public struct ExampleConfiguration {
|
||||
@usableFromInline
|
||||
let examples: [ExampleSection.Example]
|
||||
|
||||
@usableFromInline
|
||||
init(examples: [ExampleSection.Example]) {
|
||||
self.examples = examples
|
||||
}
|
||||
}
|
||||
|
||||
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 ExampleStyle where Self == DefaultExampleStyle {
|
||||
static var `default`: Self {
|
||||
DefaultExampleStyle()
|
||||
}
|
||||
}
|
||||
|
||||
public struct DefaultExamplesStyle: ExampleSectionStyle {
|
||||
|
||||
@usableFromInline
|
||||
let exampleStyle: any ExampleStyle
|
||||
|
||||
@inlinable
|
||||
public init(exampleStyle: any ExampleStyle = .default) {
|
||||
self.exampleStyle = exampleStyle
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func render(content: ExampleSectionConfiguration) -> some TextNode {
|
||||
VStack(spacing: 2) {
|
||||
HStack {
|
||||
content.header
|
||||
content.label
|
||||
}
|
||||
exampleStyle.render(content: .init(examples: content.examples))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct DefaultExampleStyle: ExampleStyle {
|
||||
|
||||
public func render(content: ExampleConfiguration) -> some TextNode {
|
||||
VStack(spacing: 2) {
|
||||
content.examples.map { example in
|
||||
VStack {
|
||||
Label(example.label.green.bold)
|
||||
ShellCommand { example.example }.style(.default)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,7 @@ public struct LabelStyle<C: TextNode>: NodeModifier {
|
||||
@inlinable
|
||||
public func render(content: Label<C>) -> some TextNode {
|
||||
var label: any TextNode = content.content
|
||||
label = label.style(styles)
|
||||
label = label.textStyle(styles)
|
||||
if let color {
|
||||
label = label.color(color)
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public struct DefaultNoteStyle: NoteStyleModifier {
|
||||
|
||||
public func render(content: NoteStyleConfiguration) -> some TextNode {
|
||||
HStack {
|
||||
content.label.color(.yellow).style(.bold)
|
||||
content.label.color(.yellow).textStyle(.bold)
|
||||
content.content
|
||||
}
|
||||
}
|
||||
|
||||
28
Sources/CliDoc/Modifiers/ShellCommandStyle.swift
Normal file
28
Sources/CliDoc/Modifiers/ShellCommandStyle.swift
Normal file
@@ -0,0 +1,28 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,12 @@ import Rainbow
|
||||
|
||||
public extension TextNode {
|
||||
@inlinable
|
||||
func style(_ styles: Style...) -> some TextNode {
|
||||
func textStyle(_ styles: Style...) -> some TextNode {
|
||||
modifier(StyleModifier(styles: styles))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
func style(_ styles: [Style]) -> some TextNode {
|
||||
func textStyle(_ styles: [Style]) -> some TextNode {
|
||||
modifier(StyleModifier(styles: styles))
|
||||
}
|
||||
}
|
||||
11
Sources/CliDoc/Nodes/Empty.swift
Normal file
11
Sources/CliDoc/Nodes/Empty.swift
Normal file
@@ -0,0 +1,11 @@
|
||||
/// An empty text node.
|
||||
public struct Empty: TextNode {
|
||||
|
||||
@inlinable
|
||||
public init() {}
|
||||
|
||||
@inlinable
|
||||
public var body: some TextNode {
|
||||
""
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import Rainbow
|
||||
|
||||
public struct Examples<Header: TextNode, Label: TextNode>: TextNode {
|
||||
public struct ExampleSection<Header: TextNode, Label: TextNode>: TextNode {
|
||||
public typealias Example = (label: String, example: String)
|
||||
|
||||
@usableFromInline
|
||||
@@ -25,24 +25,11 @@ public struct Examples<Header: TextNode, Label: TextNode>: TextNode {
|
||||
|
||||
@inlinable
|
||||
public var body: some TextNode {
|
||||
VStack(spacing: 2) {
|
||||
HStack {
|
||||
header
|
||||
label
|
||||
}
|
||||
VStack(spacing: 2) {
|
||||
self.examples.map { example in
|
||||
VStack {
|
||||
CliDoc.Label(example.label.green.bold)
|
||||
ShellCommand { example.example.italic }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
style(.default())
|
||||
}
|
||||
}
|
||||
|
||||
public extension Examples where Header == String, Label == String {
|
||||
public extension ExampleSection where Header == String, Label == String {
|
||||
@inlinable
|
||||
init(
|
||||
header: String = "Examples:".yellow.bold,
|
||||
|
||||
@@ -18,10 +18,7 @@ public struct Note<Label: TextNode, Content: TextNode>: TextNode {
|
||||
|
||||
@inlinable
|
||||
public var body: some TextNode {
|
||||
HStack {
|
||||
label
|
||||
content
|
||||
}
|
||||
noteStyle(.default)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,9 +17,16 @@ public struct ShellCommand<Content: TextNode>: TextNode {
|
||||
|
||||
@inlinable
|
||||
public var body: some TextNode {
|
||||
HStack {
|
||||
symbol
|
||||
content
|
||||
}
|
||||
style(.default)
|
||||
}
|
||||
}
|
||||
|
||||
public extension ShellCommand where Content == String {
|
||||
@inlinable
|
||||
init(
|
||||
_ content: String,
|
||||
symbol: any TextNode = "$"
|
||||
) {
|
||||
self.init(symbol: symbol) { content }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ func testVStack() {
|
||||
@Test
|
||||
func testNote() {
|
||||
#expect(setupRainbow)
|
||||
let note = Note(content: "Some note.").noteStyle(.default)
|
||||
let note = Note(content: "Some note.")
|
||||
let expected = """
|
||||
\("NOTE:".yellow.bold) Some note.
|
||||
"""
|
||||
@@ -55,7 +55,7 @@ func testNote() {
|
||||
@Test
|
||||
func testExamples() {
|
||||
#expect(setupRainbow)
|
||||
let examples = Examples(
|
||||
let examples = ExampleSection(
|
||||
examples: [("First", "ls -lah"), ("Second", "find . -name foo")]
|
||||
)
|
||||
|
||||
@@ -70,3 +70,36 @@ func testExamples() {
|
||||
"""
|
||||
#expect(examples.render() == expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
func testExamplesWithCustomExampleOnlyStyle() {
|
||||
#expect(setupRainbow)
|
||||
let examples = ExampleSection(
|
||||
examples: [("First", "ls -lah"), ("Second", "find . -name foo")]
|
||||
)
|
||||
.exampleStyle(CustomExampleOnlyStyle())
|
||||
|
||||
let expected = """
|
||||
\("Examples:".yellow.bold) Some common usage examples.
|
||||
|
||||
\("First".red)
|
||||
$ \("ls -lah".italic)
|
||||
|
||||
\("Second".red)
|
||||
$ \("find . -name foo".italic)
|
||||
"""
|
||||
#expect(examples.render() == expected)
|
||||
}
|
||||
|
||||
struct CustomExampleOnlyStyle: ExampleStyle {
|
||||
func render(content: ExampleConfiguration) -> some TextNode {
|
||||
VStack(spacing: 2) {
|
||||
content.examples.map { example in
|
||||
VStack {
|
||||
Label(example.label.red)
|
||||
ShellCommand { example.example }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user