tabulate

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2025 License: MIT Imports: 9 Imported by: 0

README

tabulate-go

Installation

go get github.com/simosdev/tabulate-go

Usage

package main

import (
    "time"
    "os"

    "github.com/simosdev/tabulate-go"
)

func main() {
	data := []struct {
		time   time.Time
		metric string
		count  any
	}{
		{
			time:   time.Time{},
			metric: "a",
			count:  1,
		},
		{
			time:   time.Time{},
			metric: "b",
			count:  20,
		},
		{
			time:   time.Time{},
			metric: "c",
			count:  1.23,
		},
	}

	tab := New([]string{"time", "metric", "count"})
	for _, elem := range data {
		tab.Add(
			func() string { return elem.time.Format("2006-01-02T15:04") },
			elem.metric,
			elem.count,
		)
	}

	tab.Print(os.Stdout)
	// Output:
	// |             time | metric | count |
	// |------------------|--------|-------|
	// | 0001-01-01T00:00 |      a |     1 |
	// | 0001-01-01T00:00 |      b |    20 |
	// | 0001-01-01T00:00 |      c |  1.23 |
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Tabulate

func Tabulate(cols []string, rows ...Row) string

Tabulate creates a string table from supplied rows. Columns are created in sorted ascending if all columns are not provided.

func WithStrict

func WithStrict(v bool) option

WithStrict sets strict in options. In strict mode colums need to be defined before *Tabulator.Add call.

Types

type Row

type Row map[string]any

type Tabulator

type Tabulator struct {
	// contains filtered or unexported fields
}

func New

func New(cols []string, opts ...option) *Tabulator

func (*Tabulator) Add

func (t *Tabulator) Add(values ...any)

Add supports adding values without re-defining the column names. In non-strict mode: If amount of values is larger than the defined columns, Add creates ad-hoc dynamic column names. In strict mode: Add will panic if amount of values is larger than defined columns.

Example
data := []struct {
	time   time.Time
	metric string
	count  any
}{
	{
		time:   time.Time{},
		metric: "a",
		count:  1,
	},
	{
		time:   time.Time{},
		metric: "b",
		count:  20,
	},
	{
		time:   time.Time{},
		metric: "c",
		count:  1.23,
	},
}

tab := New([]string{"time", "metric", "count"})
for _, elem := range data {
	tab.Add(func() string { return elem.time.Format("2006-01-02T15:04") }, elem.metric, elem.count)
}

tab.Print(os.Stdout)
Output:

|             time | metric | count |
|------------------|--------|-------|
| 0001-01-01T00:00 |      a |     1 |
| 0001-01-01T00:00 |      b |    20 |
| 0001-01-01T00:00 |      c |  1.23 |

func (*Tabulator) AddRow

func (t *Tabulator) AddRow(rows ...Row)

AddRow allows specifying columns and values. Colum names not already defined will be added at the end sorted ascending.

func (*Tabulator) Columns

func (t *Tabulator) Columns(cols ...string)

Columns adds specified colums to the list of columns defined for Tabulator. New columns are automatically set active to be printed if they do not already have active column specification.

func (*Tabulator) Print

func (t *Tabulator) Print(w io.Writer) error

func (*Tabulator) SetActiveColumns added in v0.3.0

func (t *Tabulator) SetActiveColumns(cols ...string)

SetActiveColumns causes only specified columns to be displayed via *Tabulator.Print

func (*Tabulator) SetActiveColumnsMap added in v0.3.0

func (t *Tabulator) SetActiveColumnsMap(colSpec map[string]bool)

SetActiveColumnsMap causes only specified columns where map value is true to be displayed via *Tabulator.Print

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL