table

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2023 License: BSD-2-Clause Imports: 5 Imported by: 1

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/pirmd/text/table"
)

func main() {
	tab := table.New().SetMaxWidth(80).SetGrid(&table.Grid{Columns: " | ", Header: "=", BodyRows: "-", Footer: "="})

	tab.SetHeader("Column1", "Column2", "Column3")
	tab.AddRows(
		[]string{"Basic column", "Multi-line row:\n- first line\nSecond line is working too.", "Any very and interesting long line is also going to be adequatly wrapped at column boundaries."},
		[]string{"", "<- Empty columns are properly managed (as you can see on your left and right) ->", ""},
	)
	tab.SetFooter("but whatever you'll put in your table,", "the answer will be:", "42")

	fmt.Println(tab)
	
Output:

Example (With_colors)
package main

import (
	"fmt"

	"github.com/pirmd/text/ansi"
	"github.com/pirmd/text/table"
)

func main() {
	tab := table.New().SetMaxWidth(80).SetGrid(&table.Grid{Columns: " | ", Header: ansi.Bold("-"), BodyRows: "-", Footer: ansi.Bold("-")})

	tab.SetHeader("Let's put", "Some fun", "With colors")
	tab.AddRows(
		[]string{"Basic column", ansi.Green("Multi-line row:\n- first line\nSecond line is working too."), "Any very and " + ansi.Underline("interesting") + " long line is also going to be adequatly wrapped at column boundaries."},
		[]string{"", ansi.Blue("<-") + " Empty columns are properly managed (as you can see on your left and right)" + ansi.Red("->"), ""},
	)
	tab.SetFooter("but whatever you'll put in your table,", "the answer will be:", ansi.Bold(ansi.Green("42")))

	fmt.Println(tab)
	
Output:

Index

Examples

Constants

View Source
const (
	// DefaultMaxWidth defines the maximum size of a table It tries to get
	// initialized by reading the terminal width or fall-back to 80.
	DefaultMaxWidth = 80
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Grid

type Grid struct {
	// Columns is the separator pattern between two columns.
	Columns string
	// Header is the separator pattern between the header and the table's body.
	// Default to BodyRows.
	Header string
	// BodyRows is the separator pattern between two consecutive table's body
	// rows.
	BodyRows string
	// Footer is the separator pattern between the table's body and the footer.
	// Default to BodyRows.
	Footer string
}

Grid represents a set of Table's grid decoration.

type Table

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

Table represents a table.

func New

func New() *Table

New returns a new empty table, with no grid and a maximum width set-up to the terminal width to DefaultMaxWidth.

func (*Table) AddCol

func (t *Table) AddCol(columns ...[]string) *Table

AddCol adds a list of columns to the table's body.

func (*Table) AddRows

func (t *Table) AddRows(rows ...[]string) *Table

AddRows adds a list of rows to the table's body.

func (*Table) AddTabbedRows

func (t *Table) AddTabbedRows(tabbedrows ...string) *Table

AddTabbedRows adds to table's body a set of rows whose columns are separated by "\t".

func (*Table) AddTabbedText

func (t *Table) AddTabbedText(tabbedtext string) *Table

AddTabbedText adds to table's body a text whose columns are separated by "\t" and rows by "\t\n".

func (*Table) SetColWidth

func (t *Table) SetColWidth(w ...int) *Table

SetColWidth sets the table's column width. If not set, Table will auto-determined the column width based on Table's max width.

func (*Table) SetFooter

func (t *Table) SetFooter(row ...string) *Table

SetFooter sets the Table's footer (last row).

func (*Table) SetGrid

func (t *Table) SetGrid(sep *Grid) *Table

SetGrid defines the grid separators.

func (*Table) SetHeader

func (t *Table) SetHeader(row ...string) *Table

SetHeader sets the Table's header (first row).

func (*Table) SetMaxWidth

func (t *Table) SetMaxWidth(w int) *Table

SetMaxWidth sets the table maximum width.

func (*Table) String

func (t *Table) String() string

String returns a string representation of the table

func (*Table) WriteTo

func (t *Table) WriteTo(w io.Writer) (int64, error)

WriteTo actually draws the Table to an io.Writer. Columns width, if not manually defined, is automatically determined to fit Table maximum width Table's text is automatically wrapped to fit into the columns size.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳