Skip to content

xo/tblfmt

Repository files navigation

About tblfmt

Package tblfmt provides streaming table encoders for result sets (ie, from a database), creating tables like the following:

 author_id | name                  | z
-----------+-----------------------+---
        14 | a	b	c	d  |
        15 | aoeu                 +|
           | test                 +|
           |                       |
        16 | foo\bbar              |
        17 | a	b	\r        +|
           | 	a                  |
        18 | 蒈	蒈		蒈 |
        19 | 蒈	蒈		蒈+| a+
           |                       |
(6 rows)

Additionally, there are standard encoders for JSON, CSV, HTML, unaligned and other display variants supported by usql.

Unit Tests Go Reference Discord Discussion

Installing

Install in the usual Go fashion:

$ go get -u github.com/xo/tblfmt

Using

tblfmt was designed for use by usql and Go's native database/sql types, but will handle any type with the following interface:

// ResultSet is the shared interface for a result set.
type ResultSet interface {
	Next() bool
	Scan(...interface{}) error
	Columns() ([]string, error)
	Close() error
	Err() error
	NextResultSet() bool
}

tblfmt can be used similar to the following:

// _example/example.go
package main

import (
	"log"
	"os"

	_ "github.com/lib/pq"
	"github.com/xo/dburl"
	"github.com/xo/tblfmt"
)

func main() {
	db, err := dburl.Open("postgres://booktest:booktest@localhost")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()
	res, err := db.Query("select * from authors")
	if err != nil {
		log.Fatal(err)
	}
	defer res.Close()
	enc, err := tblfmt.NewTableEncoder(
		res,
		// force minimum column widths
		tblfmt.WithWidths(20, 20),
	)
	if err = enc.EncodeAll(os.Stdout); err != nil {
		log.Fatal(err)
	}
}

Which can produce output like the following:

╔══════════════════════╦═══════════════════════════╦═══╗
β•‘ author_id            β•‘ name                      β•‘ z β•‘
╠══════════════════════╬═══════════════════════════╬═══╣
β•‘                   14 β•‘ a	b	c	d  β•‘   β•‘
β•‘                   15 β•‘ aoeu                     ↡║   β•‘
β•‘                      β•‘ test                     ↡║   β•‘
β•‘                      β•‘                           β•‘   β•‘
β•‘                    2 β•‘ 蒈	蒈		蒈 β•‘   β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•
(3 rows)

Please see the Go Reference for the full API.

Testing

Run using standard go test:

$ go test -v

About

streaming, buffered table encoder for result sets (ie from a database)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages