tabula

package module
v0.1.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: GPL-3.0 Imports: 5 Imported by: 0

README

tabula

A rudimentary terminal table rendering library for Go using charmbracelet/lipgloss.


This library has been implemented for github.com/lsg551/matricula-online and its specific requirements. Although there are much better and more mature alternatives, most were missing a crucial feature I needed: Edge-to-edge layout or rendering tables that take up the full width of the terminal and have some columns that grow to fill the remaining space. You can see an example of this here.

Therefore, you should probably NOT use this library and one of the great alternatives instead, such as:

Installation

go get github.com/lsg551/tabula

Usage

import (
    "github.com/lsg551/tabula"
)

func main() {
	table := tabula.New([]tabula.Column{
		tabula.NewColumn("Name"),
		tabula.NewColumn("Age"),
		tabula.NewColumn("City"),
	})

	table.AddRow("Alice", "30", "London")
	table.AddRow("Bob", "25", "Berlin")

	println(table.String())
}

Calling table.String() or table.Render() will render the table as a string, which can be printed to the terminal. The above example will look like this:

==========================
  Name     Age    City    
==========================
  Alice    30     London  
  Bob      25     Berlin  

More examples can be found in the examples directory.

Documentation

Docs are available on pkg.go.dev.

License

This project is licensed under GPL-3.0 License.

Documentation

Overview

package tabula is a rudimentary library for rendering tables in the terminal.

It uses lipgloss for styling and can be dynamically rendered with bubbletea.

Index

Constants

This section is empty.

Variables

View Source
var (

	// HeaderCharacter is the string used to render the header's horizontal
	// border. Override to customize the header border style.
	HeaderCharacter string = "="
	// SummaryRowCharacter is the string used to render the summary row's
	// horizontal border. Override to customize the summary row border style.
	SummaryRowCharacter string = "-"
	// RowBorderCharacter is the string used to render the vertical border between
	// columns. Override to customize the column border style.
	RowBorderCharacter string = "│"

	// DefaultCellPadding is the default horizontal padding applied to the left and
	// right of each cell. Can be overridden per column via [WithPadding]. Override
	// this variable to change the default padding for all columns that don't
	// explicitly set a padding.
	DefaultCellPadding uint = 2
	// DefaultCellAlignment is the default horizontal alignment of cell content
	// within a column. This can be overridden for each column individually by
	// setting the [Table.WithAlignment] property. Override this variable to change
	// the default alignment for all columns that don't explicitly set an
	// alignment.
	DefaultCellAlignment = lipgloss.Left
)

Functions

This section is empty.

Types

type Column

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

Column defines the properties of a single column in the table, including its header text. Create a new instance with NewColumn and configure it with the available [ColumnOption]s.

func NewColumn

func NewColumn(text string, opts ...ColumnOption) Column

NewColumn creates a new Column with the given text and options. The text may include any sytling (e.g., with lipgloss) that should be applied to the column header.

type ColumnOption

type ColumnOption func(*Column)

func WithAlignment

func WithAlignment(alignment lipgloss.Position) ColumnOption

WithAlignment sets the alignment of the column's content within each cell to the given lipgloss.Position. If not set, defaults to DefaultCellAlignment.

func WithLeftBorder

func WithLeftBorder() ColumnOption

WithLeftBorder adds the left border character RowBorderCharacter to the column. WithRightBorder can safely be called on the left-adjacent column without causing a double border.

func WithMaxWidth

func WithMaxWidth() ColumnOption

WithMaxWidth sets the column to grow in width until the table width is filled. This is similiar to CSS's `flex-grow: 1` property. This is especially useful for spacing where a limited subset of columns should stick to the right of the table. If multiple columns have this property enabled, they will share this remaining table width equally.

func WithPadding

func WithPadding(left, right uint) ColumnOption

WithPadding sets the left and right padding for the column's cells. If not set, defaults to DefaultCellPadding on both sides.

func WithRightBorder

func WithRightBorder() ColumnOption

WithRightBorder adds the right border character RowBorderCharacter to the column. WithLeftBorder can safely be called on the right-adjacent column without causing a double border.

type Row

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

Row carries the content of a single row in the table. Create a new instance with NewRow.

func NewRow

func NewRow(cells ...string) Row

NewRow creates a new Row with the given cells. The number of cells in the row should match the number of columns in the table.

type Table

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

Table is a structure that defines [Column]s and [Row]s, and is responsible for rendering them as a cohesive table. Create a new instance with New, add rows after instantiation with Table.AddRow, and then render the table with Table.Render or Table.String.

func New

func New(cols []Column, opts ...TableOption) *Table

New creates a new Table with the given [Column]s.

func (*Table) AddRow

func (t *Table) AddRow(cells ...string) error

AddRow adds a new Row to the Table. If the number of cells in the row does not match the number of columns in the table, an error is returned.

func (*Table) AddSummary

func (t *Table) AddSummary(row ...string)

AddSummary sets a summary Row for the Table, which is rendered at the end of the table.

func (*Table) Render

func (t *Table) Render() string

Render renders the table as a string.

func (*Table) String

func (t *Table) String() string

String renders the table as a string. Same as Table.Render.

type TableOption

type TableOption func(*Table)

func WithFixedWidth

func WithFixedWidth(width uint) TableOption

WithFixedWidth sets a fixed width for the table, which is used for rendering. By default, the table uses the terminal width as its width.

Directories

Path Synopsis
examples
basic command
full-width command
with-lipgloss command

Jump to

Keyboard shortcuts

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