tablewriter

repository·master·Indexed 26 days ago

https://github.com/olekukonko/tablewriter

A Go library for generating rich, text-based tables in ASCII, Unicode, Markdown, HTML, and ANSI-colored formats. It includes a modular configuration system for controlling borders, alignment, and cell formatting, as well as the csv2table CLI tool for converting CSV data into formatted tables.

Tokens
9.2K
Snippets
19
Records
70
Agent score
89%

What's inside tablewriter

  1. Install TableWriter for Go

    master

    You can install TableWriter using go get.

    • For the latest stable version (includes generics and streaming APIs): github.com/olekukonko/tablewriter@v1.1.4
    • For legacy applications: github.com/olekukonko/tablewriter@v0.0.5

    Warning: Do not use version v1.0.0 as it contains missing functionality.

    # Latest Version
    go get github.com/olekukonko/tablewriter@v1.1.4
    
    # Legacy Version
    go get github.com/olekukonko/tablewriter@v0.0.5
  2. Render a table to a string

    master

    Since tablewriter.NewWriter accepts an io.Writer, you can use strings.Builder to capture the table output as a string.

    import (
    	"strings"
    	"fmt"
    	"github.com/olekukonko/tablewriter"
    )
    
    func main() {
    	tableString := &strings.Builder{}
    	table := tablewriter.NewWriter(tableString)
    
    	// ... fill table ...
    
    	table.Render()
    	fmt.Println(tableString.String())
    }
  3. Install tablewriter

    master

    For production use, install the stable version v0.0.5 using Go modules:

    go get github.com/olekukonko/tablewriter@v0.0.5

    To try the in-progress, unstable development version (targeting v0.2.0 with generics and streaming support), use:

    go get github.com/olekukonko/tablewriter@master
    go get github.com/olekukonko/tablewriter@v0.0.5
  4. Deprecated type aliases: Behavior and Settings

    master

    The following type aliases are deprecated and will be removed in a future version. Use the tw package types directly instead.

    • tablewriter.Behavior: Use tw.Behavior directly to configure settings like auto-hiding empty columns or trimming spaces.
    • tablewriter.Settings: Use tw.Settings directly to configure renderer settings like separators and line styles.

    Migration Examples:

    // Old (deprecated)
    var b tablewriter.Behavior = tablewriter.Behavior{AutoHide: tw.On}
    // New (recommended)
    var b tw.Behavior = tw.Behavior{AutoHide: tw.On}
    
    // Old (deprecated)
    var s tablewriter.Settings = tablewriter.Settings{Separator: "|"}
    // New (recommended)
    var s tw.Settings = tw.Settings{Separator: "|"}
  5. Migrate from deprecated WithBorders to WithRendition

    master

    The WithBorders function is deprecated and will be removed. To configure border settings for renderers that support tw.Renditioning, use WithRendition instead. Alternatively, you can update the renderer's tw.RenderConfig directly via its Config() method.

    Migration Example:

    // Old (deprecated)
    table.Options(WithBorders(tw.Border{Top: true, Bottom: true}))
    
    // New (recommended)
    table.Options(WithRendition(tw.Rendition{Borders: tw.Border{Top: true, Bottom: true}}))
    // Old (deprecated)
    table.Options(WithBorders(tw.Border{Top: true, Bottom: true}))
    // New (recommended)
    table.Options(WithRendition(tw.Rendition{Borders: tw.Border{Top: true, Bottom: true}}))
  6. Use the csv2table CLI to convert CSV to tables

    master

    The csv2table command-line tool reads CSV data from a file or STDIN and renders it as a formatted ASCII, Markdown, HTML, or SVG table.

    Usage Patterns:

    • File input: Provide a filename via the -f flag or as a positional argument.
    • Pipe input: Use the -p flag to read from STDIN (e.g., cat data.csv | csv2table -p).
    • Default behavior: If no file is provided and -p is not set, it defaults to reading from STDIN.
    Usage: csv2table [options] [file]
    
    Reads CSV data from a file or STDIN and renders it as a formatted table.
    
    If [file] is provided, it overrides the -f flag.
    If no [file] and no -f is provided, and -p is not set, STDIN is used.
  7. Migrate deprecated alignment methods for Header, Footer, and Rows

    master

    The WithAlignment methods on FooterFormattingBuilder, HeaderFormattingBuilder, and RowFormattingBuilder are deprecated.

    To configure alignments, use the Alignment method on the respective builder with WithGlobal or WithPerColumn from an AlignmentConfigBuilder. Alternatively, apply a complete tw.CellAlignment configuration using WithHeaderAlignmentConfig, WithFooterAlignmentConfig, or WithRowAlignmentConfig.

    Migration Examples:

    For Footer:

    // Old (deprecated)
    builder.Footer().Formatting().WithAlignment(tw.AlignRight)
    
    // New (recommended)
    builder.Footer().Alignment().WithGlobal(tw.AlignRight)
    // Or
    table.Options(WithFooterAlignmentConfig(tw.CellAlignment{Global: tw.AlignRight}))

    For Header:

    // Old (deprecated)
    builder.Header().Formatting().WithAlignment(tw.AlignCenter)
    
    // New (recommended)
    builder.Header().Alignment().WithGlobal(tw.AlignCenter)
    // Or
    table.Options(WithHeaderAlignmentConfig(tw.CellAlignment{Global: tw.AlignCenter}))

    For Rows:

    // Old (deprecated)
    builder.Row().Formatting().WithAlignment(tw.AlignLeft)
    
    // New (recommended)
    builder.Row().Alignment().WithGlobal(tw.AlignLeft)
    // Or
    table.Options(WithRowAlignmentConfig(tw.CellAlignment{Global: tw.AlignLeft}))
  8. Migrate from WithTableMax to WithMaxWidth

    master

    WithTableMax is deprecated. Use WithMaxWidth instead for a clearer name and consistent package naming. WithMaxWidth sets the maximum width of the entire table in characters.

    Migration Example:

    // Old (deprecated)
    tablewriter.NewTable(os.Stdout, tablewriter.WithTableMax(80))
    
    // New (recommended)
    tablewriter.NewTable(os.Stdout, tablewriter.WithMaxWidth(80))
    // Old (deprecated)
    tablewriter.NewTable(os.Stdout, tablewriter.WithTableMax(80))
    // New (recommended)
    tablewriter.NewTable(os.Stdout, tablewriter.WithMaxWidth(80))
  9. Migrate from deprecated WithRendererSettings to WithRendition

    master

    The WithRendererSettings function is deprecated. To update renderer settings (like separators and line styles) for renderers implementing tw.Renditioning, use WithRendition. You can also configure the renderer's tw.Settings directly via its tw.Renderer.Config method.

    Migration Example:

    // Old (deprecated)
    table.Options(WithRendererSettings(tw.Settings{Separator: "|"}))
    
    // New (recommended)
    table.Options(WithRendition(tw.Rendition{Settings: tw.Settings{Separator: "|"}}))
    // Old (deprecated)
    table.Options(WithRendererSettings(tw.Settings{Separator: "|"}))
    // New (recommended)
    table.Options(WithRendition(tw.Rendition{Settings: tw.Settings{Separator: "|"}}))
  10. Initialize a new Table with NewTable or NewWriter

    master

    To create a new table instance, use NewTable which allows for custom configuration via Option functions, or NewWriter for a table with default settings. Both require an io.Writer as the destination for the output.

    import (
    	"os"
    	"github.com/olekukonko/tablewriter"
    )
    
    table := tablewriter.NewTable(os.Stdout)
    // or
    table := tablewriter.NewWriter(os.Stdout)
  11. Quick Example: Create a basic ASCII table

    master

    To create a simple table, use tablewriter.NewWriter(io.Writer) to initialize the table, set the header with .Header(), add data rows with .Bulk(), and finally call .Render() to output the table to the provided writer.

    package main
    
    import (
    	"github.com/olekukonko/tablewriter"
    	"os"
    )
    
    func main() {
    	data := [][]string{
    		{"Package", "Version", "Status"},
    		{"tablewriter", "v0.0.5", "legacy"},
    		{"tablewriter", "v1.1.4", "latest"},
    	}
    
    	table := tablewriter.NewWriter(os.Stdout)
    	table.Header(data[0])
    	table.Bulk(data[1:])
    	table.Render()
    }