feat: Adds stacks, working on styles

This commit is contained in:
2024-12-04 10:40:39 -05:00
parent 46c2c7ad31
commit 10119c3e51
12 changed files with 232 additions and 107 deletions

View File

@@ -12,6 +12,7 @@ public struct Examples<Header: TextNode, Label: TextNode>: TextNode {
@usableFromInline
let label: Label
@inlinable
public init(
examples: [Example],
@TextBuilder header: () -> Header,
@@ -22,19 +23,42 @@ public struct Examples<Header: TextNode, Label: TextNode>: TextNode {
self.label = label()
}
@inlinable
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") {
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 }
}
}
)
}
}
}
}
public extension Examples where Header == String, Label == String {
@inlinable
init(
header: String = "Examples:".yellow.bold,
label: String = "Some common usage examples.",
examples: [Example]
) {
self.init(examples: examples) { header } label: { label }
}
@inlinable
init(
header: String = "Examples:".yellow.bold,
label: String = "Some common usage examples.",
examples: Example...
) {
self.init(header: header, label: label, examples: examples)
}
}

View File

@@ -1,41 +1,16 @@
public struct Group: TextNode {
public struct Group<Content: TextNode>: TextNode {
@usableFromInline
var content: [any TextNode]
@usableFromInline
var separator: any TextNode
var content: Content
@inlinable
public init(
separator: any TextNode = "\n",
content: [any TextNode]
@TextBuilder content: () -> Content
) {
self.content = content
self.separator = separator
}
@inlinable
public init(
separator: any TextNode = "\n",
@TextBuilder content: () -> any TextNode
) {
// 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 {
// We didn't get a NodeContainer, so fallback to just storing
// the content.
self.content = [content]
}
self.separator = separator
self.content = content()
}
@inlinable
public var body: some TextNode {
content.map { $0.render() }.joined(separator: separator.render())
content
}
}

View File

@@ -0,0 +1,22 @@
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.map { $0.render() }.joined(separator: separator.render())
}
}

View File

@@ -7,23 +7,21 @@ public struct Note<Label: TextNode, Content: TextNode>: TextNode {
@usableFromInline
let content: Content
@usableFromInline
var separator: any TextNode = " "
@inlinable
public init(
separator: any TextNode = " ",
@TextBuilder _ label: () -> Label,
@TextBuilder content: () -> Content
) {
self.separator = separator
self.label = label()
self.content = content()
}
@inlinable
public var body: some TextNode {
Group(separator: separator, content: [label, content])
HStack {
label
content
}
}
}
@@ -31,28 +29,49 @@ public extension Note where Label == String {
@inlinable
init(
separator: any TextNode = " ",
_ label: String = "NOTE:".yellow.bold,
_ label: String = "NOTE:",
@TextBuilder content: () -> Content
) {
self.separator = separator
self.label = label
self.content = content()
}
static func important(
separator: any TextNode = " ",
_ label: String = "IMPORTANT NOTE:".red.underline,
_ label: String = "IMPORTANT NOTE:",
@TextBuilder content: () -> Content
) -> Self {
self.init(separator: separator, label, content: content)
self.init(label, content: content)
}
static func seeAlso(
separator: any TextNode = " ",
_ label: String = "SEE ALSO:".yellow.bold,
_ label: String = "SEE ALSO:",
@TextBuilder content: () -> Content
) -> Self {
self.init(separator: separator, label, content: content)
self.init(label, content: content)
}
}
public extension Note where Label == String, Content == String {
@inlinable
init(
_ label: String = "NOTE:",
content: String
) {
self.init(label) { content }
}
static func important(
_ label: String = "IMPORTANT NOTE:",
content: String
) -> Self {
self.init(label, content: content)
}
static func seeAlso(
_ label: String = "SEE ALSO:",
content: String
) -> Self {
self.init(label, content: content)
}
}

View File

@@ -15,7 +15,11 @@ public struct ShellCommand<Content: TextNode>: TextNode {
self.content = content()
}
@inlinable
public var body: some TextNode {
Group(separator: " ", content: [symbol, content])
HStack {
symbol
content
}
}
}

View File

@@ -0,0 +1,22 @@
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.map { $0.render() }.joined(separator: separator.render())
}
}