A tiny library for super simple Golang tables

Overview

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.

    opened by nuczzz 3
  • 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

    opened by kardianos 2
  • 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...)."

    opened by aescaler-a0d 1
  • 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
    
    opened by emmggi 1
  • 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ホールディングス株式会社  東京    〇についての問い合わせ
    トトト株式会社          名古屋   〇〇できない
    桜株式会社            大阪    〇の不具合
    
    opened by johanburati 0
  • 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.

    opened by emmggi 2
  • 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

    opened by avelino 1
Releases(v1.1.1)
Owner
Cheyne Wallace
Cheyne Wallace
✨ #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

null 3.1k 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

InVision 68 Oct 28, 2022
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

Makhnev Petr 54 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

null 41 Nov 9, 2022
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

Greg Osuri 1.5k Dec 28, 2022
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

Greg Osuri 2k 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

Greg Osuri 685 Dec 30, 2022
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

Konstantin Ivanov 1.3k 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

mattn 664 Jan 7, 2023
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

Zack 3k Jan 2, 2023
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

Zack Guo 12.3k 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

Taichi Nakashima 9 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

François Saint-Jacques 22 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.: +----+------------------+-----------

Alexey Popov 408 Dec 29, 2022
✨ #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

null 3.1k 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

InVision 68 Oct 28, 2022
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

Chris LaPointe 178 Dec 29, 2022
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.

Naveen Mahalingam 1.6k Dec 29, 2022
Generate PlantUML ER diagram textual description from PostgreSQL tables

Generate PlantUML ER diagram textual description from PostgreSQL tables

Akira Chiku 480 Dec 22, 2022