Documentation ¶
Overview ¶
Example (Alignment) ¶
t := auto_table.New(decoration.D_UTF8_LIGHT_CURVED) t = populateTable(t) t.AddRowItems("Sekai-ichi apple", "10.50") t.Column(2).SetProperty(align.PropertyType, align.Right) if err := t.RenderTo(os.Stdout); err != nil { fmt.Fprintf(os.Stderr, "table rendering error: %s\n", err) }
Output: ╭──────────────────┬────────────╮ │ Fruit │ Price $/lb │ ├──────────────────┼────────────┤ │ bananas │ 0.56 │ │ apples │ 1.31 │ │ strawberries │ 2.22 │ │ pears │ 1.90 │ │ Sekai-ichi apple │ 10.50 │ ╰──────────────────┴────────────╯
Example (Alignment2) ¶
t := auto_table.New(decoration.D_UTF8_LIGHT_CURVED) t = populateTable(t) t.AddRowItems("Sekai-ichi apple", "10.50") t.Column(0).SetProperty(align.PropertyType, align.Right) t.Column(1).SetProperty(align.PropertyType, align.Center) if err := t.RenderTo(os.Stdout); err != nil { fmt.Fprintf(os.Stderr, "table rendering error: %s\n", err) }
Output: ╭──────────────────┬────────────╮ │ Fruit │ Price $/lb │ ├──────────────────┼────────────┤ │ bananas │ 0.56 │ │ apples │ 1.31 │ │ strawberries │ 2.22 │ │ pears │ 1.90 │ │ Sekai-ichi apple │ 10.50 │ ╰──────────────────┴────────────╯
Example (Ascii) ¶
t := auto_table.New("ascii-simple") t = populateTable(t) err := t.RenderTo(os.Stdout) if err != nil { fmt.Fprintf(os.Stderr, "table rendering error: %s\n", err) } fmt.Printf("Table has %d rows and %d columns\n", t.NRows(), t.NColumns())
Output: +--------------+------------+ | Fruit | Price $/lb | +--------------+------------+ | bananas | 0.56 | | apples | 1.31 | | strawberries | 2.22 | | pears | 1.90 | +--------------+------------+ Table has 4 rows and 2 columns
Example (Html) ¶
// We can use `"html"` as a style when creating the table too, but the // various styles let you embed other tables, and the auto package has // package-level functions to take any table and render in any style. t := auto_table.New("utf8-light-curved") t = populateTable(t) _ = t.RenderTo(os.Stdout) _ = auto_table.RenderTo(t, os.Stdout, "html")
Output: ╭──────────────┬────────────╮ │ Fruit │ Price $/lb │ ├──────────────┼────────────┤ │ bananas │ 0.56 │ │ apples │ 1.31 │ │ strawberries │ 2.22 │ │ pears │ 1.90 │ ╰──────────────┴────────────╯ <table> <thead> <tr><th>Fruit</th><th>Price $/lb</th></tr> </thead> <tbody> <tr><td>bananas</td><td>0.56</td></tr> <tr><td>apples</td><td>1.31</td></tr> <tr><td>strawberries</td><td>2.22</td></tr> <tr><td>pears</td><td>1.90</td></tr> </tbody> </table>
Example (Inspection) ¶
t := auto_table.New("") // empty string is style-less, so a rendering error later t = populateTable(t) err := t.RenderTo(os.Stdout) if err != nil { // nb, we actually expect output, so using os.Stdout here this time fmt.Fprintf(os.Stdout, "table rendering error: %s\n", err) } fmt.Printf("Table has %d rows and %d columns\n", t.NRows(), t.NColumns()) row := 3 column := 2 cell, err := t.CellAt(tabular.CellLocation{Row: row, Column: column}) loc := cell.Location() fmt.Printf("At row %d column %d the value is %q, self-described as row %d column %d\n", row, column, cell, loc.Row, loc.Column) rowKiwi := tabular.NewRowWithCapacity(2) rowKiwi.Add(tabular.NewCell("kiwis")) rowKiwi.Add(tabular.NewCell("2.50")) t.AddRow(rowKiwi) kiwiLocation := rowKiwi.Location() fmt.Printf("Kiwi row is row %d\n", kiwiLocation.Row) fmt.Printf("\nManual style at render time:\n") if err = auto_table.RenderTo(t, os.Stdout, "utf8-double"); err != nil { fmt.Fprintf(os.Stdout, "second render, table rendering error: %s\n", err) } // nb: this highlights a limitation of tabular: we can only use characters // which exist in Unicode, and the box-drawing double-lines are missing // double left/up/right and single down, so you'll notice a display glitch // below.
Output: table rendering error: table has no decoration at all, can't render Table has 4 rows and 2 columns At row 3 column 2 the value is "2.22", self-described as row 3 column 2 Kiwi row is row 5 Manual style at render time: ╔══════════════╦════════════╗ ║ Fruit ║ Price $/lb ║ ╠══════════════╪════════════╣ ║ bananas │ 0.56 ║ ║ apples │ 1.31 ║ ║ strawberries │ 2.22 ║ ║ pears │ 1.90 ║ ║ kiwis │ 2.50 ║ ╚══════════════╧════════════╝
Example (Rows) ¶
// can use named constants so that typos can be caught at compile time, instead // of strings, _if_ you're willing to import the extra package; we don't force // callers to do this, letting them make the call on trade-offs. t := auto_table.New(decoration.D_UTF8_LIGHT_CURVED) t = populateTable(t) extra := t.NewRowSizedFor() // could call tabular.NewRow(), or tabular.NewRowWithCapacity(2), but the // table-method supplies the current table column count to the latter, // letting us just get the right size. Can always add cells to a row, and // keep adding, which we'll show here, but this is getting the "right" size // with pre-allocation. c1 := tabular.NewCell("tomato") c2 := tabular.NewCell("2.24") extra = extra.Add(c1).Add(c2).Add(tabular.NewCell("but is it a fruit?")) t.AddSeparator() t.AddRow(extra) fmt.Printf("Now have %d columns\n", t.NColumns()) if err := t.RenderTo(os.Stdout); err != nil { fmt.Fprintf(os.Stderr, "table rendering error: %s\n", err) }
Output: Now have 3 columns ╭──────────────┬────────────┬────────────────────╮ │ Fruit │ Price $/lb │ │ ├──────────────┼────────────┼────────────────────┤ │ bananas │ 0.56 │ │ │ apples │ 1.31 │ │ │ strawberries │ 2.22 │ │ │ pears │ 1.90 │ │ ├──────────────┼────────────┼────────────────────┤ │ tomato │ 2.24 │ but is it a fruit? │ ╰──────────────┴────────────┴────────────────────╯
Example (Simple) ¶
t := auto_table.New("utf8-heavy") t.AddHeaders("Fruit", "Price $/lb") t.AddRowItems("bananas", "0.56") t.AddRowItems("apples", "1.31") t.AddRowItems("strawberries", "2.22") t.AddRowItems("pears", "1.90") if errs := t.Errors(); errs != nil { for _, err := range errs { fmt.Fprintf(os.Stderr, "table error: %s\n", err) } } // This would get you the table as a string, instead of using an io.Writer: // tString, err := t.Render() err := t.RenderTo(os.Stdout) if err != nil { fmt.Fprintf(os.Stderr, "table rendering error: %s\n", err) }
Output: ┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓ ┃ Fruit ┃ Price $/lb ┃ ┣━━━━━━━━━━━━━━╇━━━━━━━━━━━━┫ ┃ bananas │ 0.56 ┃ ┃ apples │ 1.31 ┃ ┃ strawberries │ 2.22 ┃ ┃ pears │ 1.90 ┃ ┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━┛
Click to show internal directories.
Click to hide internal directories.