yr/cmd/internal/ui/table/table.go
Tim Hårek Andreassen ffe81b005d
refactor: Make table.New generic
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
2024-11-25 19:30:34 +01:00

55 lines
995 B
Go

package table
import (
"os"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
)
func New(headers ...string) *table.Table {
re := lipgloss.NewRenderer(os.Stdout)
const (
white = lipgloss.Color("#fff")
lightGray = lipgloss.Color("#dedede")
)
var (
HeaderStyle = re.
NewStyle().
Foreground(white).
Bold(true).
Align(lipgloss.Center)
CellStyle = re.NewStyle().
Padding(0, 2).
AlignHorizontal(lipgloss.Right)
EvenRowStyle = CellStyle.Foreground(lightGray)
)
t := table.New().
Border(lipgloss.RoundedBorder()).
StyleFunc(func(row, col int) lipgloss.Style {
if row == 0 {
return HeaderStyle
}
var style lipgloss.Style
switch {
case row%2 == 0:
style = EvenRowStyle
default:
style = CellStyle
}
// right align numeric values for better readability
if col > 0 && row > 1 {
style.AlignHorizontal(lipgloss.Right)
}
return style
}).
Headers(headers...)
return t
}