Documentation
¶
Overview ¶
Package gotabulate provides table formatting and rendering capabilities. It supports multiple output formats including text grids, HTML, CSV, and more.
Index ¶
- Variables
- func ExcelOut(csvIn, sheetname, excelFile string) error
- func PrintCol(line, columns string, space bool) []string
- func ShowFormats()
- type CSVOut
- type HTMLConfig
- type Line
- type Row
- type TableFormat
- type Tabulate
- func (t *Tabulate) Render(format ...interface{}) string
- func (t *Tabulate) SetAlign(align string)
- func (t *Tabulate) SetEmptyString(empty string)
- func (t *Tabulate) SetFloatFormat(format byte) *Tabulate
- func (t *Tabulate) SetHTMLBootstrap(version string)
- func (t *Tabulate) SetHTMLConfig(config HTMLConfig)
- func (t *Tabulate) SetHTMLCustomCSS(css string)
- func (t *Tabulate) SetHTMLMinimal()
- func (t *Tabulate) SetHTMLOfflineMode()
- func (t *Tabulate) SetHTMLStyle(tableClass string)
- func (t *Tabulate) SetHTMLTitle(title string)
- func (t *Tabulate) SetHTMLUseCDN(useCDN bool)
- func (t *Tabulate) SetHeaders(headers []string) *Tabulate
- func (t *Tabulate) SetHideLines(hide []string)
- func (t *Tabulate) SetIndex(index bool)
- func (t *Tabulate) SetMaxCellSize(max int)
- func (t *Tabulate) SetNoHeader()
- func (t *Tabulate) SetRemEmptyLines(remEmptyLines bool)
- func (t *Tabulate) SetWrapStrings(wrap bool)
- type TabulateRow
Constants ¶
This section is empty.
Variables ¶
var TableFormats = map[string]TableFormat{ "simple": { LineTop: Line{"", "-", " ", ""}, LineBelowHeader: Line{"", "-", " ", ""}, LineBottom: Line{"", "-", " ", ""}, HeaderRow: Row{"", " ", ""}, DataRow: Row{"", " ", ""}, Padding: 1, }, "plain": { HeaderRow: Row{"", " ", ""}, DataRow: Row{"", " ", ""}, Padding: 1, }, "tab": { HeaderRow: Row{"", "\t", ""}, DataRow: Row{"", "\t", ""}, Padding: 0, }, "text": { HeaderRow: Row{"", " ", ""}, DataRow: Row{"", " ", ""}, Padding: 0, }, "csv": { HeaderRow: Row{"", ",", ""}, DataRow: Row{"", ",", ""}, Padding: 0, }, "mysql": { LineTop: Line{"+", "-", "+", "+"}, LineBelowHeader: Line{"+", "=", "+", "+"}, LineBottom: Line{"+", "-", "+", "+"}, HeaderRow: Row{"|", "|", "|"}, DataRow: Row{"|", "|", "|"}, Padding: 1, }, "mysqlg": { LineTop: Line{"╒", "═", "╤", "╕"}, LineBelowHeader: Line{"╞", "═", "╪", "╡"}, LineBottom: Line{"└", "─", "┴", "┘"}, HeaderRow: Row{"│", "│", "│"}, DataRow: Row{"│", "│", "│"}, Padding: 1, }, "grid": { LineTop: Line{"╒", "═", "╤", "╕"}, LineBelowHeader: Line{"╞", "═", "╪", "╡"}, LineBetweenRows: Line{"│", "─", "┼", "│"}, LineBottom: Line{"└", "─", "┴", "┘"}, HeaderRow: Row{"│", "│", "│"}, DataRow: Row{"│", "│", "│"}, Padding: 1, }, "gridt": { LineTop: Line{"+", "-", "+", "+"}, LineBelowHeader: Line{"+", "=", "+", "+"}, LineBetweenRows: Line{"+", "-", "+", "+"}, LineBottom: Line{"+", "-", "+", "+"}, HeaderRow: Row{"|", "|", "|"}, DataRow: Row{"|", "|", "|"}, Padding: 1, }, "bingo": { LineTop: Line{"╒", "═", "╤", "╕"}, LineBelowHeader: Line{"╞", "═", "╪", "╡"}, LineBetweenRows: Line{"│", "─", "┼", "│"}, LineBottom: Line{"└", "─", "┴", "┘"}, HeaderRow: Row{"│", "│", "│"}, DataRow: Row{"│", "│", "│"}, Padding: 1, }, "markdown": { LineBelowHeader: Line{"|", "-", "|", "|"}, HeaderRow: Row{"| ", " | ", " |"}, DataRow: Row{"| ", " | ", " |"}, Padding: 0, }, "rst": { LineTop: Line{"", "=", " ", ""}, LineBelowHeader: Line{"", "=", " ", ""}, LineBottom: Line{"", "=", " ", ""}, HeaderRow: Row{"", " ", ""}, DataRow: Row{"", " ", ""}, Padding: 1, }, "simple-nohead": { LineTop: Line{"", "-", " ", ""}, LineBottom: Line{"", "-", " ", ""}, HeaderRow: Row{"", " ", ""}, DataRow: Row{"", " ", ""}, Padding: 1, }, "plain-nohead": { HeaderRow: Row{"", " ", ""}, DataRow: Row{"", " ", ""}, Padding: 0, }, "tab-nohead": { HeaderRow: Row{"", "\t", ""}, DataRow: Row{"", "\t", ""}, Padding: 0, }, "csv-nohead": { HeaderRow: Row{"", ",", ""}, DataRow: Row{"", ",", ""}, Padding: 0, }, "text-nohead": { HeaderRow: Row{"", " ", ""}, DataRow: Row{"", " ", ""}, Padding: 0, }, "mysql-nohead": { LineTop: Line{"+", "-", "+", "+"}, LineBottom: Line{"+", "-", "+", "+"}, HeaderRow: Row{"|", "|", "|"}, DataRow: Row{"|", "|", "|"}, Padding: 1, }, "mysqlg-nohead": { LineTop: Line{"╒", "─", "╤", "╕"}, LineBottom: Line{"└", "─", "┴", "┘"}, HeaderRow: Row{"│", "│", "│"}, DataRow: Row{"│", "│", "│"}, Padding: 1, }, "grid-nohead": { LineTop: Line{"╒", "─", "╤", "╕"}, LineBetweenRows: Line{"│", "─", "┼", "│"}, LineBottom: Line{"└", "─", "┴", "┘"}, HeaderRow: Row{"│", "│", "│"}, DataRow: Row{"│", "│", "│"}, Padding: 1, }, "gridt-nohead": { LineTop: Line{"+", "-", "+", "+"}, LineBetweenRows: Line{"+", "-", "+", "+"}, LineBottom: Line{"+", "-", "+", "+"}, HeaderRow: Row{"|", "|", "|"}, DataRow: Row{"|", "|", "|"}, Padding: 1, }, "markdown-nohead": { HeaderRow: Row{"| ", " | ", " |"}, DataRow: Row{"| ", " | ", " |"}, Padding: 0, }, "rst-nohead": { LineTop: Line{"", "=", " ", ""}, LineBottom: Line{"", "=", " ", ""}, HeaderRow: Row{"", " ", ""}, DataRow: Row{"", " ", ""}, Padding: 1, }, }
TableFormats contains all available table format configurations. Users can define custom formats by adding entries to this map and calling Render.
Formats:
- simple: Minimal ASCII table with dashes
- plain: No borders, space-separated columns
- tab: Tab-separated values (TSV)
- text: Space-separated text (minimal padding)
- csv: Comma-separated values
- mysql: ASCII art grid styled like MySQL output
- mysqlg: Unicode grid styled like MySQL with box drawing characters
- grid: Full Unicode box drawing with lines between rows
- gridt: Text-based grid with ASCII characters
- markdown: Markdown table format
- rst: reStructuredText table format
- json: JSON array format
- plus suffix "-nohead" variants that suppress headers
Functions ¶
func ExcelOut ¶ added in v1.1.3
Take CSV formatted text (csvIn String) and print it out as an XLS file.
func ShowFormats ¶ added in v1.1.3
func ShowFormats()
ShowFormats prints all available table formats to standard output.
Types ¶
type CSVOut ¶
type HTMLConfig ¶ added in v1.2.10
type HTMLConfig struct {
// TableClass is the CSS class(es) applied to the table element
TableClass string
// HeaderClass is the CSS class(es) applied to header cells
HeaderClass string
// RowClass is the CSS class(es) applied to data row <tr> elements
RowClass string
// CellClass is the CSS class(es) applied to data cells
CellClass string
// StripedRows applies alternating row colors when true
StripedRows bool
// Bordered adds borders to all cells when true
Bordered bool
// Condensed reduces padding for a more compact table
Condensed bool
// Hover highlights rows on hover
Hover bool
// ResponsiveWidth makes the table responsive to screen width
ResponsiveWidth bool
// CustomCSS contains custom CSS rules to include in the table
CustomCSS string
// BootstrapVersion specifies Bootstrap version (4, 5, or empty for no framework)
BootstrapVersion string
// UseBootstrapCDN determines whether to use CDN link (true) or embed CSS (false)
// When false, Bootstrap CSS is embedded in the HTML for offline use
UseBootstrapCDN bool
// Title is an optional title displayed above the table
Title string
}
HTMLConfig provides customizable options for HTML table rendering.
func DefaultHTMLConfig ¶ added in v1.2.10
func DefaultHTMLConfig() HTMLConfig
DefaultHTMLConfig returns a sensible default HTML configuration using Bootstrap 5.
By default, Bootstrap CSS is loaded from a CDN (Content Delivery Network) for smaller file sizes (~2KB). This assumes your users have internet access, which is reasonable for most modern web applications.
Configuration defaults:
- Framework: Bootstrap 5
- CSS Source: CDN links (jsdelivr)
- File Size: ~2KB (CSS loaded externally)
- Striped Rows: Enabled
- Hover Effects: Enabled
- Borders: Enabled
- Theme: Primary header color
If users encounter rendering issues due to lack of internet, you can enable offline mode using SetHTMLOfflineMode() or OfflineHTMLConfig() to embed the CSS directly.
Example with default CDN mode:
table := gotabulate.Create(data)
table.SetHeaders([]string{"Name", "Value"})
html := table.Render("html") // Small file, uses CDN
Example switching to offline mode:
table := gotabulate.Create(data)
table.SetHeaders([]string{"Name", "Value"})
table.SetHTMLOfflineMode() // If needed for offline use
html := table.Render("html") // Large file, self-contained
func MinimalHTMLConfig ¶ added in v1.2.10
func MinimalHTMLConfig() HTMLConfig
MinimalHTMLConfig returns a minimal HTML configuration with no framework dependencies. This is completely self-contained and works offline without any external resources. Useful if you don't want to depend on Bootstrap or CDN.
func OfflineHTMLConfig ¶ added in v1.2.10
func OfflineHTMLConfig() HTMLConfig
OfflineHTMLConfig returns an HTML configuration with fully embedded Bootstrap CSS.
This configuration embeds the complete Bootstrap 5 CSS directly in the HTML file, making it completely self-contained and usable without internet access. The resulting file is larger (~100KB) but requires no external resources.
Use this function or SetHTMLOfflineMode when generating HTML on systems that don't have internet access, such as backend servers, data processing systems, or air-gapped environments.
The HTML produced with this configuration can be:
- Emailed as an attachment and opened anywhere
- Saved to a file and opened later
- Served by internal web applications
- Archived for long-term storage
- Used in offline or disconnected environments
Example:
config := gotabulate.OfflineHTMLConfig()
config.Title = "Offline Report"
table.SetHTMLConfig(config)
html := table.Render("html")
type Line ¶
type Line struct {
// contains filtered or unexported fields
}
Line represents a horizontal line element in a table.
type Row ¶
type Row struct {
// contains filtered or unexported fields
}
Row represents a row element in a table.
type TableFormat ¶
type TableFormat struct {
LineTop Line
LineBelowHeader Line
LineBetweenRows Line
LineBottom Line
HeaderRow Row
DataRow Row
Padding int
HeaderHide bool
FitScreen bool
}
TableFormat defines the structure and styling of table output.
type Tabulate ¶
type Tabulate struct {
Data []*TabulateRow
Headers []string
FloatFormat byte
TableFormat TableFormat
Align string
EmptyVar string
HideLines []string
MaxSize int
WrapStrings bool
RemEmptyLines bool
NoHeader bool
Index bool
HTMLConfig HTMLConfig
}
Tabulate represents a data table with formatting options.
func Create ¶
func Create(data interface{}) *Tabulate
Create creates a new Tabulate object from various data types. Accepts:
- 2D String Array ([][]string)
- 2D Int Array ([][]int)
- 2D Int32 Array ([][]int32)
- 2D Int64 Array ([][]int64)
- 2D Bool Array ([][]bool)
- 2D Float64 Array ([][]float64)
- 2D Interface Array ([][]interface{})
- 1D String Array ([]string)
- 1D Interface Array ([]interface{})
- Map[string][]interface{}
- Map[string][]string
func (*Tabulate) Render ¶
Render formats the data table using the specified format. If no format is provided, uses the TableFormat set in the Tabulate struct. For HTML format, uses the HTMLConfig for customization.
func (*Tabulate) SetEmptyString ¶
Set how an empty cell will be represented
func (*Tabulate) SetFloatFormat ¶
Set Float Formatting will be used in strconv.FormatFloat(element, format, -1, 64)
func (*Tabulate) SetHTMLBootstrap ¶ added in v1.2.10
SetHTMLBootstrap sets the Bootstrap version (4, 5, or empty for none).
func (*Tabulate) SetHTMLConfig ¶ added in v1.2.10
func (t *Tabulate) SetHTMLConfig(config HTMLConfig)
SetHTMLConfig sets a custom HTML configuration for table rendering.
func (*Tabulate) SetHTMLCustomCSS ¶ added in v1.2.10
SetHTMLCustomCSS adds custom CSS to the HTML output.
func (*Tabulate) SetHTMLMinimal ¶ added in v1.2.10
func (t *Tabulate) SetHTMLMinimal()
SetHTMLMinimal uses minimal CSS styling without Bootstrap.
func (*Tabulate) SetHTMLOfflineMode ¶ added in v1.2.10
func (t *Tabulate) SetHTMLOfflineMode()
SetHTMLOfflineMode enables offline mode with fully embedded Bootstrap CSS.
By default, HTML generation uses CDN links for smaller file sizes (~2KB). Call SetHTMLOfflineMode to embed the complete Bootstrap 5 CSS directly in the HTML file for use on systems without internet access.
The resulting HTML file will be self-contained and larger (~100KB), but can be opened and viewed in any browser regardless of internet connectivity.
This is useful for:
- Generating reports on backend servers without internet access
- Creating portable HTML documents for email or file sharing
- Archiving HTML reports that must work indefinitely
- Serving tables in offline or air-gapped environments
Example:
table := gotabulate.Create(data)
table.SetHeaders([]string{"Name", "Value"})
table.SetHTMLOfflineMode()
html := table.Render("html")
func (*Tabulate) SetHTMLStyle ¶ added in v1.2.10
SetHTMLStyle is a convenience method to set the HTML table class for styling.
func (*Tabulate) SetHTMLTitle ¶ added in v1.2.10
SetHTMLTitle sets an optional title for the HTML table.
func (*Tabulate) SetHTMLUseCDN ¶ added in v1.2.10
SetHTMLUseCDN configures whether Bootstrap CSS is loaded from CDN or embedded.
By default (true), Bootstrap is loaded from a CDN for smaller file sizes (~2KB). When set to false, the complete Bootstrap 5 CSS is embedded in the HTML (~100KB), making the file completely self-contained and usable without internet.
Parameters:
- useCDN: true to use CDN (default, smaller files, requires internet)
- useCDN: false to embed CSS (larger files, works offline)
See SetHTMLOfflineMode for a convenience method to enable offline mode.
func (*Tabulate) SetHeaders ¶
Set Headers of the table If Headers count is less than the data row count, the headers will be padded to the right
func (*Tabulate) SetHideLines ¶
Set which lines to hide. Can be: top - Top line of the table, belowheader - Line below the header, bottom - Bottom line of the table
func (*Tabulate) SetMaxCellSize ¶
Sets the maximum size of cell If WrapStrings is set to true, then the string inside the cell will be split up into multiple cell
func (*Tabulate) SetNoHeader ¶
func (t *Tabulate) SetNoHeader()
func (*Tabulate) SetRemEmptyLines ¶
func (*Tabulate) SetWrapStrings ¶
type TabulateRow ¶
TabulateRow represents a normalized row in a table.