A tiny library for super simple Golang tables

Mentioned in Awesome Go

Tabby

A tiny library for super simple Golang tables

Get Tabby

go get github.com/cheynewallace/tabby

Import Tabby

import "github.com/cheynewallace/tabby"

Tabby is a tiny (around 70 lines of code) efficient libary for writing extremely simple table based terminal output in Golang.

Many table libraries out there are overly complicated and packed with features you don't need. If you simply want to write clean output to your terminal in table format with minimal effort, Tabby is for you.

Typical examples

  • Writing simple tables with heading and tab spaced columns
  • Writing log lines to the terminal with evenly spaced columns

Example With Heading

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

Output

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

Example Without Heading

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

Output

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

Example With Custom tabWriter

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

Full Example

  • Default writer (os.Stdout) example:
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()
}
  • File writer example:
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()
}
Comments
  • Update file writer example and optimized addSeparator function

    Update file writer example and optimized addSeparator function

    1.Remove repeat word 'with'

    2.Update file writer example to README.md

    3.Update fmt.Fprintln to buffer.WriteTo

    Source code:

    // addSeparator will write a new dash seperator line based on the args length
    func (t *Tabby) addSeparator(args []interface{}) {
    	var b bytes.Buffer
    	/*...*/
    	fmt.Fprintln(t.writer, b.String())
    }
    

    After changed:

    // addSeparator will write a new dash seperator line based on the args length
    func (t *Tabby) addSeparator(args []interface{}) {
    	var b bytes.Buffer
    	/*...*/
    	b.WriteString("\n")
    	b.WriteTo(t.writer)
    }
    

    Why did I make this change? I added benchmark test code of fmt.Fprintln and buffer.WriteTo in tabby_test.go, benchmark test code like this:

    func BenchmarkBuffer(b *testing.B) {
        fd, _ := os.OpenFile("temp.txt", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
    	defer fd.Close()
    	b.ResetTimer()
    	for i := 0; i < b.N; i++ {
    		var buff bytes.Buffer
    		buff.WriteString("TestString")
    		buff.WriteString("\n")
    		buff.WriteTo(fd)
    	}
    }
    
    func BenchmarkFmt(b *testing.B) {
    	fd, _ := os.OpenFile("temp.txt", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
    	defer fd.Close()
    	b.ResetTimer()
    	for i := 0; i < b.N; i++ {
    		var buff bytes.Buffer
    		buff.WriteString("TestString")
    		fmt.Fprintln(fd, buff.String())
    	}
    }
    

    Run benchmark test command:

    $ go test -run=NONE -bench="^Benchmark(Fmt|Buffer)$" -benchmem
    

    And I get benchmark test data:

    $ go test -run=NONE -bench="^Benchmark(Fmt|Buffer)$" -benchmem
    goos: windows
    goarch: amd64
    pkg: tabby
    BenchmarkBuffer-4         300000              4460 ns/op             112 B/op          1 allocs/op
    BenchmarkFmt-4            300000              5103 ns/op             112 B/op          1 allocs/op
    PASS
    ok      tabby   3.470s
    

    We can see buffer.WriteTo is more effective than fmt.Fprintln.

  • project: no license / not licensed

    project: no license / not licensed

    Hello,

    I was just about to use your project, when I saw that it isn't licensed anywhere in writing. I the country I reside, that means I don't have a license and can't use it.

    If this was intended to be an open source / freely available, I would recommend putting a license within the project.

    Thanks, -Daniel

  • AddLine can not expand variadic arguments

    AddLine can not expand variadic arguments

    AddLine accepts a variadic []interface{}. Because of this, using t.AddLine(slice...) where slice is a []string does not work.

    Propose new method "AddLineS(args []string...)."

  • How to have space inbetween tables?

    How to have space inbetween tables?

    Simple fmt.Println doesn't work really. It prints the newlines before all of the tables:

    package main
    
    import (
    	"github.com/cheynewallace/tabby"
    	"fmt"
    )
    
    func main() {
    	t := tabby.New()
    	t.AddHeader("Lice", "Präsens", "Präteritum", "Prät. Konjunktiv", "Imperativ")
    	t.AddLine("Ich", "bin", "war", "wäre", "-")
    	t.AddLine("Du", "bist", "warst", "wärest", "-")
    	t.AddLine("Er", "ist", "war", "wäre", "-")
    	fmt.Println("\n")
    	t.AddHeader("Infinitiv", "Präteritum", "Partizip II")
    	t.AddLine("sein", "war", "ist gewesen")
    
    	t.Print()
    }
    

    You get this:

    
    Lice       Präsens      Präteritum   Prät. Konjunktiv   Imperativ
    ----       --------     -----------  -----------------  ---------
    Ich        bin          war          wäre               -
    Du         bist         warst        wärest             -
    Er         ist          war          wäre               -
    Infinitiv  Präteritum   Partizip II
    ---------  -----------  -----------
    sein       war          ist gewesen
    
  • Strings in Japanese are not aligned

    Strings in Japanese are not aligned

    Strings in Japanese are not aligned, you might to use utf8.RuneCountInString instead of len to compute the length of the strings.

    Example

    • https://go.dev/play/p/CUsKLViOtlR

    Output

    COMPANY          CITY  ISSUE
    -------          ----  -----
    AAAホールディングス株式会社  東京    〇についての問い合わせ
    トトト株式会社          名古屋   〇〇できない
    桜株式会社            大阪    〇の不具合
    
  • Add colored output

    Add colored output

    Asking if you could add colored output per column and row. Alternatively, if someone knows of a golang package to use to print colored columns let me know.

  • AddHeader It's not a good name

    AddHeader It's not a good name

    AddHeader declares the table header does not necessarily add, the example below explains better my point of view

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

    OUT:

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

    SetHeader sounds better

✨ #PTerm is a modern go module to beautify console output. Featuring charts, progressbars, tables, trees, and many more 🚀 It's completely configurable and 100% cross-platform compatible.
✨ #PTerm is a modern go module to beautify console output. Featuring charts, progressbars, tables, trees, and many more 🚀 It's completely configurable and 100% cross-platform compatible.

?? PTerm | Pretty Terminal Printer A golang module to print pretty text Show Demo Code PTerm.sh | Installation | Documentation | Quick Start | Example

Dec 27, 2022
Tabular simplifies printing ASCII tables from command line utilities

tabular Tabular simplifies printing ASCII tables from command line utilities without the need to pass large sets of data to it's API. Simply define th

Oct 28, 2022
Small library for simple and convenient formatted stylized output to the console.
Small library for simple and convenient formatted stylized output to the console.

cfmt cfmt is a small library for simple and convenient formatted stylized output to the console, providing an interface that is exactly the same as th

Jan 7, 2023
Console Text Colors - The non-invasive cross-platform terminal color library does not need to modify the Print method

ctc - Console Text Colors The non-invasive cross-platform terminal color library does not need to modify the Print method Virtual unix-like environmen

Nov 9, 2022
uilive is a go library for updating terminal output in realtime
uilive is a go library for updating terminal output in realtime

uilive uilive is a go library for updating terminal output in realtime. It provides a buffered io.Writer that is flushed at a timed interval. uilive p

Dec 28, 2022
A go library to render progress bars in terminal applications
A go library to render progress bars in terminal applications

uiprogress A Go library to render progress bars in terminal applications. It provides a set of flexible features with a customizable API. Progress bar

Dec 29, 2022
A go library to improve readability in terminal apps using tabular data

uitable uitable is a go library for representing data as tables for terminal applications. It provides primitives for sizing and wrapping columns to i

Dec 30, 2022
Golang ultimate ANSI-colors that supports Printf/Sprintf methods
Golang ultimate ANSI-colors that supports Printf/Sprintf methods

Aurora Ultimate ANSI colors for Golang. The package supports Printf/Sprintf etc. TOC Installation Usage Simple Printf aurora.Sprintf Enable/Disable co

Dec 29, 2022
go-isatty - isatty for golang.

go-isatty isatty for golang Usage package main import ( "fmt" "github.com/mattn/go-isatty" "os" ) func main() { if isatty.IsTerminal(os.Stdout.F

Jan 7, 2023
A really basic thread-safe progress bar for Golang applications
A really basic thread-safe progress bar for Golang applications

progressbar A very simple thread-safe progress bar which should work on every OS without problems. I needed a progressbar for croc and everything I tr

Jan 2, 2023
Golang terminal dashboard
Golang terminal dashboard

termui termui is a cross-platform and fully-customizable terminal dashboard and widget library built on top of termbox-go. It is inspired by blessed-c

Dec 27, 2022
Go-reconcile - Super tiny go package which does reconcile planning

go-reconcile Super tiny go package which does reconcile planning: taking desired

Mar 1, 2022
RecordLite: a library (and executable) that declaratively maintains SQLite tables and views of semi-structured data

RecordLite RecordLite is a library (and executable) that declaratively maintains

May 29, 2022
Simple tables in terminal with Go

Simple tables in terminal with Go This package allows to generate and display ascii tables in the terminal, f.e.: +----+------------------+-----------

Dec 29, 2022
A super simple Lodash like utility library with essential functions that empowers the development in Go
A super simple Lodash like utility library with essential functions that empowers the development in Go

A simple Utility library for Go Go does not provide many essential built in functions when it comes to the data structure such as slice and map. This

Jan 4, 2023
✨ #PTerm is a modern go module to beautify console output. Featuring charts, progressbars, tables, trees, and many more 🚀 It's completely configurable and 100% cross-platform compatible.
✨ #PTerm is a modern go module to beautify console output. Featuring charts, progressbars, tables, trees, and many more 🚀 It's completely configurable and 100% cross-platform compatible.

?? PTerm | Pretty Terminal Printer A golang module to print pretty text Show Demo Code PTerm.sh | Installation | Documentation | Quick Start | Example

Dec 27, 2022
Tabular simplifies printing ASCII tables from command line utilities

tabular Tabular simplifies printing ASCII tables from command line utilities without the need to pass large sets of data to it's API. Simply define th

Oct 28, 2022
Fast, realtime regex-extraction, and aggregation into common formats such as histograms, numerical summaries, tables, and more!
Fast, realtime regex-extraction, and aggregation into common formats such as histograms, numerical summaries, tables, and more!

rare A file scanner/regex extractor and realtime summarizor. Supports various CLI-based graphing and metric formats (histogram, table, etc). Features

Dec 29, 2022
Utilities to prettify console output of tables, lists, progress-bars, text, etc.
Utilities to prettify console output of tables, lists, progress-bars, text, etc.

go-pretty Utilities to prettify console output of tables, lists, progress-bars, text, etc. Table Pretty-print tables into ASCII/Unicode strings.

Dec 29, 2022