Documentation
¶
Index ¶
- Constants
- func CalculateColumnWidths(columns []Column, termWidth int, padding int) []int
- func CalculateColumnWidthsWithContent(columns []Column, termWidth int, padding int, contentWidths []int) []int
- func FormatCell(value string, col Column, width int) string
- func GetTerminalWidth() int
- func MaxScrollOffset(s string, maxWidth int) int
- func PadCenter(s string, width int) string
- func PadLeft(s string, width int) string
- func PadRight(s string, width int) string
- func ParseSortKeyNumber(key string) int
- func ScrollWindow(s string, maxWidth int, offset int) string
- func ShortenPath(p string, maxWidth int) string
- func SortableColumnIndex(columns []Column, n int) int
- func SortableColumnsHelp(columns []Column) []string
- func StringWidth(s string) int
- func TruncateFromStart(s string, maxWidth int) string
- func TruncateWithEllipsis(s string, maxWidth int) string
- type Alignment
- type Column
- type Row
- type SortConfig
- type SortDirection
- type SortState
- type Table
- func (t *Table) AddRow(row Row)
- func (t *Table) CalculateWidths() []int
- func (t *Table) ClearRows()
- func (t *Table) EnsureCursorVisible()
- func (t *Table) GetColumnWidth(index int) int
- func (t *Table) InvalidateWidths()
- func (t *Table) MoveSelection(delta int)
- func (t *Table) NeedsScrollIndicator() bool
- func (t *Table) Render() string
- func (t *Table) RenderHeader() string
- func (t *Table) RenderRows() string
- func (t *Table) RenderScrollIndicator(style lipgloss.Style) string
- func (t *Table) RenderSeparator() string
- func (t *Table) RenderWithScroll(scrollStyle *lipgloss.Style) string
- func (t *Table) RowCount() int
- func (t *Table) ScrollPercentage() int
- func (t *Table) SelectedRow() *Row
- func (t *Table) SetTerminalWidth(width int)
- func (t *Table) VisibleRows() []Row
- type TruncateMode
Constants ¶
const (
// DefaultTerminalWidth is used when terminal width cannot be determined
DefaultTerminalWidth = 80
)
Variables ¶
This section is empty.
Functions ¶
func CalculateColumnWidths ¶
CalculateColumnWidths computes actual column widths based on terminal width. Use CalculateColumnWidthsWithContent for content-aware sizing.
func CalculateColumnWidthsWithContent ¶
func CalculateColumnWidthsWithContent(columns []Column, termWidth int, padding int, contentWidths []int) []int
CalculateColumnWidthsWithContent computes column widths with optional content-awareness. The algorithm: 1. Allocate fixed-width columns first 2. Distribute remaining space to flexible columns by weight 3. Clamp flexible columns to min/max constraints 4. Cap flexible columns at actual content width (if contentWidths provided) 5. Give any remaining space to flexible columns that can use it
func FormatCell ¶
FormatCell formats a cell value according to column settings
func GetTerminalWidth ¶
func GetTerminalWidth() int
GetTerminalWidth returns the current terminal width. Falls back to DefaultTerminalWidth if width cannot be determined.
func MaxScrollOffset ¶ added in v0.0.27
MaxScrollOffset returns the maximum scroll offset (in display cells) for ScrollWindow. Returns 0 if the string fits within maxWidth.
func PadCenter ¶
PadCenter centers a string within the specified display width. If the string is wider than width, it is truncated.
func PadLeft ¶
PadLeft pads a string to the specified display width with spaces on the left. If the string is wider than width, it is truncated.
func PadRight ¶
PadRight pads a string to the specified display width with spaces on the right. If the string is wider than width, it is truncated.
func ParseSortKeyNumber ¶
ParseSortKeyNumber converts a key string ("1", "f1", etc.) to a 1-based number. Returns 0 if the key is not a sort key.
func ScrollWindow ¶ added in v0.0.27
ScrollWindow returns a sliding window view of a string for scroll animation. At offset 0, truncates from end (like TruncateWithEllipsis). At offset > 0, shows content starting from that display-cell offset with ellipsis indicators.
func ShortenPath ¶
ShortenPath shortens a file path to fit within maxWidth display cells. It prioritizes keeping the filename visible and shortens directory components.
func SortableColumnIndex ¶
SortableColumnIndex returns the actual column index for the n-th sortable column (1-based). Returns -1 if n is out of range.
func SortableColumnsHelp ¶
SortableColumnsHelp returns help text lines for sortable columns.
func StringWidth ¶
StringWidth returns the display width of a string in terminal cells. Accounts for wide characters (CJK, emojis, etc.) AND ANSI escape codes.
func TruncateFromStart ¶
TruncateFromStart truncates a string from the beginning, keeping the end. Adds "…" prefix if truncated. Useful for paths where the end is more relevant.
func TruncateWithEllipsis ¶
TruncateWithEllipsis truncates a string to fit within maxWidth display cells, adding "…" if truncated. Handles wide characters correctly.
Types ¶
type Column ¶
type Column struct {
Header string // Column header text
Width int // Fixed width (0 = flexible)
MinWidth int // Minimum width for flexible columns
MaxWidth int // Maximum width (0 = unlimited)
Weight float64 // Weight for distributing remaining space (default 1.0)
Align Alignment // Left, Right, Center (default Left)
Truncate bool // Truncate with ellipsis if content too long
TruncateMode TruncateMode // How to truncate: TruncateEnd (default) or TruncateStart
SortKey string // When non-empty, column is sortable. Keys 1-9/F1-F9 map to sortable columns in order.
}
Column defines a table column configuration
func (*Column) EffectiveWeight ¶
EffectiveWeight returns the weight for flexible width distribution. Defaults to 1.0 if not set.
type Row ¶
type Row struct {
Cells []string // Cell contents (one per column)
Style lipgloss.Style // Optional row-level style override
}
Row represents a table row
type SortConfig ¶
type SortConfig struct {
Column int // Column index to sort by (-1 = none)
Direction SortDirection // Sort direction
}
SortConfig specifies current sort state
type SortDirection ¶
type SortDirection int
SortDirection for column sorting
const ( SortNone SortDirection = iota SortAsc SortDesc )
type SortState ¶
type SortState struct {
Key string // SortKey of the sorted column ("" = none)
Direction SortDirection // Sort direction
}
SortState tracks current sort settings using a key name. Unlike SortConfig (which uses column indices), SortState uses string keys so it can persist across table rebuilds and adapt to different column layouts.
func (*SortState) HandleSortKey ¶
HandleSortKey processes a keyboard key press (e.g., "1", "f1") and toggles sort for the corresponding sortable column. Returns true if sort state changed.
func (*SortState) ToConfig ¶
func (s *SortState) ToConfig(columns []Column) SortConfig
ToConfig converts key-based SortState to index-based SortConfig for table rendering.
type Table ¶
type Table struct {
Columns []Column
Rows []Row
SelectedIndex int // Currently selected row (-1 for none)
ViewportOffset int // First visible row index
ViewportHeight int // Number of visible rows (0 = show all)
TerminalWidth int // Terminal width for width calculations
Sort SortConfig
Padding int // Padding between columns (default 2)
// Styles
HeaderStyle lipgloss.Style
SelectedStyle lipgloss.Style
SeparatorChar string // Default "─"
// contains filtered or unexported fields
}
Table holds table state and configuration
func (*Table) CalculateWidths ¶
CalculateWidths computes actual column widths based on terminal width. For flexible columns, widths are capped at actual content width (no wasted space).
func (*Table) ClearRows ¶
func (t *Table) ClearRows()
ClearRows removes all rows from the table and invalidates cached widths
func (*Table) EnsureCursorVisible ¶
func (t *Table) EnsureCursorVisible()
EnsureCursorVisible adjusts viewport to keep selected row visible
func (*Table) GetColumnWidth ¶
GetColumnWidth returns the calculated width for a specific column index. Useful for pre-truncating content before adding rows.
func (*Table) InvalidateWidths ¶
func (t *Table) InvalidateWidths()
InvalidateWidths forces recalculation of column widths on next render
func (*Table) MoveSelection ¶
MoveSelection moves the selection by delta rows (positive = down, negative = up)
func (*Table) NeedsScrollIndicator ¶
NeedsScrollIndicator returns true if viewport scrolling is active
func (*Table) RenderHeader ¶
RenderHeader returns the formatted header row
func (*Table) RenderRows ¶
RenderRows returns visible rows as formatted string
func (*Table) RenderScrollIndicator ¶
RenderScrollIndicator returns scroll percentage indicator
func (*Table) RenderSeparator ¶
RenderSeparator returns the separator line between header and rows
func (*Table) RenderWithScroll ¶
RenderWithScroll returns the complete table with optional scroll indicator. Pass a style for the scroll indicator, or nil to omit it.
func (*Table) ScrollPercentage ¶
ScrollPercentage returns current scroll position as percentage (0-100)
func (*Table) SelectedRow ¶
SelectedRow returns the currently selected row, or nil if none selected
func (*Table) SetTerminalWidth ¶
SetTerminalWidth sets the terminal width and invalidates cached widths
func (*Table) VisibleRows ¶
VisibleRows returns the slice of rows currently visible in the viewport
type TruncateMode ¶
type TruncateMode int
TruncateMode specifies how to truncate content that's too long
const ( TruncateEnd TruncateMode = iota // Truncate from end: "hello..." (default) TruncateStart // Truncate from start: "...world" (good for paths) )