ffe81b005d
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
55 lines
995 B
Go
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
|
|
}
|