From 9449cba68d7b2019fb256f00aad9589ebb3cf7e0 Mon Sep 17 00:00:00 2001 From: Fredrik Kvalvik Date: Mon, 25 Nov 2024 19:13:11 +0100 Subject: [PATCH] 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. --- cmd/forecast.go | 1 + cmd/internal/ui/table/table.go | 34 ++++++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/cmd/forecast.go b/cmd/forecast.go index 28af1d7..5aacd29 100644 --- a/cmd/forecast.go +++ b/cmd/forecast.go @@ -45,6 +45,7 @@ func forecast(cmd *cobra.Command, args []string) { if !isUTC { item.Time = item.Time.Local() } + t.Row( item.Time.Format("Mon, 2 Jan 15:04"), fmt.Sprintf("%.1f °C", item.Temperature), diff --git a/cmd/internal/ui/table/table.go b/cmd/internal/ui/table/table.go index bc16aed..31bb454 100644 --- a/cmd/internal/ui/table/table.go +++ b/cmd/internal/ui/table/table.go @@ -16,22 +16,40 @@ func New() *table.Table { lightGray = lipgloss.Color("#dedede") ) var ( - HeaderStyle = re.NewStyle().Foreground(white).Bold(true).Align(lipgloss.Center) - CellStyle = re.NewStyle().Padding(0, 2) + 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 { - switch { - case row == 0: + if row == 0 { 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") return t