golang超简单表格生成插件库tabby的使用

Golang超简单表格生成插件库Tabby的使用

Tabby是一个极简的Golang表格生成库,只有约70行代码,非常适合在终端输出简单表格格式的内容。

安装Tabby

go get github.com/cheynewallace/tabby

导入Tabby

import "github.com/cheynewallace/tabby"

基本使用示例

带标题的表格

t := tabby.New()
t.AddHeader("NAME", "TITLE", "DEPARTMENT")  // 添加表头
t.AddLine("John Smith", "Developer", "Engineering")  // 添加数据行
t.Print()  // 打印表格

输出结果:

NAME        TITLE      DEPARTMENT
----        -----      ----------
John Smith  Developer  Engineering

不带标题的表格

t := tabby.New()
t.AddLine("Info:", "WEB", "Success 200")
t.AddLine("Info:", "API", "Success 201")
t.AddLine("Error:", "DATABASE", "Connection Established")
t.Print()

输出结果:

Info:   WEB       Success 200
Info:   API       Success 201
Error:  DATABASE  Connection Established

自定义tabWriter

w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
t := tabby.NewCustom(w)

完整示例

默认输出到标准输出

package main

import "github.com/cheynewallace/tabby"

func main() {
    t := tabby.New()
    t.AddHeader("NAME", "TITLE", "DEPARTMENT")
    t.AddLine("John Smith", "Developer", "Engineering")
    t.Print()
}

输出到文件

package main

import (
    "os"
    "text/tabwriter"

    "github.com/cheynewallace/tabby"
)

func main() {
    fd, _ := os.OpenFile("test.txt", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
    defer fd.Close()

    w := tabwriter.NewWriter(fd, 0, 0, 4, ' ', 0)
    t := tabby.NewCustom(w)
    t.AddHeader("NAME", "TITLE", "DEPARTMENT")
    t.AddLine("John Smith", "Developer", "Engineering")
    t.Print()
}

Tabby是一个非常轻量级的库,适合需要简单表格输出的场景,避免了复杂表格库的臃肿和过度设计。


更多关于golang超简单表格生成插件库tabby的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang超简单表格生成插件库tabby的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


Golang表格生成插件库tabby使用指南

tabby是一个简单易用的Golang表格生成库,可以快速将数据格式化为美观的表格输出到控制台。下面我将详细介绍tabby的基本用法和常见场景。

安装tabby

go get github.com/cheynewallace/tabby

基本用法

1. 创建简单表格

package main

import (
	"github.com/cheynewallace/tabby"
)

func main() {
	t := tabby.New()
	t.AddHeader("ID", "Name", "Email")
	t.AddLine("1", "John Doe", "john@example.com")
	t.AddLine("2", "Jane Smith", "jane@example.com")
	t.Print()
}

输出结果:

ID  NAME       EMAIL
1   John Doe   john@example.com
2   Jane Smith jane@example.com

2. 自定义表格样式

func main() {
	t := tabby.New()
	t.AddHeader("ID", "Name", "Age")
	t.AddLine("101", "Michael", 28)
	t.AddLine("102", "Sarah", 32)
	t.AddLine("103", "David", 25)
	
	// 自定义分隔符
	t.SetLineSeparator("-")
	
	// 自定义对齐方式
	t.SetAlignRight(1) // 第2列右对齐
	
	t.Print()
}

3. 从结构体生成表格

type User struct {
	ID    int
	Name  string
	Email string
}

func main() {
	users := []User{
		{1, "Alice", "alice@example.com"},
		{2, "Bob", "bob@example.com"},
		{3, "Charlie", "charlie@example.com"},
	}

	t := tabby.New()
	t.AddHeader("ID", "Name", "Email")
	
	for _, user := range users {
		t.AddLine(user.ID, user.Name, user.Email)
	}
	
	t.Print()
}

高级功能

1. 自定义颜色

import (
	"github.com/fatih/color"
	"github.com/cheynewallace/tabby"
)

func main() {
	red := color.New(color.FgRed).SprintFunc()
	green := color.New(color.FgGreen).SprintFunc()
	
	t := tabby.New()
	t.AddHeader("Status", "Message")
	t.AddLine(red("ERROR"), "Failed to connect")
	t.AddLine(green("OK"), "Connection established")
	t.Print()
}

2. 动态列宽

tabby会自动根据内容调整列宽:

func main() {
	t := tabby.New()
	t.AddHeader("Short", "Very Long Column Header", "Medium")
	t.AddLine("A", "This is a very long piece of text", "Normal")
	t.AddLine("B", "Short", "Another medium length text")
	t.Print()
}

3. 空值处理

func main() {
	t := tabby.New()
	t.AddHeader("ID", "Name", "Description")
	t.AddLine(1, "Product A", "High quality")
	t.AddLine(2, "Product B", nil) // nil值会显示为空
	t.AddLine(3, "Product C", "")
	t.Print()
}

实际应用示例

1. 数据库查询结果展示

import (
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
	"github.com/cheynewallace/tabby"
)

func main() {
	db, err := sql.Open("mysql", "user:password@/dbname")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	rows, err := db.Query("SELECT id, name, email FROM users LIMIT 5")
	if err != nil {
		panic(err)
	}
	defer rows.Close()

	t := tabby.New()
	t.AddHeader("ID", "Name", "Email")

	for rows.Next() {
		var id int
		var name, email string
		err = rows.Scan(&id, &name, &email)
		if err != nil {
			panic(err)
		}
		t.AddLine(id, name, email)
	}
	
	t.Print()
}

2. 命令行工具输出

func main() {
	t := tabby.New()
	t.AddHeader("Command", "Description", "Example")
	t.AddLine("add", "Add a new item", "todo add 'Buy milk'")
	t.AddLine("list", "List all items", "todo list")
	t.AddLine("done", "Mark item as done", "todo done 1")
	t.Print()
}

注意事项

  1. tabby主要用于控制台输出,不适合生成HTML或PDF表格
  2. 对于大量数据(超过1000行),考虑分页显示
  3. 中文字符对齐可能需要额外处理,因为中英文字符宽度不同

tabby是一个非常轻量级的库,适合快速生成简单的表格输出。如果需要更复杂的表格功能,可以考虑其他更强大的库如tablewriter等。

回到顶部