tablecli

package module
v0.0.0-...-19a4819 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: MIT Imports: 6 Imported by: 3

README

tablecli

GoDoc Go Report Card CI

Packages provide a convenient way to generate tabular output of any data, useful primarily for CLI tools.

Girl in a jacket
Example of use:
package main

import (
    table "github.com/MaxwelMazur/tablecli"
    "github.com/fatih/color"
    "strings"
)


type list struct {
    ID string 
    Name string
}

func main() {
    tbl := table.New("ID", "NAME")
    headerFmt := color.New(color.FgBlue, color.Underline).SprintfFunc()
    columnFmt := color.New(color.FgGreen).SprintfFunc()
    tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)

    var list = []list{
        { "123123", "Jonh"},
        { "123121", "Jeff"},
    }

    for _, i := range list {
	tbl.AddRow(i.ID, i.Name)
    }

    format := strings.Repeat("%s", len(tbl.GetHeader())) + "\n"
    tbl.CalculateWidths([]string{})

    tbl.PrintHeader(format)
	for _, r := range tbl.GetRows() {
	    tbl.PrintRow(format, r)
    }
}

Output:
ID      NAME  
123123  Jonh  
123121  Jeff 

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultPadding                        = 2
	DefaultWriter               io.Writer = os.Stdout
	DefaultHeaderFormatter      Formatter
	DefaultFirstColumnFormatter Formatter
	DefaultWidthFunc            WidthFunc = utf8.RuneCountInString
	WidthPersist                []int
)

Functions

This section is empty.

Types

type Formatter

type Formatter func(string, ...interface{}) string

type Table

type Table interface {
	WithHeaderFormatter(f Formatter) Table
	WithFirstColumnFormatter(f Formatter) Table
	WithPadding(p int) Table
	WithWriter(w io.Writer) Table
	WithWidthFunc(f WidthFunc) Table
	AddRow(vals ...interface{}) Table
	AddRows(rows []string) Table
	SetRows(Rows [][]string) Table
	Print()
	GetByteFormat() []byte
	CalculateWidths([]string)
	GetHeader() []string
	GetRows() [][]string
	GetWriter() io.Writer
	PrintHeader(format string)
	PrintRow(format string, row []string)
}

func New

func New(columnHeaders ...interface{}) Table

func NewTable

func NewTable(columns []string) Table

type WidthFunc

type WidthFunc func(string) int

Jump to

Keyboard shortcuts

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