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.
This commit is contained in:
Fredrik Kvalvik 2024-11-25 19:13:11 +01:00 committed by Tim Hårek Andreassen
parent 0d9cea8b95
commit 9449cba68d
No known key found for this signature in database
GPG key ID: E59C7734F0E10EB5
2 changed files with 27 additions and 8 deletions

View file

@ -45,6 +45,7 @@ func forecast(cmd *cobra.Command, args []string) {
if !isUTC { if !isUTC {
item.Time = item.Time.Local() item.Time = item.Time.Local()
} }
t.Row( t.Row(
item.Time.Format("Mon, 2 Jan 15:04"), item.Time.Format("Mon, 2 Jan 15:04"),
fmt.Sprintf("%.1f °C", item.Temperature), fmt.Sprintf("%.1f °C", item.Temperature),

View file

@ -16,22 +16,40 @@ func New() *table.Table {
lightGray = lipgloss.Color("#dedede") lightGray = lipgloss.Color("#dedede")
) )
var ( var (
HeaderStyle = re.NewStyle().Foreground(white).Bold(true).Align(lipgloss.Center) HeaderStyle = re.
CellStyle = re.NewStyle().Padding(0, 2) NewStyle().
Foreground(white).
Bold(true).
Align(lipgloss.Center)
CellStyle = re.NewStyle().
Padding(0, 2).
AlignHorizontal(lipgloss.Right)
EvenRowStyle = CellStyle.Foreground(lightGray) EvenRowStyle = CellStyle.Foreground(lightGray)
) )
t := table.New(). t := table.New().
Border(lipgloss.RoundedBorder()). Border(lipgloss.RoundedBorder()).
StyleFunc(func(row, col int) lipgloss.Style { StyleFunc(func(row, col int) lipgloss.Style {
switch { if row == 0 {
case row == 0:
return HeaderStyle return HeaderStyle
case row%2 == 0:
return EvenRowStyle
default:
return CellStyle
} }
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") Headers("time", "temp.", "rain", "wind")
return t return t