table

package module
v0.0.15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2025 License: MIT Imports: 6 Imported by: 0

README ΒΆ

RapidFort Table

Go Reference

A flexible Go library for creating beautiful ASCII tables with box-drawing characters and advanced formatting options.

Features

  • πŸ“š Smart text wrapping and word splitting
  • πŸ”„ Column alignment control (left, right, center)
  • πŸ“ Description support with optional titles
  • ✨ Header highlighting
  • 🎨 Dim border styling for subtle tables
  • πŸ“Š Table grouping for consistent column widths
  • πŸ“ Dynamic width adjustment and terminal detection
  • πŸ” Support for borderless mode
  • πŸ’‘ Fill width option for full terminal utilization

Installation

go get github.com/rapidfort/table

Quick Start

package main

import (
    "fmt"
    "github.com/rapidfort/table"
)

func main() {
    // Create a table
    tbl := table.RapidFortTable([]string{"Name", "Age", "City"})
    
    // Add rows
    tbl.AddRow([]string{"Alice", "30", "New York"})
    tbl.AddRow([]string{"Bob", "25", "San Francisco"})
    
    // Add description to a row
    tbl.AddDescription(0, "Special customer discount applied")
    
    // Render and print
    fmt.Println(tbl.Render())
}

This produces:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Name  β”‚ Age β”‚ City          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Alice β”‚ 30  β”‚ New York      β”‚
β”‚       β”œβ”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚       β”‚   β€’ Special         β”‚
β”‚       β”‚     customer        β”‚
β”‚       β”‚     discount        β”‚
β”‚       β”‚     applied         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Bob   β”‚ 25  β”‚ San Francisco β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Advanced Usage

Header Styling
// Highlight specific headers
tbl.SetHighlightedHeaders([]int{0, 2}) // Highlights columns 0 and 2

// Always highlight all headers (bold style)
tbl.SetHeaderHighlighting(true)

// Add/remove highlighting dynamically
tbl.AddHighlightedHeader(1)
tbl.ClearHighlightedHeaders()
Border and Style Control
// Enable dim borders (light gray)
tbl.SetDimBorder(true)

// Remove borders completely
tbl.SetBorderless(true)

// Automatic terminal width detection (default)
tbl.SetConsoleWidth(80) // Manual override
Column Formatting
// Set column alignment
tbl.SetAlignment(0, "left")   // Column 0: left-aligned
tbl.SetAlignment(1, "right")  // Column 1: right-aligned
tbl.SetAlignment(2, "center") // Column 2: center-aligned

// Set maximum column widths
tbl.SetMaxWidth(1, 20)        // Column 1 max width: 20 chars

// Enable width filling
tbl.SetFillWidth(true)        // Expand to fill terminal width
Table Groups

When multiple tables need consistent column widths:

group := table.NewGroup()

// Create tables
table1 := table.RapidFortTable(headers)
table2 := table.RapidFortTable(headers)

// Add to group
group.Add(table1)
group.Add(table2)

// Sync column widths across all tables
group.SyncColumnWidths()

// Render tables with consistent widths
fmt.Println(table1.Render())
fmt.Println(table2.Render())
Rich Descriptions
// Add description with title
tbl.AddDescriptionWithTitle(rowIndex, "Advisory", "High demand item, consider restocking")

// Add simple description
tbl.AddDescription(rowIndex, "This item is currently on backorder")

// Multi-line descriptions are automatically wrapped
tbl.AddDescription(rowIndex, "This is a long description that will wrap across multiple lines based on the available space in the table")

Examples

Complete Feature Showcase
// Create table with all features
tbl := table.RapidFortTable([]string{"ID", "Product", "Stock", "Status"})

// Configure styling
tbl.SetDimBorder(true)
tbl.SetHighlightedHeaders([]int{1, 3})
tbl.SetAlignment(2, "right")
tbl.SetMaxWidth(1, 15)

// Add data
tbl.AddRow([]string{"001", "Wireless Mouse", "125", "Active"})
tbl.AddRow([]string{"002", "USB-C Hub Pro", "25", "Low Stock"})
tbl.AddRow([]string{"003", "Mechanical Keyboard", "50", "Active"})

// Add descriptions
tbl.AddDescriptionWithTitle(1, "Alert", "Reorder soon to avoid stockout")
tbl.AddDescription(2, "Popular item, high demand expected")

fmt.Println(tbl.Render())
Borderless Table
tbl := table.RapidFortTable([]string{"Name", "Value"})
tbl.SetBorderless(true)
tbl.AddRow([]string{"Setting 1", "Enabled"})
tbl.AddRow([]string{"Setting 2", "Disabled"})
fmt.Println(tbl.Render())

Documentation

For detailed API documentation, visit pkg.go.dev.

Requirements

  • Go 1.16 or higher
  • Dependencies: golang.org/x/term

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

MIT License

Copyright (c) 2025 RapidFort

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Acknowledgments

Built by the RapidFort team.

Documentation ΒΆ

Overview ΒΆ

Package table provides a utility for rendering ASCII tables with box-drawing characters

Index ΒΆ

Constants ΒΆ

View Source
const (
	// ANSI codes
	//DimStyleStart = "\x1b[38;5;242m"
	// faint + darker gray for very subdued borders
	DimStyleStart = "\x1b[2m\x1b[38;5;240m"

	DimStyleEnd    = "\x1b[0m"
	BoldStyleStart = "\x1b[1m" // Bold style for highlighting
	BoldStyleEnd   = "\x1b[0m"

	// Box drawing characters
	TopLeft     = "β”Œ"
	TopRight    = "┐"
	BottomLeft  = "β””"
	BottomRight = "β”˜"
	HLine       = "─"
	VLine       = "β”‚"
	LeftT       = "β”œ"
	RightT      = "─"
	TopT        = "┬"
	BottomT     = "β”΄"
	Cross       = "β”Ό"
)

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type Table ΒΆ

type Table struct {
	Headers           []string
	Rows              [][]string
	Descriptions      map[int][]string // row index -> description
	DescriptionTitles map[int][]string // row index -> title (optional)
	// contains filtered or unexported fields
}

Table represents a table with borders and alignment control

func NewTable ΒΆ added in v0.0.13

func NewTable(headers []string) *Table

func RapidFortTable ΒΆ

func RapidFortTable(headers []string) *Table

func (*Table) AddDescription ΒΆ

func (t *Table) AddDescription(rowIndex int, description string)

AddDescription adds a description for a specific row

func (*Table) AddDescriptionWithTitle ΒΆ

func (t *Table) AddDescriptionWithTitle(rowIndex int, title string, description string)

AddDescriptionWithTitle adds a description with a title for a specific row

func (*Table) AddHighlightedHeader ΒΆ

func (t *Table) AddHighlightedHeader(index int)

AddHighlightedHeader adds a header to the highlighted list

func (*Table) AddRow ΒΆ

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

AddRow adds a new row to the table

func (*Table) ClearHighlightedHeaders ΒΆ

func (t *Table) ClearHighlightedHeaders()

ClearHighlightedHeaders removes all header highlights

func (*Table) EnableRowCount ΒΆ added in v0.0.13

func (t *Table) EnableRowCount(enabled bool) *Table

func (*Table) Render ΒΆ

func (t *Table) Render() string

func (*Table) SetAlignment ΒΆ

func (t *Table) SetAlignment(columnIndex int, alignment string)

SetAlignment sets the alignment for a specific column

func (*Table) SetBorderless ΒΆ

func (t *Table) SetBorderless(on bool)

SetBorderless enables/disables drawing of any box‐drawing characters.

func (*Table) SetConsoleWidth ΒΆ

func (t *Table) SetConsoleWidth(width int)

SetConsoleWidth sets the maximum width for the table

func (*Table) SetDimBorder ΒΆ

func (t *Table) SetDimBorder(enabled bool)

func (*Table) SetFillWidth ΒΆ

func (t *Table) SetFillWidth(enabled bool)

SetFillWidth sets whether the table should expand to fill the console width

func (*Table) SetHeaderHighlighting ΒΆ

func (t *Table) SetHeaderHighlighting(enabled bool)

SetHeaderHighlighting enables/disables header highlighting

func (*Table) SetHighlightedHeaders ΒΆ

func (t *Table) SetHighlightedHeaders(indices []int)

SetHighlightedHeaders sets which headers should be highlighted

func (*Table) SetMaxWidth ΒΆ

func (t *Table) SetMaxWidth(columnIndex int, maxWidth int)

SetMaxWidth sets the maximum width for a specific column

type TableGroup ΒΆ

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

TableGroup manages multiple tables with consistent column widths

func NewGroup ΒΆ

func NewGroup() *TableGroup

NewGroup creates a new TableGroup for managing multiple tables

func (*TableGroup) Add ΒΆ

func (g *TableGroup) Add(table *Table)

Add adds a table to the group

func (*TableGroup) GetTables ΒΆ

func (g *TableGroup) GetTables() []*Table

GetTables returns the tables in the group (public method for testing)

func (*TableGroup) SyncColumnWidths ΒΆ

func (g *TableGroup) SyncColumnWidths()

SyncColumnWidths ensures all tables in the group have consistent column widths

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL