tablewriter: github.com/olekukonko/tablewriter Index | Examples | Files | Directories

package tablewriter

import "github.com/olekukonko/tablewriter"

Create & Generate text based table

Code:

var multiline = `A multiline
string with some lines being really long.`

const (
    testRow = iota
    testHeader
    testFooter
    testFooter2
)
for mode := testRow; mode <= testFooter2; mode++ {
    for _, autoFmt := range []bool{false, true} {
        if mode == testRow && autoFmt {
            // Nothing special to test, skip
            continue
        }
        for _, autoWrap := range []bool{false, true} {
            for _, reflow := range []bool{false, true} {
                if !autoWrap && reflow {
                    // Invalid configuration, skip
                    continue
                }
                fmt.Println("mode", mode, "autoFmt", autoFmt, "autoWrap", autoWrap, "reflow", reflow)
                t := NewWriter(os.Stdout)
                t.SetAutoFormatHeaders(autoFmt)
                t.SetAutoWrapText(autoWrap)
                t.SetReflowDuringAutoWrap(reflow)
                if mode == testHeader {
                    t.SetHeader([]string{"woo", multiline})
                } else {
                    t.SetHeader([]string{"woo", "waa"})
                }
                if mode == testRow {
                    t.Append([]string{"woo", multiline})
                } else {
                    t.Append([]string{"woo", "waa"})
                }
                if mode == testFooter {
                    t.SetFooter([]string{"woo", multiline})
                } else if mode == testFooter2 {
                    t.SetFooter([]string{"", multiline})
                } else {
                    t.SetFooter([]string{"woo", "waa"})
                }
                t.Render()
            }
        }
    }
    fmt.Println()
}

Output:

mode 0 autoFmt false autoWrap false reflow false
+-----+-------------------------------------------+
| woo |                    waa                    |
+-----+-------------------------------------------+
| woo | A multiline                               |
|     | string with some lines being really long. |
+-----+-------------------------------------------+
| woo |                    waa                    |
+-----+-------------------------------------------+
mode 0 autoFmt false autoWrap true reflow false
+-----+--------------------------------+
| woo |              waa               |
+-----+--------------------------------+
| woo | A multiline                    |
|     |                                |
|     | string with some lines being   |
|     | really long.                   |
+-----+--------------------------------+
| woo |              waa               |
+-----+--------------------------------+
mode 0 autoFmt false autoWrap true reflow true
+-----+--------------------------------+
| woo |              waa               |
+-----+--------------------------------+
| woo | A multiline string with some   |
|     | lines being really long.       |
+-----+--------------------------------+
| woo |              waa               |
+-----+--------------------------------+

mode 1 autoFmt false autoWrap false reflow false
+-----+-------------------------------------------+
| woo |                A multiline                |
|     | string with some lines being really long. |
+-----+-------------------------------------------+
| woo | waa                                       |
+-----+-------------------------------------------+
| woo |                    waa                    |
+-----+-------------------------------------------+
mode 1 autoFmt false autoWrap true reflow false
+-----+--------------------------------+
| woo |          A multiline           |
|     |                                |
|     |  string with some lines being  |
|     |          really long.          |
+-----+--------------------------------+
| woo | waa                            |
+-----+--------------------------------+
| woo |              waa               |
+-----+--------------------------------+
mode 1 autoFmt false autoWrap true reflow true
+-----+--------------------------------+
| woo |  A multiline string with some  |
|     |    lines being really long.    |
+-----+--------------------------------+
| woo | waa                            |
+-----+--------------------------------+
| woo |              waa               |
+-----+--------------------------------+
mode 1 autoFmt true autoWrap false reflow false
+-----+-------------------------------------------+
| WOO |                A MULTILINE                |
|     | STRING WITH SOME LINES BEING REALLY LONG  |
+-----+-------------------------------------------+
| woo | waa                                       |
+-----+-------------------------------------------+
| WOO |                    WAA                    |
+-----+-------------------------------------------+
mode 1 autoFmt true autoWrap true reflow false
+-----+--------------------------------+
| WOO |          A MULTILINE           |
|     |                                |
|     |  STRING WITH SOME LINES BEING  |
|     |          REALLY LONG           |
+-----+--------------------------------+
| woo | waa                            |
+-----+--------------------------------+
| WOO |              WAA               |
+-----+--------------------------------+
mode 1 autoFmt true autoWrap true reflow true
+-----+--------------------------------+
| WOO |  A MULTILINE STRING WITH SOME  |
|     |    LINES BEING REALLY LONG     |
+-----+--------------------------------+
| woo | waa                            |
+-----+--------------------------------+
| WOO |              WAA               |
+-----+--------------------------------+

mode 2 autoFmt false autoWrap false reflow false
+-----+-------------------------------------------+
| woo |                    waa                    |
+-----+-------------------------------------------+
| woo | waa                                       |
+-----+-------------------------------------------+
| woo |                A multiline                |
|     | string with some lines being really long. |
+-----+-------------------------------------------+
mode 2 autoFmt false autoWrap true reflow false
+-----+--------------------------------+
| woo |              waa               |
+-----+--------------------------------+
| woo | waa                            |
+-----+--------------------------------+
| woo |          A multiline           |
|     |                                |
|     |  string with some lines being  |
|     |          really long.          |
+-----+--------------------------------+
mode 2 autoFmt false autoWrap true reflow true
+-----+--------------------------------+
| woo |              waa               |
+-----+--------------------------------+
| woo | waa                            |
+-----+--------------------------------+
| woo |  A multiline string with some  |
|     |    lines being really long.    |
+-----+--------------------------------+
mode 2 autoFmt true autoWrap false reflow false
+-----+-------------------------------------------+
| WOO |                    WAA                    |
+-----+-------------------------------------------+
| woo | waa                                       |
+-----+-------------------------------------------+
| WOO |                A MULTILINE                |
|     | STRING WITH SOME LINES BEING REALLY LONG  |
+-----+-------------------------------------------+
mode 2 autoFmt true autoWrap true reflow false
+-----+--------------------------------+
| WOO |              WAA               |
+-----+--------------------------------+
| woo | waa                            |
+-----+--------------------------------+
| WOO |          A MULTILINE           |
|     |                                |
|     |  STRING WITH SOME LINES BEING  |
|     |          REALLY LONG           |
+-----+--------------------------------+
mode 2 autoFmt true autoWrap true reflow true
+-----+--------------------------------+
| WOO |              WAA               |
+-----+--------------------------------+
| woo | waa                            |
+-----+--------------------------------+
| WOO |  A MULTILINE STRING WITH SOME  |
|     |    LINES BEING REALLY LONG     |
+-----+--------------------------------+

mode 3 autoFmt false autoWrap false reflow false
+-----+-------------------------------------------+
| woo |                    waa                    |
+-----+-------------------------------------------+
| woo | waa                                       |
+-----+-------------------------------------------+
|                      A multiline                |
|       string with some lines being really long. |
+-----+-------------------------------------------+
mode 3 autoFmt false autoWrap true reflow false
+-----+--------------------------------+
| woo |              waa               |
+-----+--------------------------------+
| woo | waa                            |
+-----+--------------------------------+
|                A multiline           |
|                                      |
|        string with some lines being  |
|                really long.          |
+-----+--------------------------------+
mode 3 autoFmt false autoWrap true reflow true
+-----+--------------------------------+
| woo |              waa               |
+-----+--------------------------------+
| woo | waa                            |
+-----+--------------------------------+
|        A multiline string with some  |
|          lines being really long.    |
+-----+--------------------------------+
mode 3 autoFmt true autoWrap false reflow false
+-----+-------------------------------------------+
| WOO |                    WAA                    |
+-----+-------------------------------------------+
| woo | waa                                       |
+-----+-------------------------------------------+
|                      A MULTILINE                |
|       STRING WITH SOME LINES BEING REALLY LONG  |
+-----+-------------------------------------------+
mode 3 autoFmt true autoWrap true reflow false
+-----+--------------------------------+
| WOO |              WAA               |
+-----+--------------------------------+
| woo | waa                            |
+-----+--------------------------------+
|                A MULTILINE           |
|                                      |
|        STRING WITH SOME LINES BEING  |
|                REALLY LONG           |
+-----+--------------------------------+
mode 3 autoFmt true autoWrap true reflow true
+-----+--------------------------------+
| WOO |              WAA               |
+-----+--------------------------------+
| woo | waa                            |
+-----+--------------------------------+
|        A MULTILINE STRING WITH SOME  |
|          LINES BEING REALLY LONG     |
+-----+--------------------------------+

Index

Examples

Package Files

csv.go table.go table_with_color.go util.go wrap.go

Constants

const (
    CENTER  = "+"
    ROW     = "-"
    COLUMN  = "|"
    SPACE   = " "
    NEWLINE = "\n"
)
const (
    ALIGN_DEFAULT = iota
    ALIGN_CENTER
    ALIGN_RIGHT
    ALIGN_LEFT
)
const (
    BgBlackColor int = iota + 40
    BgRedColor
    BgGreenColor
    BgYellowColor
    BgBlueColor
    BgMagentaColor
    BgCyanColor
    BgWhiteColor
)
const (
    FgBlackColor int = iota + 30
    FgRedColor
    FgGreenColor
    FgYellowColor
    FgBlueColor
    FgMagentaColor
    FgCyanColor
    FgWhiteColor
)
const (
    BgHiBlackColor int = iota + 100
    BgHiRedColor
    BgHiGreenColor
    BgHiYellowColor
    BgHiBlueColor
    BgHiMagentaColor
    BgHiCyanColor
    BgHiWhiteColor
)
const (
    FgHiBlackColor int = iota + 90
    FgHiRedColor
    FgHiGreenColor
    FgHiYellowColor
    FgHiBlueColor
    FgHiMagentaColor
    FgHiCyanColor
    FgHiWhiteColor
)
const (
    Normal          = 0
    Bold            = 1
    UnderlineSingle = 4
    Italic
)
const ESC = "\033"
const (
    MAX_ROW_WIDTH = 30
)
const SEP = ";"

func Color Uses

func Color(colors ...int) []int

func ConditionString Uses

func ConditionString(cond bool, valid, inValid string) string

Simple Condition for string Returns value based on condition

func DisplayWidth Uses

func DisplayWidth(str string) int

func Pad Uses

func Pad(s, pad string, width int) string

Pad String Attempts to play string in the center

func PadLeft Uses

func PadLeft(s, pad string, width int) string

Pad String Left position This would pace string at the right side fo the screen

func PadRight Uses

func PadRight(s, pad string, width int) string

Pad String Right position This would pace string at the left side fo the screen

func Title Uses

func Title(name string) string

Format Table Header Replace _ , . and spaces

func WrapString Uses

func WrapString(s string, lim int) ([]string, int)

Wrap wraps s into a paragraph of lines of length lim, with minimal raggedness.

func WrapWords Uses

func WrapWords(words []string, spc, lim, pen int) [][]string

WrapWords is the low-level line-breaking algorithm, useful if you need more control over the details of the text wrapping process. For most uses, WrapString will be sufficient and more convenient.

WrapWords splits a list of words into lines with minimal "raggedness", treating each rune as one unit, accounting for spc units between adjacent words on each line, and attempting to limit lines to lim units. Raggedness is the total error over all lines, where error is the square of the difference of the length of the line and lim. Too-long lines (which only happen when a single word is longer than lim units) have pen penalty units added to the error.

type Border Uses

type Border struct {
    Left   bool
    Right  bool
    Top    bool
    Bottom bool
}

type Colors Uses

type Colors []int

type Table Uses

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

func NewCSV Uses

func NewCSV(writer io.Writer, fileName string, hasHeader bool) (*Table, error)

Start A new table by importing from a CSV file Takes io.Writer and csv File name

func NewCSVReader Uses

func NewCSVReader(writer io.Writer, csvReader *csv.Reader, hasHeader bool) (*Table, error)
Start a New Table Writer with csv.Reader

This enables customisation such as reader.Comma = ';' See http://golang.org/src/pkg/encoding/csv/reader.go?s=3213:3671#L94

func NewWriter Uses

func NewWriter(writer io.Writer) *Table

Start New Table Take io.Writer Directly

func (*Table) Append Uses

func (t *Table) Append(row []string)

Append row to table

func (*Table) AppendBulk Uses

func (t *Table) AppendBulk(rows [][]string)

Allow Support for Bulk Append Eliminates repeated for loops

func (*Table) ClearFooter Uses

func (t *Table) ClearFooter()

Clear footer

func (*Table) ClearRows Uses

func (t *Table) ClearRows()

Clear rows

func (*Table) NumLines Uses

func (t *Table) NumLines() int

NumLines to get the number of lines

func (*Table) Render Uses

func (t *Table) Render()

Render table output

func (*Table) SetAlignment Uses

func (t *Table) SetAlignment(align int)

Set Table Alignment

func (*Table) SetAutoFormatHeaders Uses

func (t *Table) SetAutoFormatHeaders(auto bool)

Turn header autoformatting on/off. Default is on (true).

func (*Table) SetAutoMergeCells Uses

func (t *Table) SetAutoMergeCells(auto bool)

Set Auto Merge Cells This would enable / disable the merge of cells with identical values

func (*Table) SetAutoWrapText Uses

func (t *Table) SetAutoWrapText(auto bool)

Turn automatic multiline text adjustment on/off. Default is on (true).

func (*Table) SetBorder Uses

func (t *Table) SetBorder(border bool)

Set Table Border This would enable / disable line around the table

func (*Table) SetBorders Uses

func (t *Table) SetBorders(border Border)

func (*Table) SetCaption Uses

func (t *Table) SetCaption(caption bool, captionText ...string)

Set table Caption

func (*Table) SetCenterSeparator Uses

func (t *Table) SetCenterSeparator(sep string)

Set the center Separator

func (*Table) SetColMinWidth Uses

func (t *Table) SetColMinWidth(column int, width int)

Set the minimal width for a column

func (*Table) SetColWidth Uses

func (t *Table) SetColWidth(width int)

Set the Default column width

func (*Table) SetColumnAlignment Uses

func (t *Table) SetColumnAlignment(keys []int)

func (*Table) SetColumnColor Uses

func (t *Table) SetColumnColor(colors ...Colors)

Adding column colors (ANSI codes)

func (*Table) SetColumnSeparator Uses

func (t *Table) SetColumnSeparator(sep string)

Set the Column Separator

func (*Table) SetFooter Uses

func (t *Table) SetFooter(keys []string)

Set table Footer

func (*Table) SetFooterAlignment Uses

func (t *Table) SetFooterAlignment(fAlign int)

Set Footer Alignment

func (*Table) SetFooterColor Uses

func (t *Table) SetFooterColor(colors ...Colors)

Adding column colors (ANSI codes)

func (*Table) SetHeader Uses

func (t *Table) SetHeader(keys []string)

Set table header

func (*Table) SetHeaderAlignment Uses

func (t *Table) SetHeaderAlignment(hAlign int)

Set Header Alignment

func (*Table) SetHeaderColor Uses

func (t *Table) SetHeaderColor(colors ...Colors)

Adding header colors (ANSI codes)

func (*Table) SetHeaderLine Uses

func (t *Table) SetHeaderLine(line bool)

Set Header Line This would enable / disable a line after the header

func (*Table) SetNewLine Uses

func (t *Table) SetNewLine(nl string)

Set New Line

func (*Table) SetReflowDuringAutoWrap Uses

func (t *Table) SetReflowDuringAutoWrap(auto bool)

Turn automatic reflowing of multiline text when rewrapping. Default is on (true).

func (*Table) SetRowLine Uses

func (t *Table) SetRowLine(line bool)

Set Row Line This would enable / disable a line on each row of the table

func (*Table) SetRowSeparator Uses

func (t *Table) SetRowSeparator(sep string)

Set the Row Separator

Directories

PathSynopsis
csv2table

Package tablewriter imports 10 packages (graph) and is imported by 798 packages. Updated 2018-10-26. Refresh now. Tools for package owners.