golang终端简单表格生成插件库simpletable的使用
Golang终端简单表格生成插件库simpletable的使用
simpletable是一个用于在终端生成和显示ASCII表格的Go语言库,具有以下主要特性:
主要特性
- 声明式风格:需要编写更多代码
- 样式支持:提供7种预定义样式(默认是MySQL风格、紧凑型、紧凑精简型、紧凑经典、Markdown、圆角和Unicode)
- 页眉和页脚:与表格主体分离
- 多行单元格支持
- 单元格内容对齐:左对齐、右对齐或居中对齐
- 行跨度:类似于HTML中的实现方式
- 快速:性能优异
安装
go get -u github.com/alexeyco/simpletable
基本示例
package main
import (
"fmt"
"github.com/alexeyco/simpletable"
)
var (
data = [][]interface{}{
{1, "Newton G. Goetz", 532.7},
{2, "Rebecca R. Edney", 1423.25},
{3, "John R. Jackson", 7526.12},
{4, "Ron J. Gomes", 123.84},
{5, "Penny R. Lewis", 3221.11},
}
)
func main() {
table := simpletable.New()
// 设置表头
table.Header = &simpletable.Header{
Cells: []*simpletable.Cell{
{Align: simpletable.AlignCenter, Text: "#"},
{Align: simpletable.AlignCenter, Text: "NAME"},
{Align: simpletable.AlignCenter, Text: "TAX"},
},
}
// 计算小计并添加行数据
subtotal := float64(0)
for _, row := range data {
r := []*simpletable.Cell{
{Align: simpletable.AlignRight, Text: fmt.Sprintf("%d", row[0].(int))},
{Text: row[1].(string)},
{Align: simpletable.AlignRight, Text: fmt.Sprintf("$ %.2f", row[2].(float64))},
}
table.Body.Cells = append(table.Body.Cells, r)
subtotal += row[2].(float64)
}
// 设置页脚
table.Footer = &simpletable.Footer{
Cells: []*simpletable.Cell{
{},
{Align: simpletable.AlignRight, Text: "Subtotal"},
{Align: simpletable.AlignRight, Text: fmt.Sprintf("$ %.2f", subtotal)},
},
}
// 设置表格样式并输出
table.SetStyle(simpletable.StyleCompactLite)
fmt.Println(table.String())
}
输出结果:
# NAME TAX
--- ------------------ ------------
1 Newton G. Goetz $ 532.70
2 Rebecca R. Edney $ 1423.25
3 John R. Jackson $ 7526.12
4 Ron J. Gomes $ 123.84
5 Penny R. Lewis $ 3221.11
--- ------------------ ------------
Subtotal $ 12827.02
单元格内容对齐
可以设置单元格内容的对齐方式:
c := &simpletable.Cell{
// 可以是 simpletable.AlignLeft(默认)、simpletable.AlignCenter 或 simpletable.AlignRight
Align: simpletable.AlignRight,
Content: "Subtotal",
}
列跨度
类似于HTML中的实现方式:
c := &simpletable.Cell{
Span: 2, // 默认: 1
Content: "Subtotal",
}
注意:默认Span
为1
。如果尝试将其设置为0
,该值仍将为1
。
更多示例
查看_example
目录中的更多示例:
01-styles-demo/ // 样式演示
02-ugly-span/ // 列跨度示例
03-benchmarks-with-others/ // 性能比较
04-multiline/ // 多行单元格示例
样式
提供6种可用样式。要查看它们,可以运行:
cd $GOPATH/src/github.com/alexeyco/simpletable/_example/01-styles-demo
go run main.go
许可证
Copyright (c) 2017 Alexey Popov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
更多关于golang终端简单表格生成插件库simpletable的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复