|
11 | 11 | import Foundation
|
12 | 12 |
|
13 | 13 | struct DiffResultsTable {
|
14 |
| - static var columns: [(name: String, width: Int)] = [ |
15 |
| - ("Metric", 40), |
16 |
| - ("Change", 15), |
17 |
| - ("Before", 20), |
18 |
| - ("After", 20), |
19 |
| - ] |
20 |
| - static var totalWidth: Int { |
21 |
| - return columns.reduce(0, { $0 + $1.width + 3 }) - 1 |
| 14 | + struct Columns { |
| 15 | + typealias Column = (name: String, width: Int) |
| 16 | + var data: [Column] |
| 17 | + |
| 18 | + init() { |
| 19 | + data = [ |
| 20 | + ("Metric", 40), |
| 21 | + ("Change", 15), |
| 22 | + ("Before", 20), |
| 23 | + ("After", 20), |
| 24 | + ] |
| 25 | + } |
| 26 | + |
| 27 | + var beforeInfo: Column { |
| 28 | + get { data[2] } |
| 29 | + set { data[2] = newValue } |
| 30 | + } |
| 31 | + |
| 32 | + var afterInfo: Column { |
| 33 | + get { data[3] } |
| 34 | + set { data[3] = newValue } |
| 35 | + } |
| 36 | + |
| 37 | + var totalWidth: Int { |
| 38 | + data.reduce(0, { $0 + $1.width + 3 }) - 1 |
| 39 | + } |
| 40 | + |
| 41 | + var names: [String] { |
| 42 | + data.map { $0.name } |
| 43 | + } |
22 | 44 | }
|
23 | 45 |
|
24 | 46 | private(set) var output: String
|
25 |
| - init(results: DiffResults) { |
| 47 | + init(results: DiffResults, columns: Columns) { |
26 | 48 | var output = ""
|
27 | 49 |
|
28 | 50 | let allWarnings = results.analysis.flatMap { $0.warnings ?? [] }
|
29 | 51 | for warning in allWarnings {
|
30 | 52 | output += "\(warning)\n"
|
31 | 53 | }
|
32 | 54 |
|
33 |
| - output += "┌\(String(repeating: "─", count: Self.totalWidth))┐\n" |
34 |
| - output += Self.formattedRow(columnValues: Self.columns.map { $0.name }) |
35 |
| - output += "├\(String(repeating: "─", count: Self.totalWidth))┤\n" |
| 55 | + let totalWidth = columns.totalWidth |
| 56 | + output += "┌\(String(repeating: "─", count: totalWidth))┐\n" |
| 57 | + output += Self.formattedRow(columns: columns) |
| 58 | + output += "├\(String(repeating: "─", count: totalWidth))┤\n" |
36 | 59 |
|
37 | 60 | var footnoteCounter = 0
|
38 | 61 |
|
@@ -61,10 +84,16 @@ struct DiffResultsTable {
|
61 | 84 | footnoteCounter += footnotes.count
|
62 | 85 | }
|
63 | 86 |
|
64 |
| - output += Self.formattedRow(columnValues: [analysis.metricName, change, analysis.before ?? "-", analysis.after], colorInfo: colorInfo) |
| 87 | + var analysisColumns = columns |
| 88 | + analysisColumns.data[0].name = analysis.metricName |
| 89 | + analysisColumns.data[1].name = change |
| 90 | + analysisColumns.data[2].name = analysis.before ?? "-" |
| 91 | + analysisColumns.data[3].name = analysis.after |
| 92 | + |
| 93 | + output += Self.formattedRow(columns: analysisColumns, colorInfo: colorInfo) |
65 | 94 | }
|
66 | 95 |
|
67 |
| - output += "└\(String(repeating: "─", count: Self.totalWidth))┘\n" |
| 96 | + output += "└\(String(repeating: "─", count: totalWidth))┘\n" |
68 | 97 |
|
69 | 98 | let allFootnotes = results.analysis.flatMap { $0.footnotes ?? [] }
|
70 | 99 | if !allFootnotes.isEmpty {
|
@@ -117,9 +146,9 @@ struct DiffResultsTable {
|
117 | 146 | let upTo: String.Index
|
118 | 147 | }
|
119 | 148 |
|
120 |
| - private static func formattedRow(columnValues: [String], colorInfo: [ColumnColorInfo] = []) -> String { |
121 |
| - let values: [String] = columnValues.enumerated().map { (index, value) in |
122 |
| - let row = value.padding(toLength: Self.columns[index].width, withPad: " ", startingAt: 0) |
| 149 | + private static func formattedRow(columns: Columns, colorInfo: [ColumnColorInfo] = []) -> String { |
| 150 | + let values: [String] = columns.names.enumerated().map { (index, value) in |
| 151 | + let row = value.padding(toLength: columns.data[index].width, withPad: " ", startingAt: 0) |
123 | 152 | if let colorInfo = colorInfo.first(where: { $0.index == index }) {
|
124 | 153 | return String(row[..<colorInfo.upTo]).colored(colorInfo.color) + String(row[colorInfo.upTo...])
|
125 | 154 | }
|
|
0 commit comments