一个简单的 swift 库,为文本表渲染提供 API
快速文本表
一个简单的 swift 库,为文本表呈现提供 API。
安装
斯威夫特PM
dependencies: [
...
.package(url: "https://github.com/tplaymeow/swift-text-table", branch: "main"),
]
例子
让我们使用以下方法显示一些信息:TextTable
let points = [
Point(x: 12.0, y: 15.2),
Point(x: 120.0, y: 115.3),
Point(x: 1200.0, y: 1115.0),
]
let table = TextTable(points) {
TextTableColumn(title: "X", value: \.x, format: .number)
TextTableColumn(title: "Y", value: \.y, format: .number.notation(.scientific))
TextTableColumn(title: "Description") { point in
let x = point.x.formatted()
let y = point.y.formatted()
return "This point is located at (\(x), \(y))"
}
}
print(table.render())
输出:
+-------+---------+-----------------------------------------+
| X | Y | Description |
+-------+---------+-----------------------------------------+
| 12 | 1,52E1 | This point is located at (12, 15,2) |
+-------+---------+-----------------------------------------+
| 120 | 1,153E2 | This point is located at (120, 115,3) |
+-------+---------+-----------------------------------------+
| 1 200 | 1,115E3 | This point is located at (1 200, 1 115) |
+-------+---------+-----------------------------------------+
定制
TextTable
支持配置,可以使用配置块进行配置:
...
let table = TextTable(points) {
$0.cornerFence = "•"
$0.horizontalPadding = 0
} columns: {
TextTableColumn(title: "X", value: \.x, format: .number)
TextTableColumn(title: "Y", value: \.y, format: .number.notation(.scientific))
TextTableColumn(title: "Description") { point in
let x = point.x.formatted()
let y = point.y.formatted()
return "This point is located at (\(x), \(y))"
}
}
...
输出:
•-----•-------•---------------------------------------•
|X |Y |Description |
•-----•-------•---------------------------------------•
|12 |1,52E1 |This point is located at (12, 15,2) |
•-----•-------•---------------------------------------•
|120 |1,153E2|This point is located at (120, 115,3) |
•-----•-------•---------------------------------------•
|1 200|1,115E3|This point is located at (1 200, 1 115)|
•-----•-------•---------------------------------------•