tabulate

package module
v0.0.0-...-f8aa9f5 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2022 License: MIT Imports: 7 Imported by: 0

README

tabulate

Golang library to print data as tables, a port of the tabulate python library.

Installation

Should be as simple as

go get github.com/rgeoghegan/tabulate

Usage

Check the godocs, which can be found in tabulate.go.

Run tests

Do:

go test github.com/rgeoghegan/tabulate 

Documentation

Overview

Package tabulate formats data into tables.

Example Usage

The following example will show the table shown below:

    package main

    import (
        "fmt"
        "github.com/rgeoghegan/tabulate"
    )

    type Row struct {
        name  string
        count int
    }

    func main() {
        table := []*Row{
            &Row{"alpha", 1},
            &Row{"bravo", 2},
        }
        asText, _ := tabulate.Tabulate(
		      table, &tabulate.Layout{Format:tabulate.SimpleFormat},
		   )
        fmt.Print(asText)
    }

Which will print out the following:

 name count
----- -----
alpha     1
bravo     2

You can also provide a slice of slice of strings:

table := [][]string{
    []string{"alpha", "1"},
    []string{"bravo", "2"},
}
layout := &Layout{Headers:[]string{"name", "count"}, Format:tabulate.SimpleFormat}
asText, err := tabulate.Tabulate(table, layout)

Index

Constants

This section is empty.

Variables

View Source
var FancyGridFormat *gridFormatting = newGridFormat(
	"\u2502 ", " \u2502 ", " \u2502",

	&barFormat{"\u2552", '\u2550', "\u2564", "\u2555"},
	&barFormat{"\u255e", '\u2550', "\u256a", "\u2561"},
	&barFormat{"\u251c", '\u2500', "\u253c", "\u2524"},
	&barFormat{"\u2558", '\u2550', "\u2567", "\u255b"},
)

FancyGridLayout uses unicode characters to fancy up the grid:

╒════════╤════════╕
│   name │ amount │
╞════════╪════════╡
│  Apple │     15 │
├────────┼────────┤
│ Orange │      1 │
╘════════╧════════╛
View Source
var GridFormat *gridFormatting = newGridFormat(
	"| ", " | ", " |",

	&barFormat{"+", '-', "+", "+"},
	&barFormat{"+", '=', "+", "+"},
	&barFormat{"+", '-', "+", "+"},
	&barFormat{"+", '-', "+", "+"},
)

GridFormat surrounds every cell with a grid:

+--------+--------+
|   name | amount |
+========+========+
|  Apple |     15 |
+--------+--------+
| Orange |      1 |
+--------+--------+
View Source
var NoFormat spacerFormatting = ""

NoFormat has (you'll never guess) no formatting:

  nameamount
 Apple    15
Orange     1
View Source
var PipeFormat *headerFormatting = &headerFormatting{" | ", '-', nil}

PipeFormat is very similar to PlainLayout except it has a bar under the headers, and a pipe (|) between each column:

  name | amount
------ | ------
 Apple |     15
Orange |      1
View Source
var PlainFormat spacerFormatting = " "

PlainFormat uses a space between columns:

  name amount
 Apple     15
Orange      1
View Source
var SimpleFormat *headerFormatting = &headerFormatting{" ", '-', nil}

SimpleFormat is very similar to PlainLayout except it has a bar under the headers:

  name amount
------ ------
 Apple     15
Orange      1

Functions

func CombineHorizontal

func CombineHorizontal(left string, right string, padding string) string

CombineHorizontal place two tables next to one another like:

╒═══════════╤═══════════╤═══════════╕ ╒═══════════╤═══════════╤═══════════╕ │ A │ B │ C │ │ A │ B │ C │ ╞═══════════╪═══════════╪═══════════╡ ╞═══════════╪═══════════╪═══════════╡ │ A value 1 │ B value 1 │ C value 1 │ │ A value 2 │ B value 2 │ C value 2 │ ╘═══════════╧═══════════╧═══════════╛ ╘═══════════╧═══════════╧═══════════╛

func CombineVertical

func CombineVertical(top string, bottom string, padding string) string

CombineVertical place two tables verticaly like:

╒═══════════╤═══════════╤═══════════╕ │ A │ B │ C │ ╞═══════════╪═══════════╪═══════════╡ │ A value 1 │ B value 1 │ C value 1 │ ╘═══════════╧═══════════╧═══════════╛ ╒═══════════╤═══════════╤═══════════╕ │ A │ B │ C │ ╞═══════════╪═══════════╪═══════════╡ │ A value 2 │ B value 2 │ C value 2 │ ╘═══════════╧═══════════╧═══════════╛

func Tabulate

func Tabulate(data interface{}, layout *Layout) (string, error)

Tabulate will tabulate the provided data with the given layout. If no format is specified in the layout, it will use a simple format by default.

Data

The data parameter must either be a slice of structs, and the table will use the field names of the struct as column names. If provided a slice of slices of strings, you will need to provide a list of Headers (mostly so it can figure out how many columns to size for).

Types

type Layout

type Layout struct {
	Format      TableFormatterInterface
	HideHeaders bool
	Headers     []string
}

Layout specifies the general layout of the table. Provide Headers to show a custom list of headings at the top of the table. Set HideHeaders to false to not show Headers.

type TableFormatterInterface

type TableFormatterInterface interface {
	// Passed in a list of column widths (including the header if shown)
	// before drawing the table. Save the widths if you need (for example)
	// to show a bar across a row.
	RegisterWidths([]int)

	// Spacer returns the string to join between the columns (but not
	// before the first column or after the last one)
	Spacer() string
	// Line prefix is shown before the first column.
	LinePrefix() string
	// Line prefix is shown after the last column. Should not contain a return
	// line.
	LinePostfix() string

	// This string appears at the top of the table. Should not contain a
	// return line.
	AboveTable() string
	// This string appears in the table, right after the header. Should not
	// contain a return line.
	BelowHeader() string
	// This string appears in the table, between every "normal" row. Should
	// not contain a return line.
	BetweenRow(index int) string
	// This string appears at the bottom of the table. Should not contain a
	// return line.
	BelowTable() string
}

TableFormatterInterface determines how a layout will format the table. Create your own implementation if you need a custom format.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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