feat: Adds vertical and horizontal separators, renames modifier protocol.

This commit is contained in:
2024-12-05 22:38:42 -05:00
parent bef91c5277
commit 9985b55f88
15 changed files with 253 additions and 93 deletions

View File

@@ -0,0 +1,66 @@
public enum Separator {
/// Represents a horizontal separator that can be used between text nodes, typically inside
/// an ``HStack``
public enum Horizontal: TextNode {
/// Separate nodes by spaces of the given count.
case space(count: Int = 1)
/// Separate nodes by tabs of the given count.
case tab(count: Int = 1)
/// Separate nodes by the provided string of the given count.
case custom(String, count: Int = 1)
@TextBuilder
@inlinable
public var body: some TextNode {
switch self {
case let .tab(count: count):
seperator("\t", count: count)
case let .space(count: count):
seperator(" ", count: count)
case let .custom(string, count: count):
seperator(string, count: count)
}
}
}
/// Represents a vertical separator that can be used between text nodes, typically inside
/// a ``VStack``
public enum Vertical: TextNode {
case newLine(count: Int = 1)
case custom(String, count: Int = 1)
@TextBuilder
@inlinable
public var body: some TextNode {
switch self {
case let .newLine(count: count):
seperator("\n", count: count)
case let .custom(string, count: count):
seperator(string, count: count)
}
}
}
}
@usableFromInline
func ensuredCount(_ count: Int) -> Int {
guard count >= 1 else { return 1 }
return count
}
@usableFromInline
func seperator(_ separator: String, count: Int) -> some TextNode {
let count = ensuredCount(count)
assert(count >= 1, "Invalid count while creating a separator")
var output = ""
for _ in 1 ... count {
output += separator
}
return output
}