Skip to content

Commit 92826f9

Browse files
committed
Make shared rendering API public for third-party plugin use
1 parent 5251349 commit 92826f9

4 files changed

Lines changed: 24 additions & 16 deletions

File tree

Sources/ContainerCommands/ListDisplayable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/// Conformers provide the column headers, row values, and a primary identifier
2020
/// for quiet mode. JSON encoding is handled separately by each command using
2121
/// its own data model.
22-
protocol ListDisplayable {
22+
public protocol ListDisplayable {
2323
/// Column headers for table output (e.g., `["ID", "IMAGE", "STATE"]`).
2424
static var tableHeader: [String] { get }
2525
/// The values for each column, matching the order of ``tableHeader``.

Sources/ContainerCommands/Network/NetworkList.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,18 @@ extension Application {
5454
}
5555

5656
extension PrintableNetwork: ListDisplayable {
57-
static var tableHeader: [String] {
57+
public static var tableHeader: [String] {
5858
["NETWORK", "STATE", "SUBNET"]
5959
}
6060

61-
var tableRow: [String] {
61+
public var tableRow: [String] {
6262
if let status {
6363
return [self.id, self.state, status.ipv4Subnet.description]
6464
}
6565
return [self.id, self.state, "none"]
6666
}
6767

68-
var quietValue: String {
68+
public var quietValue: String {
6969
self.id
7070
}
7171
}

Sources/ContainerCommands/OutputRendering.swift

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,24 @@
1717
import Foundation
1818

1919
/// Options for JSON rendering, wrapping the knobs on `JSONEncoder`.
20-
struct JSONOptions {
21-
var outputFormatting: JSONEncoder.OutputFormatting = []
22-
var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy = .deferredToDate
20+
public struct JSONOptions: Sendable {
21+
public var outputFormatting: JSONEncoder.OutputFormatting = []
22+
public var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy = .deferredToDate
2323

24-
static let compact = JSONOptions()
25-
static let prettySorted = JSONOptions(outputFormatting: [.prettyPrinted, .sortedKeys])
24+
public static let compact = JSONOptions()
25+
public static let prettySorted = JSONOptions(outputFormatting: [.prettyPrinted, .sortedKeys])
26+
27+
public init(
28+
outputFormatting: JSONEncoder.OutputFormatting = [],
29+
dateEncodingStrategy: JSONEncoder.DateEncodingStrategy = .deferredToDate
30+
) {
31+
self.outputFormatting = outputFormatting
32+
self.dateEncodingStrategy = dateEncodingStrategy
33+
}
2634
}
2735

2836
/// Renders an `Encodable` value as a JSON string.
29-
func renderJSON<T: Encodable>(_ value: T, options: JSONOptions = .compact) throws -> String {
37+
public func renderJSON<T: Encodable>(_ value: T, options: JSONOptions = .compact) throws -> String {
3038
let encoder = JSONEncoder()
3139
encoder.outputFormatting = options.outputFormatting
3240
encoder.dateEncodingStrategy = options.dateEncodingStrategy
@@ -35,15 +43,15 @@ func renderJSON<T: Encodable>(_ value: T, options: JSONOptions = .compact) throw
3543
}
3644

3745
/// Renders a list of displayable items as a table (with header) or quiet-mode identifiers.
38-
func renderList<T: ListDisplayable>(_ items: [T], quiet: Bool) -> String {
46+
public func renderList<T: ListDisplayable>(_ items: [T], quiet: Bool) -> String {
3947
if quiet {
4048
return items.map(\.quietValue).joined(separator: "\n")
4149
}
4250
return renderTable(items)
4351
}
4452

4553
/// Renders a list of displayable items as a column-aligned table with a header row.
46-
func renderTable<T: ListDisplayable>(_ items: [T]) -> String {
54+
public func renderTable<T: ListDisplayable>(_ items: [T]) -> String {
4755
var rows: [[String]] = [T.tableHeader]
4856
for item in items {
4957
rows.append(item.tableRow)
@@ -53,7 +61,7 @@ func renderTable<T: ListDisplayable>(_ items: [T]) -> String {
5361

5462
/// Writes rendered output to stdout. No-ops on empty strings to avoid blank lines
5563
/// (e.g., `container list -q` with zero results should produce no output, not a newline).
56-
func emit(_ output: String) {
64+
public func emit(_ output: String) {
5765
if !output.isEmpty {
5866
print(output)
5967
}

Sources/ContainerCommands/TableOutput.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@
1616

1717
import Foundation
1818

19-
struct TableOutput {
19+
public struct TableOutput: Sendable {
2020
private let rows: [[String]]
2121
private let spacing: Int
2222

23-
init(
23+
public init(
2424
rows: [[String]],
2525
spacing: Int = 2
2626
) {
2727
self.rows = rows
2828
self.spacing = spacing
2929
}
3030

31-
func format() -> String {
31+
public func format() -> String {
3232
var output = ""
3333
let maxLengths = self.maxLength()
3434

0 commit comments

Comments
 (0)