yr/cmd/internal/ui/table/table.go
Fredrik Kvalvik 9449cba68d
style: right align cells for better readability
improve readability by right-aligning text. This
makes reading a mix of positive/negative numbers,
or 1/2 digit numbers easier.

This also makes it a bit easier to parse a longer
forecast when it spans mulitple days, as the time
stays in the same place.
2024-11-25 19:27:09 +01:00

56 lines
1 KiB
Go

package table
import (
"os"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
)
// TODO: Make more generic
func New() *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("time", "temp.", "rain", "wind")
return t
}