Documentation ¶
Overview ¶
Package mdtable generates markdown tables from string slices with formatting options for alignment and column width.
Example ¶
package main import ( "fmt" "github.com/willabides/mdtable" ) func main() { data := [][]string{ // first row is the header {"Name", "Favorite Animal", "Lucky Number"}, // the rest is data {"Dave", "Elephant", "7"}, {"Iris", "Gorilla", "8"}, {"Ava Gayle", "Sloth", "972.5"}, } b := mdtable.Generate(data) fmt.Println(string(b)) }
Output: | Name | Favorite Animal | Lucky Number | |-----------|-----------------|--------------| | Dave | Elephant | 7 | | Iris | Gorilla | 8 | | Ava Gayle | Sloth | 972.5 |
Example (Options) ¶
package main import ( "fmt" "github.com/willabides/mdtable" ) func main() { // This adds options one at a time and shows what the output of // mdtable.Generate would be after each option is added. data := [][]string{ {"Name", "Favorite Animal", "Lucky Number"}, {"Dave", "Elephant", "7"}, {"Iris", "Gorilla", "8"}, {"Ava Gayle", "Sloth", "972.5"}, } var options []mdtable.Option // Right align the whole table options = append(options, mdtable.Alignment(mdtable.AlignRight), ) /* | Name | Favorite Animal | Lucky Number | |----------:|----------------:|-------------:| | Dave | Elephant | 7 | | Iris | Gorilla | 8 | | Ava Gayle | Sloth | 972.5 | */ // Left align header text options = append(options, mdtable.HeaderAlignment(mdtable.AlignLeft), ) /* | Name | Favorite Animal | Lucky Number | |----------:|----------------:|-------------:| | Dave | Elephant | 7 | | Iris | Gorilla | 8 | | Ava Gayle | Sloth | 972.5 | */ // Set Favorite Animal's (offset 1) minimum width to 20 and center its text options = append(options, mdtable.ColumnMinWidth(1, 20), mdtable.ColumnTextAlignment(1, mdtable.AlignCenter), ) /* | Name | Favorite Animal | Lucky Number | |----------:|---------------------:|-------------:| | Dave | Elephant | 7 | | Iris | Gorilla | 8 | | Ava Gayle | Sloth | 972.5 | */ options = append(options, mdtable.ColumnAlignment(0, mdtable.AlignLeft), // Left align Name ) b := mdtable.Generate(data, options...) fmt.Println(string(b)) }
Output: | Name | Favorite Animal | Lucky Number | |:----------|---------------------:|-------------:| | Dave | Elephant | 7 | | Iris | Gorilla | 8 | | Ava Gayle | Sloth | 972.5 |
Index ¶
- func Generate(data [][]string, options ...Option) []byte
- type Align
- type Option
- func Alignment(val Align) Option
- func ColumnAlignment(column int, alignment Align) Option
- func ColumnHeaderAlignment(column int, alignment Align) Option
- func ColumnMinWidth(column, width int) Option
- func ColumnTextAlignment(column int, alignment Align) Option
- func HeaderAlignment(val Align) Option
- func TextAlignment(val Align) Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Generate ¶ added in v0.3.0
Generate generates a markdown table.
data -- is the table data. The top level slice contains rows and second level slices are cells. The first row is the header row. You need at least two rows (including the header row) to create a valid markdown table.
options -- are options for formatting the table. All options are in options.md.
There are three different types of alignment available, Alignment, TextAlignment and HeaderAlignment. Each of these can be set at either the table or column level.
Alignment -- controls how the markdown will appear when rendered in a browser.
TextAlignment -- controls the text alignment of fields. Default behavior is using the Alignment value of AlignLeft if that is unset.
HeaderAlignment -- is the same as TextAlignment, but for the header row only. Default behavior is using the TextAlignment value.
Types ¶
type Option ¶ added in v0.3.0
type Option func(*table)
Option is an option to control a table's formatting
func ColumnAlignment ¶ added in v0.3.0
ColumnAlignment sets the markdown alignment for a column
func ColumnHeaderAlignment ¶ added in v0.3.0
ColumnHeaderAlignment sets the text alignment for a column header
func ColumnMinWidth ¶ added in v0.3.0
ColumnMinWidth sets the minimum width for a column
func ColumnTextAlignment ¶ added in v0.3.0
ColumnTextAlignment sets the text alignment for a column
func HeaderAlignment ¶ added in v0.3.0
HeaderAlignment sets the text alignment for headers
func TextAlignment ¶ added in v0.3.0
TextAlignment sets the default text alignment for non-header cells