1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main

import (
	"strings"

	"github.com/charmbracelet/lipgloss"
	"github.com/charmbracelet/x/ansi"
)

// TableColumn defines a column in the table
type TableColumn struct {
	Name       string                // Header text
	Width      int                   // Content width (0 = auto-expand for last column)
	Align      lipgloss.Position     // Text alignment (Left, Right, Center)
	RenderCell func(data any) string // Custom renderer per column
}

// Table represents a configurable table with columns
type Table struct {
	columns     []TableColumn
	windowWidth int
	borderStyle lipgloss.Style
}

// NewTable creates a new table
func NewTable(columns []TableColumn, windowWidth int) *Table {
	return &Table{
		columns:     columns,
		windowWidth: windowWidth,
		borderStyle: lipgloss.NewStyle(), // Default no border
	}
}

// calculateLastColumnWidth computes the width for the last column to fill remaining space
func (t *Table) calculateLastColumnWidth() int {
	if len(t.columns) == 0 {
		return 20 // default minimum
	}

	// Sum up all fixed-width columns (all except last)
	totalFixedWidth := 0
	for i := 0; i < len(t.columns)-1; i++ {
		totalFixedWidth += t.columns[i].Width
	}

	// Calculate spaces between columns (1 space between each column)
	numInterColumnSpaces := len(t.columns) - 1

	// Account for border frame size
	borderFrameSize := t.borderStyle.GetHorizontalFrameSize()

	// Calculate remaining width
	remainingWidth := max(
		// Enforce minimum width
		t.windowWidth-totalFixedWidth-numInterColumnSpaces-borderFrameSize, 10)

	return remainingWidth
}

// RenderHeader renders the table header row
// isAtTop indicates whether we're scrolled to the top (affects color)
func (t *Table) RenderHeader(isAtTop bool) string {
	var parts []string

	lastColWidth := t.calculateLastColumnWidth()

	// Use blue at top, grey when scrolled
	headerColor := lipgloss.Color("12") // Blue
	if !isAtTop {
		headerColor = lipgloss.Color("246") // Dim grey
	}

	for i, col := range t.columns {
		width := col.Width
		if i == len(t.columns)-1 && col.Width == 0 {
			width = lastColWidth
		}

		// Truncate and pad header text
		headerText := truncateAndPad(col.Name, width, col.Align)

		// Apply bold styling with color based on scroll position
		styledHeader := lipgloss.NewStyle().
			Bold(true).
			Foreground(headerColor).
			Render(headerText)

		parts = append(parts, styledHeader)
	}

	return strings.Join(parts, " ")
}

// RenderRow renders a normal table row
func (t *Table) RenderRow(data any) string {
	var parts []string

	lastColWidth := t.calculateLastColumnWidth()

	for i, col := range t.columns {
		width := col.Width
		if i == len(t.columns)-1 && col.Width == 0 {
			width = lastColWidth
		}

		// Render cell content
		cellContent := col.RenderCell(data)

		// Truncate and pad
		formattedCell := truncateAndPad(cellContent, width, col.Align)

		parts = append(parts, formattedCell)
	}

	return strings.Join(parts, " ")
}

// RenderRowWithBackground renders a row with a background color (for selection)
func (t *Table) RenderRowWithBackground(data any, bgColor lipgloss.Color) string {
	rowContent := t.RenderRow(data)

	return lipgloss.NewStyle().
		Background(bgColor).
		Render(rowContent)
}

// truncateAndPad truncates content to maxWidth and pads according to alignment
func truncateAndPad(content string, width int, align lipgloss.Position) string {
	// Use ANSI-aware width calculation
	contentWidth := ansi.StringWidth(content)

	// Truncate if too long
	if contentWidth > width {
		content = ansi.Truncate(content, width, "…")
		contentWidth = width
	}

	// Calculate padding
	padding := width - contentWidth

	// Apply alignment
	switch align {
	case lipgloss.Left:
		return content + strings.Repeat(" ", padding)
	case lipgloss.Right:
		return strings.Repeat(" ", padding) + content
	case lipgloss.Center:
		leftPad := padding / 2
		rightPad := padding - leftPad
		return strings.Repeat(" ", leftPad) + content + strings.Repeat(" ", rightPad)
	default:
		// Default to left alignment
		return content + strings.Repeat(" ", padding)
	}
}