Rendering Tables
Render structured tables in Lip Gloss for organized terminal output.
Render Stylish Tables
Lip Gloss’s table rendering sub-package lets you create organized, styled tables for your terminal apps, perfect for displaying structured data in CLIs or Bubble Tea TUIs.
Table Rendering Options
Lip Gloss provides tools to create and style tables:
Define and render a table with data rows:
import "github.com/charmbracelet/lipgloss/table"
rows := [][]string{
{"Chinese", "您好", "你好"},
{"Japanese", "こんにちは", "やあ"},
{"Arabic", "أهلين", "أهلا"},
{"Russian", "Здравствуйте", "Привет"},
{"Spanish", "Hola", "¿Qué tal?"},
}
t := table.New().
Border(lipgloss.NormalBorder()).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...).
Row("English", "You look absolutely fabulous.", "How's it going?")
fmt.Println(t)Apply styles to headers and cells for a customized look:
var (
purple = lipgloss.Color("99")
gray = lipgloss.Color("245")
lightGray = lipgloss.Color("241")
headerStyle = lipgloss.NewStyle().Foreground(purple).Bold(true).Align(lipgloss.Center)
cellStyle = lipgloss.NewStyle().Padding(0, 1).Width(14)
oddRowStyle = cellStyle.Foreground(gray)
evenRowStyle = cellStyle.Foreground(lightGray)
)
t := table.New().
Border(lipgloss.NormalBorder()).
BorderStyle(lipgloss.NewStyle().Foreground(purple)).
StyleFunc(func(row, col int) lipgloss.Style {
switch {
case row == table.HeaderRow:
return headerStyle
case row%2 == 0:
return evenRowStyle
default:
return oddRowStyle
}
}).
Headers("LANGUAGE", "FORMAL", "INFORMAL").
Rows(rows...)Use predefined Markdown or ASCII borders:
// Markdown Table
table.New().Border(lipgloss.MarkdownBorder()).BorderTop(false).BorderBottom(false)| LANGUAGE | FORMAL | INFORMAL |
|----------|--------------|-----------|
| Chinese | Nǐn hǎo | Nǐ hǎo |
| French | Bonjour | Salut |
| Russian | Zdravstvuyte | Privet |
| Spanish | Hola | ¿Qué tal? |// ASCII Table
table.New().Border(lipgloss.ASCIIBorder())+----------+--------------+-----------+
| LANGUAGE | FORMAL | INFORMAL |
+----------+--------------+-----------+
| Chinese | Nǐn hǎo | Nǐ hǎo |
| French | Bonjour | Salut |
| Russian | Zdravstvuyte | Privet |
| Spanish | Hola | ¿Qué tal? |
+----------+--------------+-----------+How It Works
Create and render tables in your terminal apps:
Import the Table Package
Use github.com/charmbracelet/lipgloss/table to access table rendering tools.
Define Table Data
Create rows as a slice of string slices and set headers:
t := table.New().Headers("LANGUAGE", "FORMAL", "INFORMAL").Rows(rows...)Style and Render
Apply styles and borders, then render with fmt.Println(t).
Declare Rows before Offset to ensure proper rendering, as Offset has no effect otherwise.
How is this guide?