Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions internal/renderer/v2/block_rain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"fmt"
"math"
"strings"

"github.com/chubin/wttr.in/internal/options"
)

// sparkBars as []rune — correct Unicode handling
var sparkBars = []rune(" _▁▂▃▄▅▇█")

// drawRainSpark — now matches Python v2 label position (TOP of the sparkline)
func drawRainSpark(data, chanceData []float64, height, width int) string {
func drawRainSpark(data, chanceData []float64, height, width int, opts *options.Options) string {
if len(data) == 0 || width == 0 {
return strings.Repeat(" ", width) + "\n"
}
Expand All @@ -27,9 +29,13 @@ func drawRainSpark(data, chanceData []float64, height, width int) string {
for i := 0; i < height; i++ {
for j := 0; j < width; j++ {
rowFromBottom := height - i - 1
character := box(rowFromBottom, data[j], maxValue, height)
var val float64
if j < len(data) {
val = data[j]
}
character := box(rowFromBottom, val, maxValue, height)

if data[j] != 0 && j < len(chanceData) && chanceData[j] > 0 {
if val != 0 && j < len(chanceData) && chanceData[j] > 0 {
chanceOfRain := math.Min(1.0, chanceData[j]/100.0*2.0)
colorIndex := int(5 * chanceOfRain)
colorCode := 16 + colorIndex
Expand All @@ -51,7 +57,17 @@ func drawRainSpark(data, chanceData []float64, height, width int) string {
}
}

label := fmt.Sprintf("%3.2fmm|%d%%", maxValue, int(chanceData[maxCol]))
unit := "mm"
if opts != nil && (opts.UseImperial || opts.UseUscs) {
unit = "in"
}

chance := 0
if maxCol < len(chanceData) {
chance = int(chanceData[maxCol])
}

label := fmt.Sprintf("%3.2f%s|%d%%", maxValue, unit, chance)
coloredLabel := fmt.Sprintf("\033[38;5;33m%s\033[0m", label)

// Python-style alignment
Expand Down
136 changes: 136 additions & 0 deletions internal/renderer/v2/block_rain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package v2

import (
"strings"
"testing"

"github.com/chubin/wttr.in/internal/options"
)

func TestDrawRainSparkMetricUnits(t *testing.T) {
testCases := []struct {
name string
opts *options.Options
expectedUnit string
}{
{
name: "nil options defaults to metric mm",
opts: nil,
expectedUnit: "mm|",
},
{
name: "empty options defaults to metric mm",
opts: &options.Options{},
expectedUnit: "mm|",
},
{
name: "explicit use_metric true",
opts: &options.Options{UseMetric: true},
expectedUnit: "mm|",
},
{
name: "use_metric with use_ms_for_wind",
opts: &options.Options{UseMetric: true, UseMsForWind: true},
expectedUnit: "mm|",
},
}

data := []float64{0.0, 2.5, 12.7, 5.0, 0.0}
chance := []float64{0, 20, 80, 40, 0}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
output := drawRainSpark(data, chance, 5, 20, tc.opts)
if !strings.Contains(output, "12.70mm|80%") {
t.Fatalf("expected output to contain %q, got:\n%s", "12.70mm|80%", output)
}
if !strings.Contains(output, tc.expectedUnit) {
t.Fatalf("expected output to contain unit %q, got:\n%s", tc.expectedUnit, output)
}
})
}
}

func TestDrawRainSparkImperialUnits(t *testing.T) {
testCases := []struct {
name string
opts *options.Options
expectedUnit string
}{
{
name: "use_imperial true",
opts: &options.Options{UseImperial: true},
expectedUnit: "in|",
},
{
name: "use_uscs true",
opts: &options.Options{UseUscs: true},
expectedUnit: "in|",
},
{
name: "both use_imperial and use_uscs true",
opts: &options.Options{UseImperial: true, UseUscs: true},
expectedUnit: "in|",
},
}

data := []float64{0.0, 0.1, 0.5, 0.2, 0.0}
chance := []float64{0, 20, 80, 40, 0}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
output := drawRainSpark(data, chance, 5, 20, tc.opts)
if !strings.Contains(output, "0.50in|80%") {
t.Fatalf("expected output to contain %q, got:\n%s", "0.50in|80%", output)
}
if !strings.Contains(output, tc.expectedUnit) {
t.Fatalf("expected output to contain unit %q, got:\n%s", tc.expectedUnit, output)
}
})
}
}

func TestDrawRainSparkEdgeCases(t *testing.T) {
t.Run("empty data slice", func(t *testing.T) {
out := drawRainSpark([]float64{}, []float64{}, 5, 20, nil)
expected := strings.Repeat(" ", 20) + "\n"
if out != expected {
t.Fatalf("expected %q, got %q", expected, out)
}
})

t.Run("zero width", func(t *testing.T) {
out := drawRainSpark([]float64{1.0, 2.0}, []float64{10, 20}, 5, 0, nil)
if out != "\n" {
t.Fatalf("expected %q, got %q", "\n", out)
}
})

t.Run("all zero data values", func(t *testing.T) {
out := drawRainSpark([]float64{0.0, 0.0, 0.0}, []float64{10, 20, 30}, 5, 15, nil)
expected := strings.Repeat(" ", 15) + "\n"
if out != expected {
t.Fatalf("expected %q, got %q", expected, out)
}
})

t.Run("chanceData shorter than maxCol", func(t *testing.T) {
// maxValue at index 3, chanceData only has 2 elements
data := []float64{0.0, 1.0, 2.0, 8.5}
chance := []float64{10, 20}
out := drawRainSpark(data, chance, 5, 20, &options.Options{UseMetric: true})
if !strings.Contains(out, "8.50mm|0%") {
t.Fatalf("expected %q with fallback 0%% chance, got:\n%s", "8.50mm|0%", out)
}
})

t.Run("label alignment near right edge", func(t *testing.T) {
// maxValue at the very end of line
data := []float64{0.0, 0.0, 0.0, 0.0, 0.0, 5.0}
chance := []float64{0, 0, 0, 0, 0, 90}
out := drawRainSpark(data, chance, 3, 12, &options.Options{UseMetric: true})
if !strings.Contains(out, "5.00mm|90%") {
t.Fatalf("expected right aligned label in output, got:\n%s", out)
}
})
}
15 changes: 12 additions & 3 deletions internal/renderer/v2/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ func (r *V2Renderer) Render(q domain.Query, localizer localization.Localizer) (d
}

opts := q.Options
if opts == nil {
opts = &options.Options{}
}
loc := q.Location
l10n := localization.New(localizer, q.Options)
l10n := localization.New(localizer, opts)

var buf bytes.Buffer

Expand All @@ -65,11 +68,17 @@ func (r *V2Renderer) Render(q domain.Query, localizer localization.Localizer) (d
buf.WriteString("\n")

// Rain sparkline
precipValues := extractAllHourlyFloat(weather, func(h domain.Hourly) string { return h.PrecipMM })
isImperial := opts.UseImperial || opts.UseUscs
precipValues := extractAllHourlyFloat(weather, func(h domain.Hourly) string {
if isImperial {
return h.PrecipInches
}
return h.PrecipMM
})
chanceValues := extractAllHourlyFloat(weather, func(h domain.Hourly) string { return h.ChanceOfRain })
precipInterp := interpolate(precipValues, 72)
chanceInterp := interpolate(chanceValues, 72)
buf.WriteString(drawRainSpark(precipInterp, chanceInterp, 5, 72))
buf.WriteString(drawRainSpark(precipInterp, chanceInterp, 5, 72, opts))
buf.WriteString("\n")

// Weather emojis
Expand Down
Loading