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
|
@inlinable
|
||||||
public func render(content: Label<C>) -> some TextNode {
|
public func render(content: Label<C>) -> some TextNode {
|
||||||
var label: any TextNode = content.content
|
var label: any TextNode = content.content
|
||||||
label = label.style(styles)
|
label = label.textStyle(styles)
|
||||||
if let color {
|
if let color {
|
||||||
label = label.color(color)
|
label = label.color(color)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ public struct DefaultNoteStyle: NoteStyleModifier {
|
|||||||
|
|
||||||
public func render(content: NoteStyleConfiguration) -> some TextNode {
|
public func render(content: NoteStyleConfiguration) -> some TextNode {
|
||||||
HStack {
|
HStack {
|
||||||
content.label.color(.yellow).style(.bold)
|
content.label.color(.yellow).textStyle(.bold)
|
||||||
content.content
|
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 {
|
public extension TextNode {
|
||||||
@inlinable
|
@inlinable
|
||||||
func style(_ styles: Style...) -> some TextNode {
|
func textStyle(_ styles: Style...) -> some TextNode {
|
||||||
modifier(StyleModifier(styles: styles))
|
modifier(StyleModifier(styles: styles))
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
func style(_ styles: [Style]) -> some TextNode {
|
func textStyle(_ styles: [Style]) -> some TextNode {
|
||||||
modifier(StyleModifier(styles: styles))
|
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
|
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)
|
public typealias Example = (label: String, example: String)
|
||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
@@ -25,24 +25,11 @@ public struct Examples<Header: TextNode, Label: TextNode>: TextNode {
|
|||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public var body: some TextNode {
|
public var body: some TextNode {
|
||||||
VStack(spacing: 2) {
|
style(.default())
|
||||||
HStack {
|
|
||||||
header
|
|
||||||
label
|
|
||||||
}
|
|
||||||
VStack(spacing: 2) {
|
|
||||||
self.examples.map { example in
|
|
||||||
VStack {
|
|
||||||
CliDoc.Label(example.label.green.bold)
|
|
||||||
ShellCommand { example.example.italic }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public extension Examples where Header == String, Label == String {
|
public extension ExampleSection where Header == String, Label == String {
|
||||||
@inlinable
|
@inlinable
|
||||||
init(
|
init(
|
||||||
header: String = "Examples:".yellow.bold,
|
header: String = "Examples:".yellow.bold,
|
||||||
|
|||||||
@@ -18,10 +18,7 @@ public struct Note<Label: TextNode, Content: TextNode>: TextNode {
|
|||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public var body: some TextNode {
|
public var body: some TextNode {
|
||||||
HStack {
|
noteStyle(.default)
|
||||||
label
|
|
||||||
content
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,9 +17,16 @@ public struct ShellCommand<Content: TextNode>: TextNode {
|
|||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public var body: some TextNode {
|
public var body: some TextNode {
|
||||||
HStack {
|
style(.default)
|
||||||
symbol
|
}
|
||||||
content
|
}
|
||||||
}
|
|
||||||
|
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
|
@Test
|
||||||
func testNote() {
|
func testNote() {
|
||||||
#expect(setupRainbow)
|
#expect(setupRainbow)
|
||||||
let note = Note(content: "Some note.").noteStyle(.default)
|
let note = Note(content: "Some note.")
|
||||||
let expected = """
|
let expected = """
|
||||||
\("NOTE:".yellow.bold) Some note.
|
\("NOTE:".yellow.bold) Some note.
|
||||||
"""
|
"""
|
||||||
@@ -55,7 +55,7 @@ func testNote() {
|
|||||||
@Test
|
@Test
|
||||||
func testExamples() {
|
func testExamples() {
|
||||||
#expect(setupRainbow)
|
#expect(setupRainbow)
|
||||||
let examples = Examples(
|
let examples = ExampleSection(
|
||||||
examples: [("First", "ls -lah"), ("Second", "find . -name foo")]
|
examples: [("First", "ls -lah"), ("Second", "find . -name foo")]
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -70,3 +70,36 @@ func testExamples() {
|
|||||||
"""
|
"""
|
||||||
#expect(examples.render() == expected)
|
#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