2024-10-03 20:37:54 +00:00
|
|
|
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 (
|
2024-11-25 18:13:11 +00:00
|
|
|
HeaderStyle = re.
|
|
|
|
NewStyle().
|
|
|
|
Foreground(white).
|
|
|
|
Bold(true).
|
|
|
|
Align(lipgloss.Center)
|
|
|
|
|
|
|
|
CellStyle = re.NewStyle().
|
|
|
|
Padding(0, 2).
|
|
|
|
AlignHorizontal(lipgloss.Right)
|
|
|
|
|
2024-10-03 20:37:54 +00:00
|
|
|
EvenRowStyle = CellStyle.Foreground(lightGray)
|
|
|
|
)
|
|
|
|
|
|
|
|
t := table.New().
|
2024-10-05 16:33:20 +00:00
|
|
|
Border(lipgloss.RoundedBorder()).
|
2024-10-03 20:37:54 +00:00
|
|
|
StyleFunc(func(row, col int) lipgloss.Style {
|
2024-11-25 18:13:11 +00:00
|
|
|
if row == 0 {
|
2024-10-03 20:37:54 +00:00
|
|
|
return HeaderStyle
|
2024-11-25 18:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var style lipgloss.Style
|
|
|
|
switch {
|
2024-10-03 20:37:54 +00:00
|
|
|
case row%2 == 0:
|
2024-11-25 18:13:11 +00:00
|
|
|
style = EvenRowStyle
|
2024-10-03 20:37:54 +00:00
|
|
|
default:
|
2024-11-25 18:13:11 +00:00
|
|
|
style = CellStyle
|
|
|
|
}
|
|
|
|
|
|
|
|
// right align numeric values for better readability
|
|
|
|
if col > 0 && row > 1 {
|
|
|
|
style.AlignHorizontal(lipgloss.Right)
|
2024-10-03 20:37:54 +00:00
|
|
|
}
|
2024-11-25 18:13:11 +00:00
|
|
|
|
|
|
|
return style
|
2024-10-03 20:37:54 +00:00
|
|
|
}).
|
|
|
|
Headers("time", "temp.", "rain", "wind")
|
|
|
|
return t
|
|
|
|
}
|