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 ¶
- Constants
- type Grid
- type Table
- func (t *Table) AddCol(columns ...[]string) *Table
- func (t *Table) AddRows(rows ...[]string) *Table
- func (t *Table) AddTabbedRows(tabbedrows ...string) *Table
- func (t *Table) AddTabbedText(tabbedtext string) *Table
- func (t *Table) SetColWidth(w ...int) *Table
- func (t *Table) SetFooter(row ...string) *Table
- func (t *Table) SetGrid(sep *Grid) *Table
- func (t *Table) SetHeader(row ...string) *Table
- func (t *Table) SetMaxWidth(w int) *Table
- func (t *Table) String() string
- func (t *Table) WriteTo(w io.Writer) (int64, error)
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 // 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) AddTabbedRows ¶
AddTabbedRows adds to table's body a set of rows whose columns are separated by "\t".
func (*Table) AddTabbedText ¶
AddTabbedText adds to table's body a text whose columns are separated by "\t" and rows by "\t\n".
func (*Table) SetColWidth ¶
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) SetMaxWidth ¶
SetMaxWidth sets the table maximum width.
Click to show internal directories.
Click to hide internal directories.